#include <stdio.h> #include <string.h>/*头文件*/ void main() { char s1[100],s2[50]; printf("Enter the first string:\n"); gets(s1); /*运用gets()函数进行输入*/ printf("Enter the second string:\n"); gets(s2); strcat(s1,s2); puts(s1); /*运用puts()函数进行输出*/ char str1[20] = {"how do you do"}, str2[] = {"How are you!"}; strcpy(str1,str2); puts(str1); }
输出函数puts()和gets()函数比printf和scanf的好处在于不用过多考虑格式化问题。函数
#include <stdio.h> #include <string.h> void main() { char str1[20] = {"how do you do"}, str2[] = {"How are you!"}; strcpy(str1,str2);/*字符串拷贝*/ puts(str1); }
字符串比较函数strcmp()、求字符串长度函数strlen()code
#include <stdio.h> #include <string.h> void main() { int a, b, c; char str1[]={"hello"}, str2[]={"how are you!"}; a = strcmp(str1,str2); b = strcmp("Chinese","China"); c = strcmp(str1,"how"); printf("%d,%d,%d",a,b,c);/*按ASCII码比较,若是str1大于str2,输出1,若是str1等于str2,输出0;若是str小于str2,输出-1.*/ printf("%d\n",strlen(str1));/*输出字符串str1的长度*/ printf("%d\n",strlen(str2));/*输出字符串str2的长度*/ }