天天看點

C語言-06字元串處理函數

字元串處理函數

字元串操作函數,必須包括的頭檔案,

#include<string.h>

gets()

#include <stdio.h>

int main(int argc,char *argv[]){
    char s[100]={0};
    gets(s);
    //gets認為回車是結束辨別,空格不是輸入結束辨別,是以用gets這個函數就可以實作輸入帶空格的字元串
    //gets同樣和scanf一樣存在緩沖區溢出的問題
    for(int i=0;i<10;i++){
        printf("%d\n",s[i]);
    }
    printf("--------------\n");
    printf("%s\n",s);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
warning: this program uses gets(), which is unsafe.
hello word
104
101
108
108
111
32
119
111
114
100
--------------
hello word

Process finished with exit code 0
           

gets函數不能類似"%s"或者"%d"之類的字元轉義,隻能接收字元串的輸入,get(s)這裡隻能接受字元串s。引入下文的atoi(),字元串的不同類型之間的轉換。

fgets()

#include <stdio.h>

int main(int argc,char *argv[]){
    char s[10]={0};
    fgets(s,10//sizeof(s)-1,stdin);
    //s參數是char的數組,100是數組的大小(機關:位元組),stdin是标準輸入的意思
    printf("%s\n",s);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
0123456789abc
012345678

Process finished with exit code 0

           

這裡輸出的是9位,有一位字元串結束辨別符"\0"。

fgets()是安全的,不會出現緩沖區溢出的問題,調用fgets()的時候,隻要保證第二個參數小于數組的實際大小,那麼久不會出現緩沖區溢出的問題。

puts()列印輸出字元串

#include <stdio.h>

int main(int argc,char *argv[]){
    char s[10]={0};
    fgets(s, sizeof(s)-1,stdin);
    puts(s);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
hello word
hello wo

Process finished with exit code 0
           

puts()函數自動會在輸出完成之後列印一個"\n"出來。

fputs()函數

同上

strlen() 得到字元串長度

#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[]){
    char s[100]="hello world";
    int len = strlen(s);
    //得到字元串的長度,傳回一個字元串中有效字元的數量(不包含字元串中結尾的'\0')
    printf("%d",len);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
11
Process finished with exit code 0
           

strcat()字元串追加

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

int main(int argc,char *argv[]){
    char s[100]="hello world";
    char s1[100]="abc";
    strcat(s,s1);
    //将兩個字元串合并,結果放入第一個參數裡面,starcat也存在字元串緩沖區溢出的問題
    printf("%s",s);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
hello worldabc
Process finished with exit code 0
           

strncat() 有限追加

不會存在字元串緩沖區溢出現象,隻追加到有效位置。

strncat(s1,s2,int n)

合并的時候可以追加限制多少個位元組(int n)。

strcmp() 兩個字元串的比較,是否相等

strcmp(s1,s2,)

在c語言中,字元串的比較中,我們不能使用(s1==s2)來進行判斷兩個字元串是否相等。

strcmp(s1,s2)

它傳回的值是一個int類型的值,我們可以:

if (strcmp(s1,s2)==0)

:如果strcmp的傳回值為0,代表參數的兩個字元串内容是相同的。

它的比較是兩個字元串的ASCII碼進行比較的

如果第一個參數的ASCII碼小于第二個參數的ASCII碼,那麼函數-1

如果相等傳回0

否則傳回1

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

int main(int argc,char *argv[]){
    char s[100]="abc";
    char s1[100]="abc";
    printf("%d\n",strcmp(s,s1));
    return 0;
}
           

相同傳回的是0.

strncmp() 比較指定數量的字元

strncmp(s1,s2,int n)

strncmp(s1,s2,5)

:比較前5個是否相同。

strcpy() 字元串拷貝

strcpy(s1,s2)

将s2的内容拷貝到s1.将原來的内容替換掉。

拷貝的過程中,第一個字元串的大小必須大于等于第二個字元串,要不然就緩沖區溢出。

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

int main(int argc,char *argv[]){
    char s1[100]="abc";
    char s2[100]="123";
    strcpy(s1,s2);
    printf("%s",s1);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
123
Process finished with exit code 0
           

strncpy() 字元串拷貝

strncpy(s1,s2,int n)

将s2的内容拷貝到s1,拷貝的位數由n指定

sprintf() 格式化字元串

1.printf():輸出在螢幕上

2.sprintf():将格式化後的字元串輸出到第一個參數指定的字元串當中去

兩者輸出目标不一樣,sprintf()是把輸出的放在一個字元串中去的。

#include <stdio.h>

int main(int argc,char *argv[]){
    int i=200;
    char s1[100]="abc";
    sprintf(s1,"i=%d",i);
    printf("%s\n",s1);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
i=200

Process finished with exit code 0
           

把一個整數轉換成字元串的功能,可以用fprintf()實作:

#include <stdio.h>

int main(int argc,char *argv[]){
    int i=200;
    char s1[100]="200";
    //itoa()不是标準的C語言庫函數
    //atoi()是标準的C語言庫函數,将字元串轉換成整數
    sprintf(s1,"%d",i);
    printf("%s",s1);

    return 0;
}
           

sscanf()

從某個字元串中接收輸入:

#include <stdio.h>

int main(int argc,char *argv[]){
    char s[100]="abc=100";
    int i=0;
    sscanf(s,"abc=%d",&i);
    printf("%d\n",i);
    return 0;
}
           

它的意思是:把s這個字元串中的100拿出來指派給i。

/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
100

Process finished with exit code 0
           
#include <stdio.h>

int main(int argc,char *argv[]){
    char s[100]="5+6=";
    int a=0;
    int b=0;
    sscanf(s,"%d+%d",&a,&b);
    printf("%d\n",a+b);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
11

Process finished with exit code 0
           

把字元串中的類似于sscanf()中的型拿出來指派上去再運算。scanf()是從鍵盤中讀取使用者輸入,sscanf()是從指定格式化字元串讀取輸入,并得到這個值。轉義字元在這裡替換原字元串。

strchr()查找

字元串中,初始化了以後就不能再用’=‘進行指派或者修改了,要麼用strcpy()

const char *buf=strchr(s,‘o’),傳回的是一個指針。在s這個字元串中間查找第二個參數指定的字元。

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

int main(int argc,char *argv[]){
    char s[100]="hello world";
    const char *buf=strchr(s,'o');
    printf("%s",buf);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
o world
Process finished with exit code 0
           

strstr()

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

int main(int argc,char *argv[]){
    char s[100]="hello world";
    const char *buf=strchr(s,'o');
    buf=strstr(s,"ll");
    printf("%s",buf);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
llo world
Process finished with exit code 0
           

strtok() 分割字元串

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

int main(int argc,char *argv[]){
    char s[100]="abc_123_qwe";
    const char *buf=strtok(s,"_");
    //strtok第一次調用的時候,第一個參數是字元串,但第二次調用的時候,第一個參數是NULL
    printf("%s\n",buf);
    buf=strtok(NULL,"_");
    //strtok第一次調用的時候,第一個參數是字元串,但第二次調用的時候,第一個參數是NULL
    printf("%s\n",buf);
    buf=strtok(NULL,"_");
    //strtok第一次調用的時候,第一個參數是字元串,但第二次調用的時候,第一個參數是NULL
    printf("%s\n",buf);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
abc
123
qwe

Process finished with exit code 0
           

改進:

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

int main(int argc,char *argv[]){
    char s[100]="abc_123_qwe";
    const char *buf=strtok(s,"_");
    while(buf){
        printf("%s\n",buf);
        buf=strtok(NULL,"_");//如果strtok()沒有找到指定的分割符号,那麼傳回NULL,NULL就是0
    }
    return 0;
}

           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
abc
123
qwe

Process finished with exit code 0
           

atoi 轉換成int

頭檔案包含:

#include<stdlib.h>

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

int main(int argc,char *argv[]){
    char s[100]="200";
    int i=atoi(s);//實作字元串向int型的轉換
    printf("i=%d\n",i);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
i=200

Process finished with exit code 0
           

atof 轉換成float

atol轉換成long

整數(正數)轉換成字元串

#include <stdio.h>
#include <string.h>
//整數轉換成字元串
void myitoa(int n,char s[]){
    int i=0;
    while(n){
        int a=n%10;//取出整數的個位
        char c=a+'0';//将整數轉換成字元
        s[i]=c;//将轉換後的char依次放入字元串s中
        i++;
        n/=10;
    }
    //字元串的逆置
    int min=0;
    int max=i-1;
    while (min<max){
        char tmp=s[min];
        s[min]=s[max];
        s[max]=tmp;
        min++;
        max--;
    }
}

int main(){
    int i=45456;
    char s[100]={0};
    myitoa(i,s);
    printf("%s",s);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
45456
Process finished with exit code 0
           

整數(負數)轉換成字元串

#include <stdio.h>
#include <string.h>
//整數轉換成字元串
void myitoa(int n,char s[]){
    int status =0;//0代表正數,1代表負數
    if(n<0){
        status=1;
        n=0-n;//把n轉成正數
    }
    int i=0;
    while(n){
        int a=n%10;//取出整數的個位
        char c=a+'0';//将整數轉換成字元
        s[i]=c;//将轉換後的char依次放入字元串s中
        i++;
        n/=10;
    }
    int min=0;
    int max=i-1;
    while (min<max){//将字元串逆置
        char tmp=s[min];
        s[min]=s[max];
        s[max]=tmp;
        min++;
        max--;
    }
    if(status==1){//如果是負數,那麼将字元串每一位向後移動一位,再将字元的開始位置為負(-)
        int len=0;
        for (len =i-1;len>=0;len--){
            s[len+1]=s[len];
        }
        s[0]='-';
    }
}

int main(){
    int i;
    scanf("%d",&i);
    char s[100]={0};
    myitoa(i,s);
    printf("%s",s);
    return 0;
}
           
/Users/Xx/CLionProjects/Struct1/cmake-build-debug/Struct1
-12333322
-12333322
Process finished with exit code 0
           

負号的添加如圖:

C語言-06字元串處理函數

繼續閱讀