1,统计字符长度(包括字符串的输入输出)
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
gets(str);
printf("%s\n",str);
int len=strlen(str);
printf("%d\n",len);
return 0;
}
2,拼接字符串
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100]={"https://"};
char s2[]={"www.baidu.com"};
strcat(s1,s2);
puts(s1);
return 0;
}
3,拷贝字符串
#include <stdio.h>
#include <string.h>
int main ()
{
char s1[100]={"cuit"};
char s2[]={"hello word!"};
strcpy(s1,s2);
puts(s1);
strcpy(s1,"C Programming Language");//"cuit"会被覆盖
puts(s1);
strcpy(s1+22,"hello!");//s1+22后便不会被覆盖了
puts(s1);
return 0;
}
4,比较字符串大小
#include <stdio.h>
#include <string.h>
int main ()
{
char s1[20]={"A"};
char s2[20]={"B"};
int n=strcmp(s1,s2);
strcmp("apples","apple");
strcmp("Abc","Abc");
strcmp("C Program","C Practice");
return 0;
}