字符处理函数总结大全
gets()、fgets()
#include <stdio.h>
/************************
#include <stdio.h>
char *gets(char *s);
功能:从标准输入读入字符,并保存到s指定的内存空间,直到出现换行符或读到文件结尾为止。
参数:
s:字符串首地址
返回值:
成功:读入的字符串
失败:NULL
************************/
int main(int argc, char const *argv[])
{
//scanf();不能读取空格
//gets(); 能读取空格,从键盘读取字符串,放在指定数组中
//同样都是不做越界检查,此函数不安全
char buf[100];
gets(buf);
printf("buf = %s\n", buf);
return 0;
}
#include <stdio.h>
/*************************************
#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);
功能:从stream指定的文件内读入字符,保存到s所指定的内存空间,直到出现换行字符、读到文件结尾或是已读了size - 1个字符为止,最后会自动加上字符 '\0' 作为字符串结束。
参数:
s:字符串
size:指定最大读取字符串的长度(size - 1)
stream:文件指针,如果读键盘输入的字符串,固定写为stdin
返回值:
成功:成功读取的字符串
读到文件尾或出错: NULL
*************************************/
int main(int argc, char const *argv[])
{
char buf[10];
//从stdin(代表标准输入,键盘),读取内容
//输入内容如果大于sizeof(buf) - 1,只读取sizeof(buf) - 1,放在buf所在的数组
fgets(buf, sizeof(buf), stdin);
//注意:会把换行符也读进去
printf("buf = %s\n", buf);
return 0;
}
puts(),fput()
#include <stdio.h>
/****************************
#include <stdio.h>
int puts(const char *s);
功能:标准设备输出s字符串,在输出完成后自动输出一个'\n'。
参数:
s:字符串首地址
返回值:
成功:非负数
失败:-1
*****************************/
int main(int argc, char const *argv[])
{
char buf[] = "hello";
printf("%s", buf); //没有加换行
puts(buf);//把buf内容输出到屏幕,自动在屏幕加换行,是在屏幕加,字符串本身没有变量
return 0;
}
/*
输出需要注意的一些点:
char buf[100] = "aaaaaaaaaaaaaaaaaaaaa";
scanf("%s", buf);
//这时候字符将会以这样的形式放置到数组
键盘:hello回车
char buf[100] = "hello\0aaaaaaaaaaaaaaaaaaaaa"
printf("buf = %s\n", buf);
*/
#include <stdio.h>
/*************************************
#include <stdio.h>
int fputs(const char * str, FILE * stream);
功能:将str所指定的字符串写入到stream指定的文件中, 字符串结束符 '\0' 不写入文件。
参数:
str:字符串
stream:文件指针,如果把字符串输出到屏幕,固定写为stdout
返回值:
成功:0
失败:-1
**************************************/
int main(int argc, char const *argv[])
{
char buf[] = "hello";
printf("hello world");
//往stdout(代表屏幕,标准输出)输出buf的内容
fputs(buf, stdout);
return 0;
}
sscanf()、sprintf()
#include <stdio.h>
/*
#include <stdio.h>
int sscanf(const char *str, const char *format, ...);
功能:从str指定的字符串读取数据,并根据参数format字符串来转换并格式化数据。
参数:
str:指定的字符串首地址
format:字符串格式,用法和scanf()一样
返回值:
成功:参数数目,成功转换的值的个数
失败: - 1
*/
int main(int argc, char const *argv[])
{
char buf[] = "1 2 3";
int a, b, c;
//从buf中以指定的格式提取内容
sscanf(buf, "%d %d %d", &a, &b, &c);
printf("a = %d b = %d c = %d\n", a, b, c);
//提取整形变量是最方便的
char str[] = "a = 1, b = 2, c = 3";
a = 0;
b = 0;
c = 0;
sscanf(str, "a = %d, b = %d, c = %d", &a, &b, &c);
printf("a1 = %d b1 = %d c1 = %d\n", a, b, c);
char str2[] = "1, 2, 3";
a = 0;
b = 0;
c = 0;
sscanf(str, "%d, %d, %d", &a, &b, &c);
printf("a2 = %d b2 = %d c2 = %d\n", a, b, c);
//提取字符串,默认以空格分隔,可以提取
char tmp[] = "abc mike 250";
char m[10], n[10], k[10];
sscanf(tmp, "%s, %s, %s", m, n, k);//不用&,数组名就是首元素地址
printf("m = %s, n = %s, k = %s\n", m, n, k);
char tmp[] = "abc,mike,250";
char m1[10], n1[10], k1[10];
sscanf(tmp1, "%s, %s, %s", m1, n1, k1);//不用&,数组名就是首元素地址
printf("m1 = %s, n1 = %s, k1 = %s\n", m, n, k);
//,不能分隔字符串
return 0;
}
int main01(int argc, char const *argv[])
{
int a,b,c;
printf("请输入a,b,c:");
//从键盘中以指定的格式提取内容
scanf("%d %d %d", &a, &b, &c);//默认以空格标识
printf("a = %d b = %d c = %d\n", a, b, c);
return 0;
}
#include <stdio.h>
/**************************************************
#include <stdio.h>
int sprintf(char *str, const char *format, ...);
功能:根据参数format字符串来转换并格式化数据,然后将结果输出到str指定的空间中,直到出现字符串结束符 '\0' 为止。
参数:
str:字符串首地址
format:字符串格式,用法和printf()一样
返回值:
成功:实际格式化的字符个数
失败: - 1
***************************************************/
int main(int argc, char const *argv[])
{
int a = 10;
char ch = 'd';
char buf[] = "hello";
//格式化一个字符串,把这个字符串输出到屏幕
printf("a = %d, ch = %c, buf =%s\n", a, ch, buf);
char dst[100];
//格式化一个字符串,把这个字符串输出到(保存在)指定的数组
printf(dst, "a = %d, ch = %c, buf =%s\n", a, ch, buf);
printf("@@%s@@", dst);
return 0;
}
strlen()测字符串长度
#include <stdio.h>
/**************************************************
#include <stdio.h>
int sprintf(char *str, const char *format, ...);
功能:根据参数format字符串来转换并格式化数据,然后将结果输出到str指定的空间中,直到出现字符串结束符 '\0' 为止。
参数:
str:字符串首地址
format:字符串格式,用法和printf()一样
返回值:
成功:实际格式化的字符个数
失败: - 1
***************************************************/
int main(int argc, char const *argv[])
{
int a = 10;
char ch = 'd';
char buf[] = "hello";
//格式化一个字符串,把这个字符串输出到屏幕
printf("a = %d, ch = %c, buf =%s\n", a, ch, buf);
char dst[100];
//格式化一个字符串,把这个字符串输出到(保存在)指定的数组
printf(dst, "a = %d, ch = %c, buf =%s\n", a, ch, buf);
printf("@@%s@@", dst);
return 0;
}
strcpy()字符串复制
#include <stdio.h>
/********************************************
#include <string.h>
char *strcpy(char *dest, const char *src);
功能:把src所指向的字符串复制到dest所指向的空间中,'\0'也会拷贝过去
参数:
dest:目的字符串首地址
src:源字符首地址
返回值:
成功:返回dest字符串的首地址
失败:NULL
*********************************************/
/*******************************************
char src[100] = "hello mike";//源字符串
char dst[100];//目的
//功能:把src字符数组的内容拷贝给dst所代表的的数组
strcpy(dst, src);
printf("dst = %s\n", dst);
*********************************************/
int main(int argc, char const *argv[])
{
char src[100] = "hello\0 mike";//源字符串
char dst[100];//目的
//功能:把src字符数组前12个字符的内容拷贝给dst所代表的的数组
//可以把'\0'拷贝过去,但是'\0'的内容无法拷贝
strnpy(dst, src, strlen(src));
printf("dst = %s\n", dst);
//打印:hello
return 0;
}
int main02(int argc, char const *argv[])
{
char src[100] = "hello mike";//源字符串
char dst[100] = "aaaaaaaaaaaaaaaaaaaa";//目的
//功能:把src字符数组前12个字符的内容拷贝给dst所代表的的数组
//strnpy(dst, src, strlen(src)+1);//把结束符'\0'也拷贝进去
strnpy(dst, src, strlen(src));//不把结束符'\0'拷贝进去
printf("dst = %s\n", dst);
//打印:hello mike
printf("dst = %s\n", dst);
//打印:hello mikeaaaaaaaaaaaaa
return 0;
}
int main01(int argc, char const *argv[])
{
char src[100] = "hello mike";//源字符串
char dst[100];//目的
//功能:把src字符数组的内容拷贝给dst所代表的的数组
strcpy(dst, src);
printf("dst = %s\n", dst);
//打印:hello mike
char src2[100] = "hello\0 mike";//源字符串
char dst2[100];//目的
//功能:把src字符数组的内容拷贝给dst所代表的的数组
//拷贝原理,从首元素开始,遇到结束符'\0'结束
strcpy(dst2, src2);
printf("dst = %s\n", dst);
//打印:hello
return 0;
}
strcmp() strncmp()字符串比较
#include <stdio.h>
/******************************************
#include <string.h>
int strcmp(const char *s1, const char *s2);
功能:比较 s1 和 s2 的大小,比较的是字符ASCII码大小。
参数:
s1:字符串1首地址
s2:字符串2首地址
返回值:
相等:0
大于:>0
小于:<0
*******************************************
#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n);
功能:比较 s1 和 s2 前n个字符的大小,比较的是字符ASCII码大小。
参数:
s1:字符串1首地址
s2:字符串2首地址
n:指定比较字符串的数量
返回值:
相等:0
大于: > 0
小于: < 0
*******************************************/
//例如:abc 与 abcd比较 那么abcd > abc
// abc 与 aBc 比较 那么abc > aBc
//因为比较的是每个字符的ascii码,不是他们的字符串长度
int main(int argc, char *argv[])
{
char s1[] = "abc";
char s2[] = "Abcd";
//int flag = strcmp(s1, s2);
int flag = strncmp(s1, s2, 3); //指定比较前3个字符
if(flag > 0)
{
printf("[%s] > [%s]\n", s1, s2);
}
else if(flag < 0)
{
printf("[%s] < [%s]\n", s1, s2);
}
else
{
printf("[%s] == [%s]\n", s1, s2);
}
return 0;
}
int main01(int argc, char *argv[])
{
char s1[] = "abc";
char s2[] = "abcd";
//int flag = strcmp(s1, s2);
int flag = strncmp(s1, s2, 3); //指定比较前3个字符
if(flag > 0)
{
printf("[%s] > [%s]\n", s1, s2);
}
else if(flag < 0)
{
printf("[%s] < [%s]\n", s1, s2);
}
else
{
printf("[%s] == [%s]\n", s1, s2);
}
return 0;
}
strcat()、strncat()字符串追加
#include <stdio.h>
/************************************************
#include <string.h>
char *strcat(char *dest, const char *src);
功能:将src字符串连接到dest的尾部,‘\0’也会追加过去
参数:
dest:目的字符串首地址
src:源字符首地址
返回值:
成功:返回dest字符串的首地址
失败:NULL
************************************************
#include <string.h>
char *strncat(char *dest, const char *src, size_t n);
功能:将src字符串前n个字符连接到dest的尾部,‘\0’也会追加过去
参数:
dest:目的字符串首地址
src:源字符首地址
n:指定需要追加字符串个数
返回值:
成功:返回dest字符串的首地址
失败:NULL
*************************************************/
int main(int argc, char const *argv[])
{
char src[] = " hello mike";
char dst[100] = "abc";
//把src的内容追加到dst的后面
strcat(dst, src);//dst = "abc hello mike"
//指定追加长度
strncat(dst, src, strlen("hello"));
//把src的内容覆盖到dst
strcpy(dst, src);
printf("dst = %s\n", dst);
return 0;
}
strchr()、strstr()字符串查询
#include <stdio.h>
#include <string.h>
/******************************************
#include <string.h>
char *strchr(const char *s, int c);
功能:在字符串s中查找字母c出现的位置
参数:
s:字符串首地址
c:匹配字母(字符)
返回值:
成功:返回第一次出现的c地址
失败:NULL
*******************************************
#include <string.h>
char *strstr(const char *haystack, const char *needle);
功能:在字符串haystack中查找字符串needle出现的位置
参数:
haystack:源字符串首地址
needle:匹配字符串首地址
返回值:
成功:返回第一次出现的needle地址
失败:NULL
*******************************************/
int main(int argc, char const *argv[])
{
char buf[] = "abcdefg";
//在buf中查询字符串"cde",如果找到,返回匹配字符串所在位置的地址
//如果查询失败,返回NULL
char *p = strchr(buf, "cde ");
if(p == NULL)
{
printf("查询失败\n");
}
else
{
printf("p = %s\n", p);
}
return 0;
}
int main01(int argc, char const *argv[])
{
char buf[] = "abcdefg";
//在buf中查询字符d,如果找到,返回d所在位置的地址
//如果查询失败,返回NULL
char *p = strchr(buf, 'd');
if(p == NULL)
{
printf("查询失败\n");
}
else
{
printf("p = %s\n", p);
}
return 0;
}
strtok()字符串切割
#include <stdio.h>
/*
#include <string.h>
char *strtok(char *str, const char *delim);
功能:来将字符串分割成一个个片段。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时, 则会将该字符改为\0 字符,当连续出现多个时只替换第一个为\0。
参数:
str:指向欲分割的字符串
delim:为分割字符串中包含的所有字符
返回值:
成功:分割后字符串首地址
失败:NULL
*/
int main(int argc, char const *argv[])
{
char buf[] = "abc,mike,jiang,250";
char tmp[100];
strcpy(tmp, buf);//把buf的内容拷贝到tmp数组,后面使用tmp
char *p;
//第一次调用
p = strtok(tmp, ",");//说明切割成功
while(p != NULL)
{
printf("p = %s\n", p);
//第二次起,第一个参数需要写NULL
p = strtok(NULL, ",");
}
return 0;
}
int main01(int argc, char const *argv[])
{
char buf[] = "abc,mike,jiang,250";
char tmp[100];
//第一个调用
//第一个参数写源字符串,第二参数写切割字符
//返回值就是切割后的字符串
//在匹配切割字符的地方,换成结束符
//使用strtok()会破坏原来字符串的结构
printf("before,buf[3] = %d\n", buf[3]);//打印出44,是一个,字符
char *p = strtok(buf, ",");
printf("p = %s\n", p);
printf("buf = %s\n", buf);
printf("buf[3] = %d\n", buf[3]);//打印出0,是一个结束字符
//这就验证了破坏了原来的字符串
return 0;
}