|
常见的处理字符串函数有如下:
1、strcpy函数:
先定义两个字符数组,分别代表s1和s2,则:strcpy(s1,s2);
作用是将s2的字符拷贝至s1数组当中,有个注意的地方是s2遇到\0会结束,同时会将\0拷贝进s1中;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[128] = &#34;hello&#34;;
char s2[128] = &#34;world&#34;;
strcpy(s1, s2);
printf(&#34;%s&#34;, s1); // 结果为:world
return 0;
}2、strncpy函数:
同样的定义两个字符数组,strncpy(s1,s2,n);
作用是将s2中的前n个字符拷贝至s1中,如果拷贝过程中不足n个,遇到\0拷贝则结束。代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[128] = &#34;&#34;;
char s2[128] = &#34;hel\0lo&#34;;
strncpy(s1, s2, 4);
int i = 0;
while (i < 4) {
printf(&#34;[%c ]&#34;, s1); // [h ][e ][l ][ ]
i++;
}
return 0;
}3、strcat函数:
同样的定义两个字符数组,strcat(s1,s2);
是将s2字符数组中的字符连接到s1字符数组中,拼接字符串的作用,一样的遇到\0则结束;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[128] = &#34;hello&#34;;
char s2[128] = &#34;world&#34;;
strcat(s1,s2);
printf(&#34;%s\n&#34;, s1); // helloworld
return 0;
}4、strncat函数:
同样的定义两个字符数组,strncat(s1,s2,n);
是将s2字符数组中的n个字符连接到s1字符数组中,拼接字符串的作用,一样的遇到\0则结束。代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[128] = &#34;hello&#34;;
char s2[128] = &#34;world&#34;;
strncat(s1, s2, 3);
printf(&#34;%s\n&#34;, s1); // hellowor
return 0;
}5、strcmp函数:
同样的定义两个字符数组,strcmp(s1,s2);
为比较函数,两个字符比较时,如果遇到\0则结束比较;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[128] = &#34;hello&#34;;
char s2[128] = &#34;world&#34;;
printf(&#34;%d\n&#34;,strcmp(s1,s2)); // 返回值为-1。表示s1 < s2
return 0;
}6、strncmp函数:
同样的定义两个字符数组,strcmp(s1,s2,n);
为比较函数,两个字符比较时,如果遇到\0则结束比较;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[128] = &#34;hello&#34;;
char s2[128] = &#34;hello&#34;;
printf(&#34;%d\n&#34;, strncmp(s1, s2, 5)); // 返回值为0。表示s1 == s2
return 0;
}总结以上两个比较函数,“strcmp”,“strncmp”。s1,s2两个数组中各拿出一个元素进行比较,相等则继续往后比较,根据的是字符的ASCII值进行比较,如果 &#34;s1> s2&#34; 则返回1,&#34;s1 == s2&#34; 返回0,&#34;s1 < s2&#34; 则返回-1。
7、sprintf函数:
同样的我先定义一个字符数组为c1, 格式为:sprintf(c1,&#34;格式&#34;,&#34;数据&#34;);
作用是将数据按照格式组包,存放在数组c1中,需要注意的是 “sprintf函数”的返回值是组完包的有效长度;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
int age = 30;
char c1[1024] = &#34;&#34;;
int length = sprintf(c1, &#34;age=%d&#34;, age);
printf(&#34;length = %d\n&#34;, length); // 组完包有效长度为:6
printf(&#34;c1 = [%s]&#34;, c1); // c1 = [age=30]
return 0;
}8、sscanf函数:
同样的我先定义一个字符数组为c1, 格式为:sscanf(c1,&#34;格式&#34;,数据);
作用是将c1的内容格式化输出到数据中;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
int age = 0;
char c1[128] = &#34;小华:20&#34;;
sscanf(c1, &#34;小华:%d&#34;, &age); //从c1中按照相应的格式获取数据
printf(&#34;小华今年%d岁了!\n&#34;, age);
return 0;
}注意和scanf函数的区别是:scanf(&#34;%d&#34;,&age); 是从键盘按照相应的格式获取数据;
9、strchr函数:
同样的我先定义一个字符数组为s1, 格式为:strchr(s1,ch);
作用让在s1字符数组中查找字符ch出现的位置,如果成功返回此字符出现位置的地址,如果没有找到,则就返回NULL。代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[] = &#34;hello&#34;;
char *p = strchr(s1, &#39;t&#39;);
printf(&#34;%s\n&#34;, p); // 没找到,返回(null)
return 0;
}如图结果所示:

10、strstr函数:
同样的我先定义两个字符数组分别为s1,s2, 格式为:strstr(s1,s2);
作用让在s1字符数组中查找s2字符串出现的位置,并且返回这个位置的地址。代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[] = &#34;helloaabbccworld&#34;;
char s2[] = &#34;cc&#34;;
char *p = strstr(s1, s2);
printf(&#34;%s\n&#34;, p); // ccworld
return 0;
}11、strtok函数:
同样的我先定义两个字符数组分别为s1,s2, 格式为:strtok(s1,&#34;切割的目标&#34;);
作用是让在s1中将@切割,返回切割前面的字符串;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(){
char s1[] = &#34;abcdefg#@123456&#34;;
char *p = strtok(s1,&#34;@&#34;); // 在s1中将@切割,返回切割前面的字符串
printf(&#34;%s\n&#34;,p); // abcdefg#
return 0;
}12、atoi,atof,atol,atoll等函数:
同样的我先定义两个字符数组分别为a1,a2; 函数的作用分别是:
(1)、atoi函数:可以将字符串转整数;
(2)、atof函数:可以将字符串转float类型的数据;
(3)、atol函数:可以将字符串转long类型的数据;
(4)、atoll函数:可以将字符串转long long类型的数据。
代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char a1[] = &#34;10&#34;;
char a2[] = &#34;22.22&#34;;
// atoi
int num1 = atoi(a1);
printf(&#34;%d\n&#34;, num1); // 10
// atof
float num2 = atof(a2);
printf(&#34;%0.2f\n&#34;, num2); // 22.22
// atol
long num3 = atol(a1);
printf(&#34;%ld\n&#34;, num3); // 10
// atoll
long long num4 = atoll(a1);
printf(&#34;%lld\n&#34;, num4); // 10
return 0;
} |
|