天天看点

C语言函数第三篇------String家族(处理字符串的):strcpy,strcmp,strchr ,strstr,sprintf,strcatstrcpystrcmpstrchrstrstrsprintfstrcat

这个家族很大

它们专门处理字符串

C语言是没有字符串类型的,它实际上就是一个字符数组

下面是它们的成员

登场吧,你认识它们几个?

strcpy,strcmp,strchr ,strstr,sprintf,strcat 

strcpy

它是字符串拷贝函数,可以这么叫它(string copy)

它有个特点那就是 完全覆盖

下面举个栗子

#include <stdio.h>
#include <string.h>

typedef char Str[20]; 

int main(int argc,char** argv)
{
    Str str1 ;
    Str str2 ;
    memset(str1,0,sizeof(str1));  
    memset(str2,0,sizeof(str2));
    
    printf("str1<===str2\n");//str1将被str2覆盖掉所有
    printf("str1=");
    scanf("%s",str1);
    printf("str2=");
    scanf("%s",str2);
    printf("str1=%s\n",strcpy(str1,str2));
    return 0;
}
           

输出:

str1<===str2
str1=hello
str2=sevenrainbow          
str1=sevenrainbow
           

可见

str2的值全部覆盖到str1中

另外

还有一个它的另一个版本strncpy(string n copy)

顾名思义,它可以指定多少进行覆盖

最后一个参数指的是 覆盖内容的大小(单位:字节)

strncpy(str1,str2,2);
           

这里,指定str2里开头的2个字节大小内容 覆盖掉  str1里开头的2个字节大小内容

输出结果如下:

str1<===str2
str1=12345
str2=ABCD 
str1=AB345
           

strcmp

它是字符串比较函数 ,也可以叫 (string  compare)

很多时候我们都要比较两个字符串

这个时候用这个再好不过了

我们来看下怎么用?

设这两个字符串为str1,str2,

若str1==str2,则返回零;

若str1<str2,则返回负数;

若str1>str2,则返回正数。

两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。

下面举个栗子

#include <stdio.h>
#include <string.h>
typedef char str_20[20];


int main(int argc,char** argv)
{
    str_20 str1,str2;
    scanf("%s",str1);
    scanf("%s",str2);
    if(0==strcmp(str1,str2)){
        printf("相同\n");
        
    }else if(strcmp(str1,str2)){
        printf("str1<str2\n");
    }else{
        printf("str1>str2\n");        
    }
}
           

输出:

sevenRainbow
sevenRainbow
相同

sevenRainbow
sixRainbow
str1<str2

sixRainbow
sevenRainbow
str1<str2
           

strchr

在str里查找到指定chr并返回chr的地址 

常用于关键字匹配、检索等

举个栗子:

#include <stdio.h>
#include <string.h>

int main(int argc,char** argv)
{
        char * p = NULL;   //防止野指针(指针在没有初始化的时候它具有不确定性瞎指)
        if(p = strchr("[email protected]",'@'))
            printf("found : %p  %s \n",p,p+1);
        else
            printf("not found : %p  \n",p);
        return 0;

}
           

解析:

if(p = strchr("[email protected]",'@'))   小括号里赋值是什么情况?

看p最后的值是真是假即可,strchr的返回值是一个指针

放回的指针要么是一个地址(真),要么是NULL(假)。

输出:

found : 0x10094cf83  abc 
           

strstr

在str里查找到指定str1并返回str1的地址 

和strchr不同,它是检索字符串

常用于关键字匹配、检索等

范例:我们常用的播放器软件的工作原理:用检索查看目标文件的格式,然后再用相应的编码器进行解码播放。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main()
{
    int count;
    int jpg_count=0;
    int bmp_count=0;
    char filename[10][30];
    char bmpfile[10][30];  
    char jpgfile[10][30];


    for(count=0;count<10;count++)
    {    
entry:                                 //goto语句  可往前可往后  不能跳到别的函数上  entry可自己命名
        printf(" please input the file[%d]  name :\n",count);
        scanf("%s",filename[count]);
        if(  !(strstr(filename[count],".jpg"))  &&  !(strstr(filename[count],".bmp"))  ){
                printf("请以格式.jpg .bmp重新输入\n");
                goto entry;
        }
    }


    for(count=0;count<10;count++)
    {
        if(strstr(filename[count],".jpg"))    
            {
            printf("jpg  file[%d]  name :  %s \n",count,filename[count]);
            strncpy(jpgfile[jpg_count++],filename[count],sizeof(jpgfile[0]));
        }
       
        
    }    
    
     for(count=0;count<10;count++)
    {
        if(strstr(filename[count],".bmp"))    
        {
            printf("bmp  file[%d]  name :  %s \n",count,filename[count]);
            strncpy(bmpfile[bmp_count++],filename[count],sizeof(jpgfile[0]));
        }
    }   
  
    printf("bmp  file  total :  %d    jpg  file  total :  %d   \n",bmp_count,jpg_count);

    return 0;
}
           

输出:

please input the file[0]  name :
1.jpg
 please input the file[1]  name :
2.bmp
 please input the file[2]  name :
3.jpg
 please input the file[3]  name :
4.jpg
 please input the file[4]  name :
5.jpg
 please input the file[5]  name :
6.bmp
 please input the file[6]  name :
7.jpg
 please input the file[7]  name :
8.jpg
 please input the file[8]  name :
9.jpg
 please input the file[9]  name :
10.jpg
jpg  file[0]  name :  1.jpg 
jpg  file[2]  name :  3.jpg 
jpg  file[3]  name :  4.jpg 
jpg  file[4]  name :  5.jpg 
jpg  file[6]  name :  7.jpg 
jpg  file[7]  name :  8.jpg 
jpg  file[8]  name :  9.jpg 
jpg  file[9]  name :  10.jpg 
bmp  file[1]  name :  2.bmp 
bmp  file[5]  name :  6.bmp 
bmp  file  total :  2    jpg  file  total :  8  
           

sprintf

这个sprintf拥有一颗宽广的包容心❤️

能够包容各种格式(整型、字符、浮点数等)

并将它们重新组织成新的字符串

在实际中,用处大大

举个例子:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main()
{
    char str[100];
    memset(str,0,sizeof(str));  

    int d1 = 1;
    float f1 = 3.1415;
    char c1 = 'h';

    sprintf(str,"int = %d, float = %f , char = %c \n",d1,f1,c1);
    printf("str :  %s \n",str);

    
    return 0;    
}
           

输出:

str :  int = 1, float = 3.141500 , char = h 
           

strcat

将两个str相加   

头接尾

举个例子:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main()
{
    char arry_A[20] = "hello ";
    char arry_B[20] = "gec";
    
    strcat(arry_A,arry_B);
    printf("arry_A = %s \n",arry_A);


    return 0;
}
           

输出:

arry_A = hello gec 
           

整理不易

点赞收藏关注喔~

继续阅读