編一個程序,將兩個字符串連接起來,不要用strcat函數。
#include <stdio.h>void my_strcat(char *s1, const char *s2) {while (*s1) {s1++;}while (*s2) {*s1 = *s2;s1++;s2++;}*s1 = '\0';
}int main() {char s1[100] = "Hello, ";char s2[] = "World!";my_strcat(s1, s2);printf("連接后的字符串:%s\n", s1);return 0;
}
代碼說明:
- 將兩個字符串連接起來,且不使用標準庫中的`strcat`函數。
- 通過遍歷第一個字符串找到其結束位置,然后逐個復制第二個字符串的字符到第一個字符串末尾,最后添加結束符`'\0'`。