C语言习题:字符串操作函数练习题目

1、将包含字符数字的字符串分开, 使得分开后的字符串前一部分是数字后一部分是字母。例如“h1ell2o3”->”123hello”

#include <stdio.h>
#include <string.h>
#include <ctype.h>
void divide_str(char* str1, char* str2);
int main() {
	char c[50] = {0};
	char a[50] = {0};
	printf("请输入字符串:\n");
	while (gets(c) != NULL) {
		divide_str(c, a);
	}
	return 0;
}
void divide_str(char* str1, char* str2) {
	if (str1 == NULL || str2 == NULL) {
		return;
	}
	int len = strlen(str1);
	int j = 0;
	for (int i = 0; i < len; i++) {
		if (str1[i] >= '0' && str1[i] <= '9') {
			str2[j] = str1[i];
			j++;
		}
	}
	for (int i = 0; i < len; i++) {
		if (isalpha(str1[i])) {
			str2[j] = str1[i];
			j++;
		}
	}
	printf("转化后的字符串顺序为:\n");
	puts(str2);
}

2.将字符串中的空格替换成“ % 020”

#include <stdio.h>
void print_str(char* str) {
	if (str == NULL) {
		return;
	}
	int i = 0;
	int j = 0;
	char word[50] = { 0 };
	while (str[i]) {
		if (str[i] == ' ') {
			printf("%s", word);
			printf("%s", "%020");
			memset(word, 0, sizeof(word));
			j = 0;
			i++;
		}
		else {
			word[j] = str[i];
			i++;
			j++;
		}
	}
	puts(word);
}
int main() {
	char str[50] = {0};
	puts("请输入一行字符串:");
	while (gets(str) != NULL) {
		print_str(str);
		puts("请再次输入一行字符串:");
	}
	return 0;
}

3、删除字符串中指定的字符。例如“abcdaefaghiagkl“删除‘a’, 以后:“bcdefghigkl”

#include <stdio.h>
void delete_char(char* str) {
	if (str == NULL) {
		return;
	}
	char word[50] = {0};
	char a = '\0';
	int i = 0, j = 0;
	puts("please input need to deleted char:");
	scanf("%c", &a);
	while (str[i]) {
		if (str[i] != a) {
			word[j] = str[i];
			j++;
		}
		i++;
	}
	printf("%s\n", word);
}
int main() {
	char c[50] = {0};
	printf("input str:\n");
	while (gets(c) != NULL) {
		delete_char(c);
		printf("input str again:\n");
		getchar();
	}
}

4、删除一个数组中重复的元素。例如 1,2,2,2,3,3,3,4,4,5,5,5,6,6,6 -> 1,2,3,4,5,6

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void delete_num(char* num) {
	if (num == NULL) {
		return;
	}
	char c[50] = { 0 };
	int i = 0, j = 0;
	int len = strlen(num);
	while(i < len) {
		if (num[i] != num[i + 1]) {
			c[j] = num[i];
			i++;
			j++;
		}
		else {
			i++;
		}
	}
	puts(c);
}
int main() {
	char num[50];
	while (gets(num) != NULL) {
		delete_num(num);
	}
	return 0;
}

5、将字符串中的相邻的多余空格去掉,例如(空格用下划线表示): __hello____world___how_are_you -> hello_world_how_are_you

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void delete_blank(char* str) {
	if (str == NULL) {
		return;
	}
	int i = 0;
	int j = 0;
	char word[50] = { 0 };
	while (str[i]) {
		if (str[i] == ' ') {
			i++;
		}
		else if (str[i] != ' '){
			word[j] = str[i];
			i++;
			j++;
			if (str[i] == ' ') {
				printf("%s ", word);
				memset(word, 0, sizeof(word));
				j = 0;
				i++;
			}
		}
	}
	puts(word);
}
int main() {
	char c[50] = {0};
	while (gets(c) != NULL) {
		delete_blank(c);
	}
	return 0;
}

6、大整数加法,实现任意范围的两个整数的加法( 整数的范围用 int 型的变量无法表示,50位)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void reserve_str(int* str, int len);
void add_big_int(int* a, int* b, int len1, int len2) {
	int k = 0, t = 0;
	int count = 0;            //记录计算结果数字的位数
	if (len1 > len2) {        //第一个数长度大于第二个数
		while (t <= len1 - 1) {
			k = a[t] + b[t];
			if (k < 10) {
				a[t] = a[t] + b[t];
			}
			else {
				a[t] = k % 10;
				a[t + 1] = a[t + 1] + (k - a[t]) / 10;
			}
			t++;
		}
		if (a[t] != 0 && a[t] > 0) {
			count = t + 1;
		}
		else {
			count = t;
		}
	}
	else {                          
		while (t <= len2 - 1) {
			k = a[t] + b[t];
			if (k < 10) {
				a[t] = a[t] + b[t];
			}
			else {
				a[t] = k % 10;
				a[t + 1] = a[t + 1] + (k - a[t]) / 10;
			}
			t++;
		}
		if (a[t] != 0 && a[t] > 0) {
			count = t + 1;
		}
		else {
			count = t;
		}
	}
	printf("超大数字A和B的和为:\n");
	count--;
	while (count >= 0) {
		printf("%d", a[count]);
		count--;
	}
	printf("\n");
}
void reserve_str(int* str, int len) {
	int start = 0;
	char c = 0;
	while (start < len) {
		c = str[start];
		str[start] = str[len - 1];
		str[len - 1] = c;
		start++;
		len--;
	}
	return;
}
int main() {
	int c[100] = { 0 };
	int a[100] = { 0 };
	char data;
	int i = 0, j = 0;
	printf("请输入一个超大数字A(超过50个字节):\n");
	while ((data = getchar()) != '\n'){
		c[i++] = data - 48;
	}
	reserve_str(c, i);
	printf("请输入一个超大数字B(超过50个字节):\n");
	while ((data = getchar()) != '\n') {
		a[j++] = data - 48;
	}
	reserve_str(a, j);
	add_big_int(c, a, i, j);
	return 0;
}

7、求一个字符串数组的最大值和次大值

#include <stdio.h>
#include <string.h>
void big(char* arr[], int size, char** big1, char** big2);
int main() {
	char* arr[5] = {"wuhan", "hello","word","dwdeedef","15646959" };
	char* max = arr[0];   //最大字符串和次大字符串的指针
	char* c_max = arr[1];  
	big(arr, 5, &max, &c_max);
	return 0;
}
void big(char* arr[], int size, char** big1, char** big2) {
	for (int i = 1; i < size; ++i) {
		if (strcmp(*big1, arr[i]) < 0) {      //当前字符串大于最大字符串
			*big2 = *big1;
			*big1 = arr[i];
		}
		else if (strcmp(*big2, arr[i]) < 0) {     //当前字符串大于次大字符串
			*big2 = arr[i];
		}
	}
	puts("最大字符串:");
	puts(*big1);
	puts("次大字符串:");
	puts(*big2);
}
  • 4
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值