C语言学习5-1:字符加法,字符串操作函数编写,查找数组中第二大的数
1,字符加法
#include <stdio.h> #include <string.h> void reverse(char s[]) //反向遍历 { int i; int len = strlen(s); for (i = 0; i < len / 2; i++) { s[i] ^= s[len - i - 1]; s[len - i - 1] ^= s[i]; s[i] ^= s[len - i - 1]; } } void add_str(char sum[], char s1[], char s2[])//字符串数字相加 { int len1 = strlen(s1); int len2 = strlen(s2); int len_max = len1 > len2 ? len1 : len2;//选取最大的作为循环长度 reverse(s1);//切换高位和地位的位置 reverse(s2);//切换高位和地位的位置 int i; int digit, carry;//digit是存储当前位数相加的值 for (i = 0, digit = 0; i < len_max; i++, digit = 0) { if (s1[i] != '\0') digit += s1[i] - '0';//变成数字 if (s2[i] != '\0') digit += s2[i] - '0';//变成数字 digit += sum[i]; //把上次的进位加上 sum[i] = digit % 10;//当前数字 carry = digit / 10;//进位 if (carry != 0) sum[i + 1] = carry;//把进位加到下一个位中 sum[i] += '0';//变成字符 } if (sum[i] != '\0') sum[i] += '0';//最后看是否有进位,有的话就加上,比如1000+9000的10000结果就是 reverse(sum); } int main(void) { char s1[31] = { 0 }; char s2[31] = { 0 }; char sum[32] = { 0 }; printf("input num1: "); gets(s1); printf("input num2: "); gets(s2); add_str(sum, s1, s2); printf("the sum is: %s\n", sum); return 0; }
结果:
will@will-Inspiron-N4010:~/c/4th$ ./a.out input num1: 1234 input num2: 9899 the sum is: 11133
2,字符串操作函数编写
#include <stdio.h> int my_strlen(char s[]) { int i = 0; while (s[i] != '\0') i++; return i; } void my_strcpy(char dst[], char src[]) { int i = 0, j = 0; while ((dst[i++] = src[j++]) != '\0') ; } void my_strcat(char dst[], char src[]) { int i = 0, j = 0; while (dst[i] != '\0') i++; while ((dst[i++] = src[j++]) != '\0') ; } int my_strcmp(char s1[], char s2[]) { int i = 0, j = 0; while (s1[i] == s2[j] && s1[i] != '\0') i++, j++; return s1[i] - s2[j]; } int my_strchr(char s[], int ch) { int i; for (i = 0; s[i] != '\0'; i++) { if (ch == s[i]) return i; } return -1; } int my_strstr(char s[], char substr[]) { int i = 0, j; int start_cmp; while (s[i] != '\0') { start_cmp = i, j = 0; while (s[i] == substr[j] && substr[j] != '\0') i++, j++; if (substr[j] == '\0') return start_cmp; i = start_cmp; i++; } return -1; } int main(void) { char s1[128]; char s2[128]; printf("test strlen, input s1:"); gets(s1); printf("len of s1 is %d\n", my_strlen(s1)); printf("test strcpy, input s1: "); gets(s1); my_strcpy(s2, s1); printf("s2: %s\n", s2); printf("test strcat, input s1: "); gets(s1); my_strcat(s2, s1); printf("s2: %s\n", s2); printf("test strcmp, input s1: "); gets(s1); printf("test strcmp, input s2: "); gets(s2); if (strcmp(s1, s2) > 0) printf("%s > %s\n", s1, s2); else if (strcmp(s1, s2) < 0) printf("%s < %s\n", s1, s2); else printf("%s == %s\n", s1, s2); int ch, index; printf("test strchr, input s1: "); gets(s1); printf("input ch: "); ch = getchar(); if ((index = my_strchr(s1, ch)) != -1) printf("%s has %c, index = %d\n", s1, ch, index); else printf("not find.\n"); while (getchar() != '\n') ; printf("test strstr, input s1: "); gets(s1); printf("input s2: "); gets(s2); if ((index = my_strstr(s1, s2)) != -1) printf("%s has %s, index = %d\n", s1, s2, index); else printf("not find.\n"); return 0; }
3,查找第二个最大的
#include <stdio.h> void rand_a(int a[], int len) { int i; for (i = 0; i < len; i++) a[i] = rand() % 100; } void print_a(int a[], int len) { int i; for (i = 0; i < len; i++) printf("%d ", a[i]); putchar('\n'); } int second_max(int a[], int len) { int i, max = a[0], min = a[0]; for (i = 0; i < len; i++) { if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; } int sec_max = min; for (i = 0; i < len; i++) { if (sec_max < a[i] && a[i] < max) sec_max = a[i]; } return sec_max; } int main(void) { int a[10]; rand_a(a, 10); print_a(a, 10); printf("second max is %d\n", second_max(a, 10)); return 0; }
这个太简单我就不说了