字符串操作庫函數(shù)
1、strcpy
//把字符串str2(包括'\0')拷貝到字符串str1當(dāng)中杠河,并返回str1碌尔。
char *strcpy(char *str1, const char *str2);
#include <stdio.h>
#include <string.h>
int main()
{
char *res;
char s1[20];
char *s2 = "slander";
res = strcpy(s1, s2);
printf("res = %s", res);
return 0;
}
// res = slander
2、strncpy
char *strncpy(char *str1, const char *str2, size_t count);
把字符串str2中最多count個字符拷貝到字符串str1中券敌,并返回str1唾戚。如果str2中少于count個字符,那么就用'\0'來填充待诅,直到滿足count個字符為止叹坦。
#include <stdio.h>
#include <string.h>
int main()
{
char *res;
char s1[20];
char *s2 = "slander";
res = strncpy(s1, s2, 3);
printf("res = %s", res);
return 0;
}
// res = sla
3、strcat
char *strcat(char *str1, const char *str2);
把str2(包括'\0')拷貝到str1的尾部(連接)卑雁,并返回str1募书。其中終止原str1的'\0'被str2的第一個字符覆蓋。
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Jack ";
char *s2 = "scores";
strcat(s1, s2);
printf("s1 = %s", s1);
return 0;
}
//s1 = Jack scores
4测蹲、strncat
char *strncat(char *str1, const char *str2, size_t count);
把str2中最多count個字符連接到str1的尾部莹捡,并以'\0'終止str1,返回str1扣甲。其中終止原str1的'\0'被str2的第一個字符覆蓋篮赢。
注意,最大拷貝字符數(shù)是count+1琉挖。
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "Jack ";
char *s2 = "scores";
strncat(s1, s2, 3);
printf("s1 = %s", s1);
return 0;
}
//s1 = Jack sco
5启泣、strcmp
int strcmp(const char *str1, const char *str2);
按字典順序比較兩個字符串,返回整數(shù)值的意義如下:
小于0示辈,str1小于str2寥茫;
等于0,str1等于str2矾麻;
大于0纱耻,str1大于str2芭梯;
#include <stdio.h>
#include <string.h>
int main()
{
int res;
char *s1 = "Jack";
char *s2 = "Jacm";
res = strcmp(s1, s2);
printf("res = %d", res);
return 0;
}
// res = -1
6、strncmp
int strncmp(const char *str1, const char *str2, size_t count);
同strcmp膝迎,除了最多比較count個字符粥帚。根據(jù)比較結(jié)果返回的整數(shù)值如下:
小于0,str1小于str2限次;
等于0,str1等于str2柴灯;
大于0卖漫,str1大于str2;
#include <stdio.h>
#include <string.h>
int main()
{
int res;
char *s1 = "Jack";
char *s2 = "Jacm";
res = strncmp(s1, s2, 3);
printf("res = %d", res);
return 0;
}
// res = 0
7赠群、strchr
char *strchr(const char *str, int ch);
返回指向字符串str中字符ch第一次出現(xiàn)的位置的指針羊始,如果str中不包含ch,則返回NULL查描。
#include <stdio.h>
#include <string.h>
int main()
{
char *res;
char *s1 = "Jacak";
char c = 'a';
res = strchr(s1, c);
printf("res = %s", res);
return 0;
}
// res = acak
8突委、strrchr
char *strrchr(const char *str, int ch);
返回指向字符串str中字符ch最后一次出現(xiàn)的位置的指針,如果str中不包含ch冬三,則返回NULL匀油。
#include <stdio.h>
#include <string.h>
int main()
{
char *res;
char *s1 = "Jacak";
char c = 'a';
res = strrchr(s1, c);
printf("res = %s", res);
return 0;
}
// res = ak
9、strspn
size_t strspn(const char *str1, const char *str2);
返回字符串str1中由字符串str2中字符構(gòu)成的第一個子串的長度勾笆。
str1中的字符從index = 0開始在str2中查找敌蚜,直到查不到結(jié)束
// 函數(shù)原型
int strspn(const char *s,const char *accept)
{
const char *p;
const char *a;
int count = 0;
for(p = s; *p != '\0'; ++p)
{
for (a = accept; *a != '\0'; ++a)
{
if (*p == *a)
break;
}
if (*a == '\0')
return count;
++count;
}
return count;
}
#include <stdio.h>
#include <string.h>
int main()
{
int res;
char *s1 = "I am a student";
char *s2 = "I am not a teacher";
res = strspn(s1, s2);
printf("res = %d", res);
return 0;
}
// res = 7
10、 strcspn
size_t strcspn(const char *str1, const char *str2);
返回字符串str1中由不在字符串str2中字符構(gòu)成的第一個子串的長度窝爪。
#include <stdio.h>
#include <string.h>
int main()
{
int res;
char *s1 = "I am a student";
char *s2 = "Jack am not a teacher";
res = strcspn(s1, s2);
printf("res = %d", res);
return 0;
}
//res = 1
11弛车、 strpbrk
char *strpbrk(const char *str1, const char *str2);
返回指向字符串str2中的任意字符第一次出現(xiàn)在字符串str1中的位置的指針;如果str1中沒有與str2相同的字符蒲每,那么返回NULL纷跛。
#include <stdio.h>
#include <string.h>
int main()
{
char* res;
char *s1 = "I am a student";
char *s2 = "Jack am not a teacher";
res = strpbrk(s1, s2);
printf("res = %s", res);
return 0;
}
//res = am a student
12、 strstr
char *strstr(const char *str1, const char *str2);
返回指向字符串str2第一次出現(xiàn)在字符串str1中的位置的指針邀杏;如果str1中不包含str2贫奠,則返回NULL。
#include <stdio.h>
#include <string.h>
int main()
{
char* res;
char *s1 = "I am a student";
char *s2 = "am";
res = strstr(s1, s2);
printf("res = %s", res);
return 0;
}
//res = am a student
13淮阐、strlen
size_t strlen(const char *str);
返回字符串str的長度叮阅,'\0'不算在內(nèi)。
#include <stdio.h>
#include <string.h>
int main()
{
int res;
char *s1 = "I am a student";
res = strlen(s1);
printf("res = %d", res);
return 0;
}
// res = 14
14泣特、strtok
char *strtok(char *str1, const char *str2);
在str1中搜索由str2中的分界符界定的單詞浩姥。
對strtok()的一系列調(diào)用將把字符串str1分成許多單詞,這些單詞以str2中的字符為分界符状您。第一次調(diào)用時str1非空勒叠,它搜索str1兜挨,找出由非str2中的字符組成的第一個單詞,將str1中的下一個字符替換為'\0'眯分,并返回指向單詞的指針拌汇。隨后的每次strtok()調(diào)用(參數(shù)str1用NULL代替),均從前一次結(jié)束的位置之后開始弊决,返回下一個由非str2中的字符組成的單詞噪舀。當(dāng)str1中沒有這樣的單詞時返回NULL。每次調(diào)用時字符串str2可以不同飘诗。
#include <string.h>
#include <stdio.h>
int main () {
char str[80] = "This is - www.runoob.com - website";
const char s[2] = "-";
char *token;
/* 獲取第一個子字符串 */
token = strtok(str, s);
/* 繼續(xù)獲取其他的子字符串 */
while( token != NULL ) {
printf( "%s\n", token );
token = strtok(NULL, s);
}
return(0);
}
// This is
www.runoob.com
website
參考
https://www.cnblogs.com/kerwincui/p/14278444.html