C语言的常用字符串操作函数(一)

  一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这样丰富而实用,在此记录,已备后用。

No.1  strlen():字符串长度计算函数

应用实例:

1 #include<stdio.h>
2 #include<string.h> 
3 
4 char TextBuff[] = "Hello_My_Friend!";
5 
6 int main(void)
7 {
8     printf("TextBuff的长度是:%d
",strlen(TextBuff));        
9 }

No.2   strcpy():字符串拷贝函数

应用实例:

 1 #include<stdio.h>
 2 #include<string.h> 
 3 
 4 char *TextBuff= "Hello_My_Friend!";
 5 char RevBuff[13];
 6 
 7 int main(void)
 8 {
 9     strcpy(RevBuff,TextBuff);
10     printf("RevBuff:%s
",RevBuff);        
11 }

No.3  strcat():字符串拼接函数

应用实例:

 1 #include<stdio.h>
 2 #include<string.h> 
 3 
 4 int main(void)
 5 {
 6     char *TextBuff;
 7     char *A="IamA";
 8     char *B="IamB";
 9     char *C="IamC";    
10     strcat(TextBuff,A);
11     strcat(TextBuff,B);
12     strcat(TextBuff,C);    
13     printf("TextBuff的长度是:%d
",strlen(TextBuff));
14     printf("%s
",TextBuff);
15 }

No.4   strchr():字符串查找(第一次出现的位置)

应用实例:

 1 #include<stdio.h>
 2 #include<string.h> 
 3 
 4 int main(void)
 5 {
 6     char Text[10]="wearetheAB";
 7     char *Ptr;
 8     char a='a';
 9     
10     Ptr=strchr(Text,a);
11     printf("a的位置在Text的第%d个位置
",Ptr-Text+1);
12 }

No.5  strcmp():字符串比较函数

应用实例:

 1 #include<stdio.h>
 2 #include<string.h> 
 3 
 4 int main(void)
 5 {
 6     char *A="Hello!";
 7     char *B="Hello!";
 8     char Num=0;
 9     Num=strcmp(A,B);
10     if(Num==0)
11     {
12         printf("两个数组相等
");
13     }
14     else
15     {
16         printf("两个数组不相等
");
17     }
18 }
原文地址:https://www.cnblogs.com/achao123456/p/5811612.html