strcat()函数用于拼接字符串。
示例
//Visual Studio中加上这句才可以使用scanf() //否则只能使用scanf_s() #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdbool.h> //strcat()函数的原型在string.h #include <string.h> #define SIZE 80 char * s_gets(char * st, int n); //argc: 参数个数 argv[]: 参数数组 int main(int argc, char *argv[]) { char flower[SIZE]; char addon[] = "s smell like old shoes."; puts("What is your favorite flower?"); if (s_gets(flower, SIZE)) { //将addon连接到flower后面 strcat(flower, addon); puts(flower); puts(addon); } else puts("End of file encountered!"); puts("bye"); system("pause"); return 0; } // 自己实现读取函数 char * s_gets(char * st, int n) { char * ret_val; int i = 0; ret_val = fgets(st, n, stdin); if (ret_val) //即,ret_val != NULL { while (st[i] != '\n' && st[i] != '\0') i++; if (st[i] == '\n') st[i] = '\0'; else while (getchar() != '\n') continue; } return ret_val; }
运行测试