深入攻克c语言--day04
1.将包含字符数字的字符串分开,使得分开后的字符串前一部分是数字后一部分是字母。例
如“h1ell2o3” ->”123hello”
#include<stdio.h> #include<stdlib.h> void fun(char *c) { char p[100], *loc; loc = c; int i = 0; while (*c != '\0') { if (*loc >= 48 && *loc <= 59) { printf("%c", *loc); } else if (*loc >= 'a'&&*loc <= 'z' || *loc >= 'A'&&*loc <= 'Z') { p[i++] = *loc; } else printf("字符串格式不对\n"); loc++; c++; } p[i] = '\0'; i = 0; while (p[i] != '\0') { printf("%c", p[i++]); } } int main() { char c[] = "h1ell2o3"; fun(c); system("pause"); return 0; }
2.将 字 符 串 中 的 空 格 替 换 成 “%020” , 例 如 “hello world how ”
->”hello%020%020%020world%020%020%020how%020%020%020%020”
#include<stdio.h> #include<stdlib.h> void fun(char *c) { char *loc = (char*)malloc(strlen(c)); strcpy(loc, c); int i = 0; while (*loc != '\0') { if (*loc == ' ') { c[i++] = '%'; c[i++] = '0'; c[i++] = '2'; c[i++] = '0'; } else { c[i++] = *loc; } loc++; } c[i] = '\0'; printf("%s\n", c); } int main() { char c[100] = "hello world how "; fun(c); system("pause"); return 0; }
3.删除字符串中指定的字符。例如 “abcdaefaghiagkl“ 删除‘a’,以后: “bcdefghigkl”
#include<stdio.h> #include<stdlib.h> #include<string.h> void fun(char c[]) { char *s=(char*)malloc(strlen(c)); strcpy(s,c); int i = 0; while (*s != '\0') { if (*s != 'a') { c[i++] = *s; } s++; } c[i] = '\0'; printf("%s", c); } int main() { char c[] = "abcdaefaghiagkl"; fun(c); system("pause"); return 0; }
4.删除一个数组中重复的元素。例如 1 ,2, 2,2,3,3,3,4,4,5,5,5,6,6,6 -> 1,2,3,4,5,6
#include<stdio.h> #include<stdlib.h> #define N 100 int delete_elem(int arr[], int new_arr[]) { int i = 0, new_arr_len = 0; int flag[N] = { 0 };//对每个元素做一个初始标记 for (i = 0; i < 15; i++) { if (flag[arr[i]] == 0) { new_arr[new_arr_len++] = arr[i]; flag[arr[i]] = 1;//出现过则标记属性设为1 } } return new_arr_len; } void main() { int i, arr[N] = {1,2,2,2,3,3,3,4,4,5,5,5,6,6,6}; int new_arr[N]; int arr_len = 15;//输入number的个数 int new_arr_len; new_arr_len = delete_elem(arr, new_arr); printf("\n"); printf("new array is :\n"); for (i = 0; i < new_arr_len; i++) { printf("%d ", new_arr[i]); } printf("\n"); system("pause"); }
5.将 字 符 串 中 的 相 邻 的 多 余 空 格 去 掉 , 例 如 (空 格 用 下 划 线 表
示): ”___hello____world___how_are_you__” ->”hello_world_how_are_you”
#include<stdio.h> #include<stdlib.h> #include<string.h> void fun(char c[]) { int cnt = 0; char *s = (char*)malloc(strlen(c)); strcpy(s, c); int i = 0; while (*s != '\0') { if (*s != ' ') { c[i++] = *s; cnt = 0; }else { cnt++; if(cnt==1)c[i++]=' '; } s++; } c[i] = '\0'; printf("%s", c); } int main() { char a[] = { " hello world how are you " }; fun(a); system("pause"); return 0; }
6.附加题:大整数加法。 实现任意范围的两个整数的加法( 整数的范围用 int 型的变量无法表示,50位)
#include<stdio.h> #include<string.h> #define MAX 100 int main() { int num1[100], num2[100]; memset(num1, 0, sizeof(num1)); memset(num2, 0, sizeof(num2));//将这些数组清零 char *str1 = "12345678901234567890123456789012345678901234567890";//用于保存键盘输入的两个待加大整数 char *str2 = "12345678901234567890123456789012345678301234567823"; int len1, len2, i, j; len1 = strlen(str1); len2 = strlen(str2); j = 0; int max = len1 > len2 ? len1 : len2; for (i = len1 - 1; i >= 0; i--) num1[j++] = str1[i] - '0'; /*字符串反转并且转换为数字保存到数组里面*/ j = 0; for (i = len2 - 1; i >= 0; i--) num2[j++] = str2[i] - '0'; for (i = 0; i < max; i++) { num2[i] += num1[i];//相加 if (num2[i] >= 10) { num2[i] -= 10; num2[i + 1] += 1;//产生进位 } } if (num2[max]) printf("%d", num2[max]);//如果最高位产生进位 for (i = max - 1; i >= 0; i--) printf("%d", num2[i]); printf("\n"); system("pause"); return 0; }
7.求一个字符串数组的最大值和次大值 void big(char *arr[],int size ,char** big1,char** big2)
#include<stdio.h> #include<stdlib.h> #include<string.h> void big(char *arr[], int size, char** big1, char** big2) { int i = 0, j = 0; char* max, *min, (*tmp)[5] = arr; *big1 = tmp[0]; printf("%s\n", *big1); for (i = 0; i < size; i++) { printf("%s ", tmp[i]); } printf("\n"); for (j = 1; j < size; j++) { //printf("\n%s %s\t", arr[i], arr[i + 1]); if (strcmp(*big1, tmp[j]) < 0) { strcpy(*big2, *big1); strcpy(*big1, tmp[j]); } } printf("\n---------------------------------\n"); printf("the biggest string is %s\n", *big1); printf("the bigger string is %s\n", *big2); } int main() { char arr[10][5] = { "hdua","dade","heoc","das3" ,"heod" }; char *big1 = (char*)malloc(10), *big2 = (char*)malloc(10), (*a)[5] = (char*)malloc(25); a = arr; big(arr, 5, &big1, &big2); system("pause"); return 0; }