C语言中常用的字符串处理函数总结

C语言中字符串处理函数备注

此文仅用于自己研究和记录

字符串处理函数

1. char *gets(char *s);

#include<stdio.h>

功能: 从标准输入读入字符,并保存到s指定的内存空间,直到出现换行符或读到文件结尾为止

参数:

  • s:字符串首地址

返回值: 成功:读入的字符串 失败:NULL

gets(str)scanf(“%s”,str)的区别:

  • gets(str)允许输入的字符串含有空格
  • scanf(“%s”,str)不允许含有空格

注意:

由于scanf()gets()无法知道字符串s大小,必须遇到换行符或读到文件结尾为止才接收输入,因此容易导致字符数组越界(缓冲区溢出)的情况。

举例:

printf("请输入str: ");
gets(str);
printf("str = %s\n", str);
2. char *fgets(char *s, intsize, FILE *stream);

#include<stdio.h>

功能:stream指定的文件内读入字符,保存到s所指定的内存空间,直到出现换行字符读到文件结尾或是已读了size - 1个字符为止,最后会自动加上字符 '\0' 作为字符串结束

参数:

  • s:字符串
  • size:指定最大读取字符串的长度(size - 1)
  • stream:文件指针,如果读键盘输入的字符串,固定写为stdin

返回值:

  • 成功:成功读取的字符串
  • 读到文件尾或出错: NULL

描述:

fgets()在读取一个用户通过键盘输入的字符串的时候,同时把用户输入的回车也做为字符串的一部分。通过scanfgets输入一个字符串的时候,不包含结尾的“\n”,但通过fgets结尾多了“\n”。fgets()函数是安全的不存在缓冲区溢出的问题

举例:

charstr[100];
printf("请输入str: ");
fgets(str, sizeof(str), stdin);
printf("str = \"%s\"\n", str);
3. int puts(const char *s);

#include<stdio.h>

功能: 标准设备输出s字符串,在输出完成后自动输出一个'\n'

参数: s:字符串首地址

返回值:

  • 成功:非负数
  • 失败:-1

举例:

# include <stdio.h>

int main()
{
	printf("hello world");
	puts("hello world");

	return 0;
}
4. int fputs(const char *str, FILE *stream);

#include<stdio.h>

功能:str所指定的字符串写入到stream指定的文件中,字符串结束符'\0'不写入文件。

参数:

  • str:字符串
  • stream:文件指针,如果把字符串输出到屏幕,固定写为stdout。

返回值:

  • 成功:0
  • 失败:-1

注意:
fputs()puts()的文件操作版本,但fputs() 不会自动输出一个'\n'

举例:

printf("hello world");
puts("hello world");
fputs("hello world", stdout);
5. size_t strlen(const char *s);

#include<string.h>

功能: 计算指定指定字符串s的长度,不包含字符串结束符‘\0’.

参数: s:字符串首地址

返回值: 字符串s的长度,size_tunsigned int类型

举例:

char str[] = "abcdefg";
int n = strlen(str);
printf("n = %d\n", n);
6. char *strcpy(char *dest, const char *src);

#include<string.h>

功能:src所指向的字符串复制dest所指向的空间中,'\0'也会拷贝过去

参数:

  • dest:目的字符串首地址
  • src:源字符首地址

返回值:

  • 成功:返回dest字符串的首地址
  • 失败:NULL

注意: 如果参数dest所指的内存空间不够大,可能会造成缓冲溢出的错误情况。

举例:

char dest[20] = "123456789";
char src[] = "hello world";
strcpy(dest, src);
printf("%s\n", dest);
7. char *strncpy(char *dest, const char *src, size_tn);

#include<string.h>

功能:src指向字符串前n个字符复制到dest所指向的空间中,是否拷贝结束符看指定的长度是否包含'\0'

参数:

  • dest:目的字符串首地址
  • src:源字符首地址
  • n:指定需要拷贝字符串个数

返回值:

  • 成功:返回dest字符串的首地址
  • 失败:NULL

举例:

char dest[20] ;
char src[] = "hello world";

strncpy(dest, src, 5);
printf("%s\n", dest);

dest[5] = '\0';
printf("%s\n", dest);
8. char *strcat(char *dest, const char *src);

#include<string.h>

功能:src字符串连接到dest尾部‘\0’也会追加过去

参数:

  • dest:目的字符串首地址
  • src:源字符首地址

返回值:

  • 成功:成功:返回dest字符串的首地址
  • 失败:NULL

举例:

char str[20] = "123";
char *src = "hello world";
printf("%s\n", strcat(str, src));
9. char *strncat(char *dest, const char *src, size_tn);

#include<string.h>

功能:src字符串前n个字符连接到dest的尾部,‘\0’也会追加过去

参数:

  • dest:目的字符串首地址
  • src:源字符首地址
  • n:指定需要追加字符串个数

返回值:

  • 成功:成功:返回dest字符串的首地址
  • 失败:NULL

举例:

char str[20] = "123";
char *src = "hello world";
printf("%s\n", strncat(str, src, 5));
10. int strcmp(const char *s1, const char *s2);

#include<string.h>

功能: 比较 s1s2 的大小,比较的是字符ASCII码大小

参数:

  • s1:字符串1首地址
  • s2:字符串2首地址

返回值:

  • 相等:0
  • 大于:>0
  • 小于:<0

举例:

char *str1 = "hello world";
char *str2 = "hello mike";

if (strcmp(str1, str2) == 0)
{
	printf("str1==str2\n");
}
elseif (strcmp(str1, str2) > 0)
{
	printf("str1>str2\n");
}
else
{
	printf("str1<str2\n");
}
11. int strncmp(const char *s1, const char *s2, size_tn);

#include<string.h>

功能: 比较 s1s2n个字符的大小,比较的是字符ASCII码大小

参数:

  • s1:字符串1首地址
  • s2:字符串2首地址
  • n:指定比较字符串的数量

返回值:

  • 相等:0
  • 大于:>0
  • 小于:<0

举例:

char *str1 = "hello world";
char *str2 = "hello mike";

if (strncmp(str1, str2, 5) == 0)
{
	printf("str1==str2\n");
}
elseif (strcmp(str1, "hello world") > 0)
{
	printf("str1>str2\n");
}
else
{
	printf("str1<str2\n");
}
12. int sprintf(char *str , const char *format, ...);

#include<stdio.h>

功能: 根据参数format字符串来转换格式化数据,然后将结果输出str指定的空间中,直到出现字符串结束符 '\0' 为止。

参数:

  • str:字符串首地址
  • format:字符串格式,用法和printf()一样

返回值:

  • 成功:实际格式化的字符个数
  • 失败: - 1

举例:

char dst[100] = { 0 };
int a = 10;
char src[] = "hello world";
printf("a = %d, src = %s", a, src);
printf("\n");
int len = sprintf(dst, "a = %d, src = %s", a, src);
printf("dst = \" %s\"\n", dst);
printf("len = %d\n", len);
13. int sscanf(const char *str, const char *format, ...);

#include<stdio.h>

功能:str指定的字符串读取数据,并根据参数format字符串转换格式化数据。

参数:

  • str:指定的字符串首地址
  • format:字符串格式,用法和scanf()一样

返回值:

  • 成功:参数数目,成功转换的值的个数
  • 失败: - 1

举例:

char src[] = "a=10, b=20";
int a;
int b;
sscanf(src, "a=%d,  b=%d", &a, &b);
printf("a:%d, b:%d\n", a, b);
14. char *strchr(const char *s, intc);

#include<string.h>

功能: 在字符串s查找字母c出现的位置

参数:

  • s:字符串首地址
  • c:匹配字母(字符)

返回值:

  • 成功:返回第一次出现的c地址
  • 失败: NULL

举例:

char src[] = "ddda123abcd";
char *p = strchr(src, 'a');
printf("p = %s\n", p);
15. char *strstr(const char *haystack, const char *needle);

#include<string.h>

功能: 在字符串haystack查找字符串needle出现的位置

参数:

  • haystack:源字符串首地址
  • needle:匹配字符串首地址

返回值:

  • 成功:返回第一次出现的needle地址
  • 失败: NULL

举例:

char src[] = "ddddabcd123abcd333abcd";
char *p = strstr(src, "abcd");
printf("p = %s\n", p);
16. char *strtok(char *str, const char *delim);

#include<string.h>

功能: 来将字符串分割成一个个片段。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时, 则会将该字符改为\0 字符,当连续出现多个时只替换第一个\0

参数:

  • str:指向欲分割的字符串
  • delim:为分割字符串中包含的所有字符

返回值:

  • 成功:分割后字符串首地址
  • 失败: NULL

举例:

char a[100] = "adc*fvcv*ebcy*hghbdfg*casdert";
char *s = strtok(a, "*");//将"*"分割的子串取出
while (s != NULL)
{
	printf("%s\n", s);
	s = strtok(NULL, "*");
}
17. int atoi(const char *nptr);

#include<stdlib.h>

功能: atoi()会扫描 nptr字符串, 跳过前面的 空格字符,直到遇到 数字正负号才开始做 转换,而遇到 非数字字符串结束符('\0')结束转换,并将结果返回 返回值

参数:

  • nptr:待转换的字符串

返回值:

  • 成功:转换后整数

类似的函数有:

  • atof():把一个小数形式的字符串转化为一个浮点数。
  • atol():将一个字符串转化为long类型

举例:

char str1[] = "-10";
int num1 = atoi(str1);
printf("num1 = %d\n", num1);

char str2[] = "0.123";
double num2 = atof(str2);
printf("num2 = %lf\n", num2);
原文地址:https://www.cnblogs.com/During/p/11167948.html