IE盒子

搜索
查看: 134|回复: 0

C语言中,常见的处理字符串函数有哪些?

[复制链接]

1

主题

3

帖子

4

积分

新手上路

Rank: 1

积分
4
发表于 2023-1-9 15:42:59 | 显示全部楼层 |阅读模式
常见的处理字符串函数有如下:

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] = "hello";
        char s2[128] = "world";
        strcpy(s1, s2);
        printf("%s", 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] = "";
        char s2[128] = "hel\0lo";
        strncpy(s1, s2, 4);
        int i = 0;
        while (i < 4) {
                printf("[%c ]", 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] = "hello";
        char s2[128] = "world";
        strcat(s1,s2);
        printf("%s\n", 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] = "hello";
        char s2[128] = "world";
        strncat(s1, s2, 3);
        printf("%s\n", 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] = "hello";
        char s2[128] = "world";
        printf("%d\n",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] = "hello";
        char s2[128] = "hello";
        printf("%d\n", strncmp(s1, s2, 5)); // 返回值为0。表示s1 == s2
        return 0;
}总结以上两个比较函数,“strcmp”,“strncmp”。s1,s2两个数组中各拿出一个元素进行比较,相等则继续往后比较,根据的是字符的ASCII值进行比较,如果 "s1> s2"  则返回1,"s1 == s2" 返回0,"s1 < s2" 则返回-1。
7、sprintf函数:
同样的我先定义一个字符数组为c1, 格式为:sprintf(c1,"格式","数据");
作用是将数据按照格式组包,存放在数组c1中,需要注意的是 “sprintf函数”的返回值是组完包的有效长度;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main(){
        int  age = 30;
        char c1[1024] = "";
        int length = sprintf(c1, "age=%d", age);
        printf("length = %d\n", length); // 组完包有效长度为:6
        printf("c1 = [%s]", c1); // c1 = [age=30]
        return 0;
}8、sscanf函数:
同样的我先定义一个字符数组为c1, 格式为:sscanf(c1,"格式",数据);
作用是将c1的内容格式化输出到数据中;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main(){
        int age = 0;
        char  c1[128] = "小华:20";
        sscanf(c1, "小华:%d", &age); //从c1中按照相应的格式获取数据
        printf("小华今年%d岁了!\n", age);
        return 0;
}注意和scanf函数的区别是:scanf("%d",&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[] = "hello";
        char *p = strchr(s1, 't');
        printf("%s\n", 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[] = "helloaabbccworld";
        char s2[] = "cc";
        char *p = strstr(s1, s2);
        printf("%s\n", p); // ccworld
        return 0;
}11、strtok函数:
同样的我先定义两个字符数组分别为s1,s2, 格式为:strtok(s1,"切割的目标");
作用是让在s1中将@切割,返回切割前面的字符串;代码如下所示:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main(){
        char s1[] = "abcdefg#@123456";
        char *p = strtok(s1,"@"); // 在s1中将@切割,返回切割前面的字符串
        printf("%s\n",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[] = "10";
        char a2[] = "22.22";
        // atoi
        int num1 = atoi(a1);
        printf("%d\n", num1); // 10
        // atof
        float num2 = atof(a2);
        printf("%0.2f\n", num2); // 22.22
        // atol
        long num3 = atol(a1);
        printf("%ld\n", num3); // 10
        // atoll
        long long num4 = atoll(a1);
        printf("%lld\n", num4); // 10
        return 0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表