C语言字符串常用函数
strlen()
常用于统计字符串的长度。在这里要注意的是,它会从内存的某个位置开始扫描,直到碰到第一个字符串结束符’\0’为止,然后返回计数器值(长度不包含’\0’),
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(void){
char str[10000] = "Hello,world";
printf("this string has %d characters",strlen(str));
system("pause");
return 0;
}
// output:
// this string has 11 characters
strcat()
用于拼接字符串,该函数接受两个字符串作为参数。该函数把第2个字符串的备份附加在第1个字符串末尾,并把拼接后形成的新字符串作为第1个字符串,第2个字符串不变。strcat()函数的类型是char *(指向char的指针)。strcat()函数返回第一个参数,即拼接第2个字符串后的第1个字符串的地址。
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(void){
char str1[1000] = "Hello";
char str2[1000] = "world";
strcat(str1,str2);
puts(str1);
system("pause");
return 0;
}
// output:
// Helloworld
strncat()
strncat()函数一共接受3个参数,例如如下写法
strncat(str1,str2,13);
的含义为将str2字符串内的内容附加给str1,在加到第13个字符或者空字符时停止。
所以,算上空字符,str1数组应该足够大,用以容纳原始字符串(不包含空字符)、添加原始字符串在后面的13个字符和末尾的空字符。
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(void){
char str1[1000] = "Hello ";
char str2[1000] = "world";
strncat(str1,str2,2);
puts(str1);
system("pause");
return 0;
}
// output:
// Hello wo
strcmp()
用于将用户的响应与已存储的字符串作比较。(注意它比较的对象是字符串,不是字符)
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(void){
char str1[20] = "Hello";
char str2[20] = "Hello";
char str3[20];
if(strcmp(str1,str2) == 0)printf("str1 is same as str2\n");
printf("please input a string to str3:\n");
gets(str3);
if(strcmp(str3,str1) == 0)printf("str3 is same as str1\n");
else if(strcmp(str3,str1) == 1)printf("str3 is different to str1\n");
system("pause");
return 0;
}
// output_1:
/*
str1 is same as str2
please input a string to str3:
hello
str3 is different to str1
*/
// output_2:
/*
str1 is same as str2
please input a string to str3:
Hello
str3 is same as str1
*/
strncmp()
strncmp()函数在比较两个字符串时,可以比较到字符不同的地方,也可以比较第3个参数指定的字符数。
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(void){
char str1[20] = "Hello";
char str2[20] = "Helio";
if(strncmp(str2,str1,3) == 0)printf("Yeah\n");
if(strncmp(str2,str1,4) != 0)printf("emmm\n");
system("pause");
return 0;
}
// output:
/*
Yeah
emmm
*/
strcpy()
用于拷贝字符串。其返回值为char *
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(void){
char str1[20];
char str2[20] = "Hello World.";
strcpy(str1,str2); // 将str2的内容copy到str1中
puts(str1);
system("pause");
return 0;
}
// output:
// Hello World.
strncpy()
函数的第三个参数指明可拷贝的最大字符数。
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(void){
char str1[20];
char str2[20] = "Hello World.";
strncpy(str1,str2,9); // 将str2的内容copy到str1中
puts(str1);
system("pause");
return 0;
}
// output:
// Hello wor
sprintf()
不太冷的知识:该函数声明在stdio.h中,而不是在string.h中
该函数类似printf(),但sprintf()的功能是将数据写入字符串,而不是打印在显示器上。因此,该函数可以将多个元素组合成一个字符串。sprintf()的第一个参数是目标字符串的地址。其余参数和printf()相同,即格式字符串和待写入项的列表。
#include <stdio.h>
#include <windows.h>
int main(void){
char str1[20];
int a = 114514;
sprintf(str1,"Hello %d",a);
puts(str1);
system("pause");
return 0;
}
// output:
// 114514
strstr()
char *strstr(const char * s1,const char * s2);
该函数返回指向s1字符串中s2字符串出现的首位置。如果在s1中没有找到s2,则返回空指针。
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(void){
char str1[20] = "Hello,world";
char str2[20] = "world";
char *str3;
str3 = strstr(str1,str2);
printf("the string is:%s",str3);
system("pause");
return 0;
}
// output:
// the string is:world