C语言中处理字符串的函数

在C语言中有许多字符串处理函数,在写程序的时候常常想不起来或则不了解区别,今天就花点时间将这些函数做个总结,以便以后查找使用。

一、输入函数

  • scanf 与 scanf_s 函数
char buf[10] = { 0 };
scanf("%s", buf);//输入:tttt3
printf("buf = %s\n", buf);//结果:buf = tttt3
char buf1[10] = {0};
scanf_s("%s", buf1, 10);//输入:yyyy355
printf("buf1 = %s\n", buf1);//结果:buf1 = yyyy355

当使用scanf 函数来读取字符串的时候需要注意两点:
① scanf 函数如果输入的字符数大于定义的字符数组长度就会出现缓冲区溢出。
② 当输入的字符串中包含空格时,scanf 和 scanf_s 函数只会接收空格前的字符串。例如:hello world 则只会接收到:hello
③ scanf_s 函数最后一个参数代表缓冲区的大小,示例中缓冲区大小为10,但最多能放入9个字符,因为最后一个需要放’\0’

  • gets 与 gets_s 函数
char buf[10] = {0};
gets(buf);
gets_s(buf,10);
printf("buf = %s\n",buf);

① gets 函数解决了scanf 和 scanf_s 不能输入空格的问题,但是没有解决缓冲区溢出的问题。
② gets 函数由于也不安全所以被 gets_s 函数代替,该函数的后一个参数代表缓冲区大小。

  • fgets 函数
char buf[10] = { 0 };
fgets(buf, sizeof(buf), stdin);//输入:devbird
printf("buf = %s\n", buf);//输出:buf = devbird

① fgets 改进了 gets 函数缓冲区溢出的问题,fgets 函数是为读取文件设计的,
所以读取键盘时没有 gets 那么方便。
② fgets 函数是安全函数,所以程序中使用此函数更安全。


二、输出函数

  • printf 与 printf_s 函数
char* str="hello world";
printf("%s\n",str);
printf_s("%s\n",str);

这两个函数都是输出函数,区别在于 printf_s 函数会检查前面一个参数的格式化字符是否合法。

  • puts 与 fputs 函数
char* str="hello world";
puts(str);
fputs(str,stdout);

① puts 函数与 printf 不同,会自动在末尾添加一个’\n’
② fputs 函数和 fgets 函数一样也是安全的函数,在程序中使用更安全。


三、字符串处理函数

  • strlen 函数
char* str = "12345";
int len = strlen(str);
printf("len = %d\n", len);

该函数是计算字符串长度的函数,但不包含字符串结尾的’\0’

  • strcat 与 strncat 函数
char str1[10] = {'1','2','3'};
char str2[5] = {'x','y','z','w'};
strcat(str1, str2);//把str2追加到str1的后面
strncat(str1, str2, sizeof(str1) – strlen(str1) - 1);//把str2追加到str1的后面,最后一个参数是str1剩余的长度。
printf("string = %s\n", str1);

① 这两个函数都是拼接字符串的函数。
② str1一定要有足够的空间来放str2,不然会内存溢出。
③ strncat 函数比 strcat 函数更安全。

  • strcmp 与 strncmp 函数
char* str1 = "hello";
char* str2 = "hellojj";
int result1 = strcmp(str1, str2);//比较两个字符串
printf("result1 = %d\n", result1);
int result2 = strncmp(str1, str2, 5);//最后一个参数代表,比较两个字符串的前n个
printf("result2 = %d\n", result2);

① strcmp 函数,如果两个字符串相同,则返回0,否则为非0
② strncmp 函数,最后一个参数代表比较的前n个字符,如果两个数前n个相同则返回0,否则为非0

  • strcpy 与 strncpy 函数
char strBuf[20] = {0};
char* str = "hello devbird";
strcpy(strBuf, str);//将str中的字符串拷贝到strBuf中
printf("strBuf = %s\n", strBuf);
strncpy(strBuf, str, 8);//将str中的前8个字符拷贝到strBuf中
printf("strBuf = %s\n", strBuf);

① 两个函数都是拷贝字符串函数,有缓冲区溢出危险。
② strncpy 最后一个参数代表要拷贝的字符数。

  • sprintf 函数
char buf[50];
sprintf(buf, "我有%d块钱!", 100);
printf("buf = %s\n", buf);//输出结果:buf = 我有100块钱!

① printf是向屏幕输出一个字符串。
② sprintf是向char数组输出一个字符串,其他行为和printf一模一样。
③ sprintf也存在缓冲区溢出的问题。

  • strchr 函数
char* str = "devbird";
char* result = strchr(str, 'b');
printf("result = %s\n", result);//结果:result = bird
char* res = strchr(str, 'z');
printf("res = %s\n", res);//结果:res = (null)

该函数是查找字符的函数,如果能找到则返回当前字符及其后面的字符,否则返回为null

  • strstr 函数
char* str = "I am a Android developer!";
char* subStr = "Android";
char* result = strstr(str, subStr);
printf("result = %s\n", result);//结果:result = Android developer!

该函数是查找子字符串,返回值为子字符串第一次出现的位置及其后面的字符串,如果没查找到则返回为NULL

  • strtok 函数
char str[] = "2016/08/29 20:06:55";
char* result = strtok(str, "/");
while (result)
{
    printf("result = %s\n", result);//结果:result = 2016
                                    //     result = 08
                                    //     result = 29 20:06:55
    result = strtok(NULL, "/");
}

字符在第一次调用时strtok()必需给予参数str字符串,往后的调用则将参数str设置成NULL,每次调用成功则返回指向被分割出片段的指针。

  • atoi 函数
char* str = "123";
int i = atoi(str);
printf("i = %d\n", i);//输出:i = 123

该函数是一个将数字字符串转换成int类型的函数。

  • atof 函数
char* str = "29.3547";
float f = atof(str);
printf("f = %f\n", f);//输出:f = 29.354700

该函数是将一个数字字符串转换成float类型的函数。

  • atol 函数
char* str = "31465666";
long l = atof(str);
printf("ld = %ld\n", l);//输出:ld = 31465666

该函数是将数字字符串转换成长整型的函数。


四、随机数函数

  • rand 与 srand函数
int t = (int)time(NULL);
srand(t);
for (int i = 0; i < 3; i++)
{
    printf("randNum = %d\n", rand());
    //输出:randNum = 14824
    //     randNum = 16692
    //     randNum = 15958
}

① 使用该函数需要包含头文件:#include <time.h>
② 通过time函数生成一个不同的int数。

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值