c语言经典100例

发布于:2024-10-15 ⋅ 阅读:(117) ⋅ 点赞:(0)

1.字符串转为数字

#include <stdio.h>

int strToInt(char *s)
{
	int num=0;
	int sign=1;
	int step=1;
	if (*s == '-')
	{
		sign = -1;
		s++;
	}
	while (*s >= '0'&&*s <= '9')
	{
		num = num*10+(*s-'0');
		step += 10;
		s++;
	}
	
	return num*sign;
}

int main()
{
	char a[10] = "-1234";
	char *s =a ;
	printf("字符型:%s,整形:%d",s,strToInt(s));


	return 0;
}

2.二维字符数组转一维数组

#include <stdio.h>
#include <stdlib.h>

int main()
{
	char a[3][3] = { 'w','w','w','a','a','a','l','l','l'};
	char n[10] = "";
	for (int i = 0,m=0; i < 3; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
			printf("%c",a[i][j]);
			n[m] = a[i][j];
			m++;
		}
		printf("\n");
	}
	n[9] = '\0';
	printf("%s\n",n);

	return 0;
}

3.数字转换为字符串

#include <stdio.h>

void intToString(int num, char *str) {
    int i = 0;
    int isNegative = 0;

    // 处理负数
    if (num < 0) {
        isNegative = 1;
        num = -num; // 将负数转为正数
    }

    // 转换整数为字符串
    do {
        str[i++] = (num % 10) + '0'; // 获取当前数字并转换为字符
        num /= 10; // 去掉当前的最后一位数字
    } while (num > 0);

    // 如果是负数,添加负号
    if (isNegative) {
        str[i++] = '-';
    }

    // 添加字符串结束符
    str[i] = '\0';

    // 反转字符串
    for (int j = 0; j < i / 2; j++) {
        char temp = str[j];
        str[j] = str[i - j - 1];
        str[i - j - 1] = temp;
    }
}

int main() {
    int number = -12345; // 示例整数
    char str[20]; // 确保足够大以存放转换结果

    intToString(number, str); // 调用转换函数
    printf("整数转换的字符串为:%s\n", str); // 输出结果

    return 0;
}

4.统计一行字符串中单词的个数

#include <stdio.h>

int main()
{
	char a[] = "hello world   my man!";
	char *pa = a;
	int count = 0;
	int inWord = 0; // 用于跟踪是否在单词内部

	while (*pa != '\0') // 直到字符串的结尾
	{
		if (*pa == ' ' || *pa == '\t' || *pa == '\n') // 如果是空格、制表符或换行符
		{
			inWord = 0; // 不在单词内部
		}
		else
		{
			if (inWord == 0) // 进入一个新单词
			{
				count++;
				inWord = 1; // 现在在单词内部
			}
		}
		pa++; // 移动到下一个字符
	}

	printf("单词的数量为:%d\n", count);

	return 0;
}

5.删除数组中相同的数

#include <stdio.h>
#include <stdlib.h>

void del(int *a, int size) {
    int *temp = (int*)malloc(size * sizeof(int)); // 临时数组,用于存储去重后的结果
    int pre = 0;
    int rear = 1;
    int j = 0;

    // 第一个元素肯定是要保留的
    temp[j++] = a[pre];

    // 遍历数组
    while (rear < size) {
        if (a[pre] != a[rear]) { // 如果当前元素和前一个元素不同
            temp[j++] = a[rear]; // 保存不重复的元素
            pre = rear;          // 更新前指针
        }
        rear++; // 后指针继续前进
    }

    // 输出去重后的数组
    printf("去重后的数组: ");
    for (int i = 0; i < j; i++) {
        printf("%d ", temp[i]);
    }
    printf("\n");

    free(temp); // 释放动态分配的内存
}

int main() {
    int a[] = {1, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8, 9, 9, 9};
    int size = sizeof(a) / sizeof(a[0]); // 计算数组大小

    del(a, size); // 调用函数删除重复元素

    return 0;
}

6.移动字符串中的内容

#include <stdio.h>

void moveBack(int m, char *p)
{
	char temp[24];  // 临时数组用于保存前 m 个字符
	int n = 0;      // 用于计算字符串长度

	// 手动计算字符串长度
	while (p[n] != '\0') {
		n++;
	}

	if (m > n) m = n;  // 确保 m 不会大于字符串的长度

	// 复制前 m 个字符到 temp 中
	for (int i = 0; i < m; ++i) {
		temp[i] = p[i];
	}

	// 将后面的字符向前移动
	for (int i = m; i < n; ++i) {
		p[i - m] = p[i];
	}

	// 将 temp 中的前 m 个字符放到末尾
	for (int i = 0; i < m; ++i) {
		p[n - m + i] = temp[i];
	}

	// 重新设置字符串的结尾
	p[n] = '\0';
}

int main() {
	char str[] = "world hello ";
	printf("前:%s\n", str);
	moveBack(6, str);
	printf("后:%s\n", str);

	return 0;
}

7.无符号整数去掉最高位

#include <stdio.h>

unsigned int fun(unsigned int a)
{
	int b=1;
	int temp = a;
	while (temp/10>=10)
	{
		b *= 10;
		temp /= 10;
	}
	b=b *10;
	a=a%b;

	return a;
}

int main() {
	printf("%d", fun(7894));
	return 0;
}

8.打印下三角

#include <stdio.h>

void xiasanjiao(int a[4][4])
{
	for (int i=0;i<4;++i)
	{
		for (int j=0; j <=i ; ++j)
		{
			printf("%d ",a[i][j]);
		}
		printf("\n");
	}
	
}

int main()
{
	int a[4][4] = {
		8,0,0,0,
		8,8,0,0,
		8,8,8,0,
		8,8,8,8
	};

	for (int i = 0; i < 4; ++i)
	{
		for (int j = 0; j <4; ++j)
		{
			printf("%d ", a[i][j]);
		}
		printf("\n");
	}
	printf("下三角:\n");
	xiasanjiao(a);

	return 0;
}

9.移动数组中的内容

#include <stdio.h>

void moveBack(int a[10], int m) {
	int b[10] = { 0 };

	// 复制前 m 个元素到 b
	for (int i = 0; i < m; ++i) {
		b[i] = a[i];
	}

	// 将后面的元素向前移动 m 位
	for (int i = m; i < 10; ++i) {
		a[i - m] = a[i];
	}

	// 将 b 中的元素放到数组的后 m 位
	for (int i = 0; i < m; ++i) {
		a[10 - m + i] = b[i];
	}
}

int main() {
	int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	moveBack(a, 3);
	for (int i = 0; i < 10; ++i) {
		printf("%d ", a[i]);
	}
	return 0;
}

10.删除字符串中的空格

#include <stdio.h>

void delSpace(char* a) {
	char* original = a; // 保存原始字符串指针
	char* result = a;   // 用于存储处理后的结果

	// 遍历原始字符串
	while (*a != '\0') {
		if (*a != ' ' && *a != '\t') { // 只复制非空格和非制表符字符
			*result++ = *a; // 复制字符到结果字符串
		}
		a++;
	}
	*result = '\0'; // 添加字符串结束符
}

int main() {
	char a[] = "h  iph  o p";
	delSpace(a);
	printf("%s\n", a); // 打印处理后的字符串
	return 0;
}

11.字符串是否回文

#include <stdio.h>

int huiwen(char* a) {
	int count=0;

	while (a[count] != '\0') {
		count++;
	}

	for (int i=0;i<count/2;++i)
	{
		if (a[i] != a[count - i-1])
			return 0;
	}
	return 1;
}

int main() {
	char a[] = "hiphop";
	
	printf("%d\n", huiwen(a)); // 打印处理后的字符串
	return 0;
}

12.找出链表中的最大值

#include <stdio.h>

typedef struct stu {
	int data;
	struct stu *next;
} Student;

// 查找链表中的最大值
int findMax(Student* h) {
	if (h == NULL) {
		return -1;  // 如果链表为空,返回特殊值 -1 表示无数据
	}

	int max = h->data;  // 假设第一个节点的数据为最大值
	Student *current = h->next;

	// 遍历链表
	while (current != NULL) {
		if (current->data > max) {
			max = current->data;  // 更新最大值
		}
		current = current->next;  // 移动到下一个节点
	}

	return max;
}

int main() {
	// 创建链表节点
	Student s1, s2, s3;
	Student* h;

	s1.data = 120;
	s2.data = 130;
	s3.data = 140;

	// 设置链表指针
	h = &s1;
	s1.next = &s2;
	s2.next = &s3;
	s3.next = NULL;  // 最后一个节点的 next 为 NULL

	// 查找链表中的最大值并打印
	printf("链表中的最大值是: %d\n", findMax(h));

	return 0;
}

13.字符按ASCII码表排序

#include <stdio.h>

// 手动计算字符串长度的函数
int my_strlen(char *str) {
    int len = 0;
    while (str[len] != '\0') {
        len++;
    }
    return len;
}

// 排序中间字符的函数(降序)
void sort_desc(char *arr, int len) {
    for (int i = 0; i < len - 1; i++) {
        for (int j = i + 1; j < len; j++) {
            if (arr[i] < arr[j]) {
                // 交换字符
                char temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

void fun(char str[]) {
    // 手动获取字符串长度
    int len = my_strlen(str);

    // 确保字符串长度为7
    if (len != 7) {
        printf("字符串长度错误\n");
        return;
    }

    // 提取首尾字符
    char first = str[0];
    char last = str[6];

    // 提取中间5个字符到一个数组中
    char middle[6];  // 5个字符 + 1个终止符
    for (int i = 0; i < 5; i++) {
        middle[i] = str[i + 1];  // 从 str[1] 开始
    }
    middle[5] = '\0';  // 手动添加字符串结束符

    // 对中间字符进行按ASCII码降序排序
    sort_desc(middle, 5);

    // 输出结果:首字符 + 排序后的中间部分 + 尾字符
    printf("%c%s%c\n", first, middle, last);
}

int main() {
    char str[] = "CEAedca";
    fun(str); // 调用函数进行排序
    return 0;
}

14.成绩表中挑出某分数段的人

#include <stdio.h>

// 定义学生结构体
typedef struct {
	int id;    // 学号
	int score; // 成绩
} Student;

// 函数 fun:筛选分数在指定范围内的学生,并返回人数
int fun(Student a[], int N, Student b[], int low, int high) {
	int count = 0; // 用于统计符合条件的学生人数

	// 遍历学生数组
	for (int i = 0; i < N; i++) {
		// 如果学生成绩在指定范围内(包含 low 和 high)
		if (a[i].score >= low && a[i].score <= high) {
			// 将该学生记录放入 b 数组中
			b[count] = a[i];
			count++; // 增加符合条件的学生数量
		}
	}

	// 返回符合条件的学生人数
	return count;
}

int main() {
	// 假设有 5 个学生,主函数中已初始化
	Student a[5] = {
		{1, 55},
		{2, 65},
		{3, 70},
		{4, 66},
		{5, 60}
	};

	Student b[5]; // 用于存储符合条件的学生
	int low = 60; // 分数下限
	int high = 69; // 分数上限

	// 调用 fun 函数,筛选分数范围内的学生
	int count = fun(a, 5, b, low, high);

	// 输出符合条件的学生数据
	printf("符合分数范围 %d 到 %d 的学生有 %d 名:\n", low, high, count);
	for (int i = 0; i < count; i++) {
		printf("学号: %d, 成绩: %d\n", b[i].id, b[i].score);
	}

	return 0;
}

15.字符串中删除ASCII码为奇数的

#include <stdio.h>
#include <stdlib.h> // 包含 malloc 函数

char *oushu(char *a) {
	int i = 0, count = 0;

	// 分配足够的内存,假设最多有16个偶数字符
	char *result = (char *)malloc(sizeof(char) * 16);

	// 遍历字符串,找到偶数ASCII码值的字符
	while (*a != '\0') {
		if (*a % 2 == 0) { // 判断是否为偶数ASCII字符
			result[count + 1] = *a; // 存储在result数组中
			count++;
		}
		a++;
	}

	// 第一个位置存储偶数字符的数量
	result[0] = count;

	return result;
}

int main() {
	char a[] = "abcdef"; // 示例字符串

	// 调用函数获取偶数字符
	char *p = oushu(a);

	// 输出偶数字符
	for (int i = 1; i <= p[0]; ++i) {
		printf("%c\n", p[i]);
	}

	// 释放动态分配的内存
	free(p);

	return 0;
}

16.找出链表中的最小值(可能不止一个)

#include <stdio.h>

typedef struct stu {
	int data;
	struct stu *next;
} Student;

int findMin(Student* h) {
	if (h == NULL) {
		return -1;  
	}

	int min = h->data;  
	int count = 0;
	Student *current = h->next;

	// 遍历链表
	while (current != NULL) {
		if (current->data < min) {
			min = current->data;  
		}
		current = current->next;  
	}
	current = h;
	while (current != NULL) {
		if (current->data == min) {
			count++;
		}
		current = current->next;  // 移动到下一个节点
	}

	return count;
}

int main() {
	// 创建链表节点
	Student s1, s2, s3;
	Student* h;

	s1.data = 120;
	s2.data = 120;
	s3.data = 120;

	// 设置链表指针
	h = &s1;
	s1.next = &s2;
	s2.next = &s3;
	s3.next = NULL;  

	printf("链表中的最小值是: %d\n", findMin(h));

	return 0;
}

17.字符串后移

#include <stdio.h>

void fun(char *s) {
	int i = 0, j = 0;
	int star_count = 0;

	// 统计前导的 * 号数量
	while (s[i] != '\0') {
		if (s[i] == '*') {
			star_count++;
		}
		else {
			// 将非 * 的字符依次移动到最前面
			s[j++] = s[i];
		}
		i++;
	}

	// 在字符串的后面填充前导的 * 号
	while (star_count--) {
		s[j++] = '*';
	}

	// 添加字符串的结束符号
	s[j] = '\0';
}

int main() {
	char str[] = "******A*BC*DEF*G***";
	printf("原字符串: %s\n", str);

	fun(str);

	printf("处理后字符串: %s\n", str);

	return 0;
}

18.删除指定位置的字符

#include <stdio.h>
#include <stdlib.h>

char* fun(char *s1, char* s2, int n) {
	int i = 0, j = 0;
	n = n - 1;
	// 遍历字符串 s1
	while (s1[i] != '\0') {
		// 跳过第 n 个字符
		if (i == n) {
			i++;  // 跳过 s1 的第 n 个字符
			continue;
		}
		s2[j] = s1[i];  // 将字符从 s1 复制到 s2
		i++;
		j++;
	}
	s2[j] = '\0';  // 添加字符串结束符
	return s2;
}

int main() {
	char str[] = "haiphop";

	char *str2 = (char*)malloc((sizeof(str)) * sizeof(char));

	if (str2 == NULL) {
		printf("内存分配失败!\n");
		return 1;
	}

	printf("原字符串: %s\n", str);

	fun(str, str2, 2);

	printf("处理后字符串: %s\n", str2);

	free(str2); 
	return 0;
}

19.找出最长的字符串


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

void findLongestString(char *strings[], int count, char **max) {
	*max = strings[0];  // 默认第一个字符串为最长
	int maxLength = strlen(strings[0]);

	for (int i = 1; i < count; i++) {
		int currentLength = strlen(strings[i]);
		if (currentLength > maxLength) {
			maxLength = currentLength;
			*max = strings[i];  // 更新最长字符串
		}
	}
}

int main() {
	char *strings[100];  // 假设最多输入100个字符串
	int count = 0;
	char *input;

	printf("请输入字符串(输入**结束):\n");
	while (1) {
		input = (char *)malloc(100 * sizeof(char)); // 分配内存存储输入字符串
		scanf("%s", input);
		if (strcmp(input, "**") == 0) {
			free(input); // 释放内存
			break; // 输入结束
		}
		strings[count++] = input; // 存储输入的字符串
	}

	char *longest;
	findLongestString(strings, count, &longest);
	printf("最长的字符串是:%s\n", longest);

	// 释放分配的内存
	for (int i = 0; i < count; i++) {
		free(strings[i]);
	}

	return 0;
}

20.阶乘

#include <stdio.h>

int jiechen(int a)
{
	if (a == 1)
		return 1;
	return a * jiechen(a-1);
}

int main() {

	printf("%d",jiechen(5));
	return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到