C言語練習問題:文字列操作関数練習問題

53092 ワード

1、文字数を含む文字列を分けて、分けた文字列の前の部分が数字の後の部分がアルファベットになるようにします.例えば「h 1 ell 2 o 3」->「123 hello」
#include 
#include 
#include 
void divide_str(char* str1, char* str2);
int main() {
	char c[50] = {0};
	char a[50] = {0};
	printf("      :
"
); 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(" :
"
); puts(str2); }

2.文字列のスペースを「%020」に置換
#include 
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、文字列で指定した文字を削除します.例えば「abcdaefghiagkl」は「a」を削除し、以降は「bcdefghigkl」
#include 
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
"
, word); } int main() { char c[50] = {0}; printf("input str:
"
); while (gets(c) != NULL) { delete_char(c); printf("input str again:
"
); getchar(); } }

4、配列内の重複する要素を削除します.例えば1,2,2,2,3,3,4,4,5,5,5,6,6->1,2,3,4,5,6
#define _CRT_SECURE_NO_WARNINGS
#include 
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 
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、大整数加算、任意範囲の2つの整数の加算を実現する(整数の範囲はint型の変数では表せない、50ビット)
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
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   :
"
); count--; while (count >= 0) { printf("%d", a[count]); count--; } printf("
"
); } 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 ):
"
); while ((data = getchar()) != '
'
){ c[i++] = data - 48; } reserve_str(c, i); printf(" B( 50 ):
"
); while ((data = getchar()) != '
'
) { a[j++] = data - 48; } reserve_str(a, j); add_big_int(c, a, i, j); return 0; }

7、1つの文字列の配列の最大値と次の大きい値を求めます
#include 
#include 
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);
}