字符串函数整理2


   前面的博客中我已经详细解释过strcpy,strlen,strchr,strcmp,strcat,strstr,memmove,memcopy这几个字符串,其

他的字符串函数只是稍微说了一下。由于我们又学习了一些字符串函数,所以接下来我再详细补充一些这样的函数。

1、字符串操作 

strncpy(p, p1, n) 复制指定长度字符串 

strncat(p, p1, n) 附加指定长度字符串 

strcasecmp 忽略大小写比较字符串

strncmp(p, p1, n) 比较指定长度字符串 

strchr(p, c) 在字符串中查找指定字符 

strrchr(p, c) 在字符串中反向查找

strpbrk(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找该集合的任一元素 

strspn(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找不属于该集合的任一元素的偏移 

strcspn(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找属于该集合的任一元素的偏移  

2、字符串到数值类型的转换 

strtod(p, ppend)   从字符串 p 中转换 double 类型数值,并将后续的字符串指针存储到 ppend 指向的 char* 类型

存储。

strtol(p, ppend, base) 从字符串 p 中转换 long 类型整型数值,base 显式设置转换的整型进制,设置为 0 以根据特

定格式判断所用进制,0x, 0X 前缀以解释为十六进制格式整型,0x 前缀以解释为八进制格式整型

atoi(p)   字符串转换到 int 整型 

atof(p)   字符串转换到 double 符点数 

atol(p)   字符串转换到 long 整型

3、字符检查 

isalpha() 检查是否为字母字符 

isupper() 检查是否为大写字母字符 

islower() 检查是否为小写字母字符 

isdigit() 检查是否为数字 

isxdigit() 检查是否为十六进制数字表示的有效字符 

isspace() 检查是否为空格类型字符 

iscntrl() 检查是否为控制字符 

ispunct() 检查是否为标点符号 

isalnum() 检查是否为字母和数字 

isprint() 检查是否是可打印字符 

isgraph() 检查是否是图形字符,等效于 isalnum() | ispunct() 

4、函数原型

注意:同样的,所有字符串处理函数都包含在头文件string.h中。

1)strncpy:

原型:char *strncpy( char *Dest, const char *Source, size_t count );

功能:将字符串source中前count个字符拷贝到字符串destination中。

 strncpy函数应用举例 


例子: 

        #include <stdio.h>

#include <string.h>

int main()

{

char string[10];

char *str1 = "abcdefghi";

strncpy(string, str1, 3);

string[3] = '\0';

printf("%s\n", string);

return 0;

}


运行结果:abc

注意:字符串source中前count个字符将覆盖掉字符串destination中前count个字符!如果count值小于或等于

strSource串的长度, 不会自动添加NULL结束符目标串中, 而count大于strSource串的长度时, 则将strSource用NULL

结束符填充补齐count个字符, 复制到目标串中. 不能处理源串与目标串重叠的情况.函数返回strDestinatio值. strncpy

是一个长度受限的字符串,它调用的结果可能不是一个字符串,一次字符串必须以NUL字节结尾。

2)strncat:

原型:char *strncat( char *Dest, const char *Source, size_t count );

功能:将字符串source的前count个字符接到字符串dest的后面 

例子:

#include<stdio.h>

#include<string.h>

int main()

{

char d[20] = "tianna";

char *s = "xiasiwole ";

strncat(d, s, 9);

printf("%s", d);

getchar();

return 0;

}

运行结果:tiannaxiasiwole

注意:将源串strSource开始的count个字符添加到目标串strDest后. 源串strSource的字符会覆盖目标串

strDestination后面的结束符NULL. 如果count大于源串长度, 则会用源串的长度值替换count值. 得到的新串后面会自

动加上NULL结束符. 与strcat函数一样, 本函数不能处理源串与目标串重叠的情况. 函数返回strDestination值. 


3)memset:

原型:void *memset(void *dest, int c, size_t count);  

功能:将dest前面count个字符置为字符c.  返回dest的值. 

例子:

#include <string.h>

#include <stdio.h>

int main()
{
char buffer[] = "This is a test of the memset function";

printf("Before: %s\n", buffer);

memset(buffer, '*', 4);

printf("After:  %s\n", buffer);

return 0;
}

运行结果:

Before:This is a test of the meset function

After:****is a test of the meset function


4)memcpy:

原型:void *memcpy(void *dest, const void *src, size_t count);  

功能:从src复制count字节的字符到dest. 与memmove功能一样, 只是不能处理src和dest出现重叠.  返回dest的值. 

例子:

#include <string.h>

#include <stdio.h>

int main()

{

int i = 0;

int str1[] = { 1,2,3,4,5 ,6,7,8};

int str2[] = { 6,7,8,9,0 };

int sz = sizeof(str1) / sizeof(str1[0]);

memcpy(str1, str2, sizeof(str2));

for (i = 0; i < sz; i++)
{
printf("%d  ", str1[i]);
}
return 0;
}


运行结果:

6 7 8 9 0 6 7 8


5)memchr:

原型:void *memchr(const void *buf, int c, size_t count);  

功能:在buf前面count字节中查找首次出现字符c的位置. 找到了字符c或者已经搜寻了count个字节, 查找即停止. 操

作成功则返回buf中首次出现c的位置指针, 否则返回NULL. 

例子:

#include<stdio.h>

#include <string.h>

int main()

{

char *s = "0123456789";

char *p;

p = memchr(s, '5', 10);

printf("%s\n", p);
}


运行结果:

56789


6)memccpy:

原型:void *_memccpy(void *dest, const void *src, int c, size_t count);  

功能:从src复制0个或多个字节的字符到dest. 当字符c被复制或者count个字符被复制时, 复制停止. 如果字符c被复

制, 函数返回这个字符后面紧挨一个字符位置的指针. 否则返回NULL.与memcpy()不同的是,memccpy()会在复制时

检查参数c 是否出现,若是则返回dest 中值为c 的下一个字节地址。

例子:

#include <stdio.h>

#include <string.h>

int main()
{

char str1[60] = "The bird is fly in the syk";

char buf[61];

char *pdest;

pdest = _memccpy(buf, str1, 'y', 60);

*pdest = '\0';

printf("Result:%s\n", buf);

printf("Length:%d characters\n", strlen(buf));

return 0;

}


运行结果:

The bird is fly

15 characters

注意:dest 指针要分配足够的空间,也即大于等于 num 字节的空间。如果没有分配空间,会出现断错误。dest 和 

src 所指的内存空间不能重叠(如果发生了重叠,使用 memmove() 会更加安全)。


 7)memcmp:

原型:int memcmp(const void *buf1, const void *buf2, size_t count);  

功能:比较buf1和buf2前面count个字节大小.  

返回值< 0, 表示buf1小于buf2;  

返回值为0, 表示buf1等于buf2;  

返回值> 0, 表示buf1大于buf2. 

例子:

#include <string.h>

#include <stdio.h>

int main()
{

char *str1 = "hello";

char *str2 = "my friends";

int temp;

temp = memcmp(str1, str2, strlen("my"));  

printf("%d\n", temp);

return 0;

}


运行结果:-1


8)memicmp:

原型:int memicmp(const void *buf1, const void *buf2, size_t count); 

功能:比较buf1和buf2前面count个字节. 与memcmp不同的是, 它不区分大小写. 

返回值同上.

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char *str1 = "welcome to my school";  
char *str2 = "welcome to my school";
int temp;
temp = _memicmp(str1, str2, strlen(",welcome to my school"));  
printf("%d\n", temp);
return 0;
}

运行结果:0


9)strrev: 

原型:char *strrev(char *string);  

功能:将字符串string中的字符顺序颠倒过来. NULL结束符位置不变.  返回调整后的字符串的指针. 

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char str[100] = "howareyou";
char *temp;
temp = _strrev(str);   //实现字符串反序
printf("The string strtemp reversed is:   %s  ", temp);
return 0;
}

运行结果:uoyerawoh


10)strupr:

原型:char *_strupr(char *string);  

功能:将string中所有小写字母替换成相应的大写字母, 其它字符保持不变.  返回调整后的字符串的指针.

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char str[100] = "I AM very happy";
char *temp = NULL;
temp = _strlupr(str);   //将字符串str中的大写字母转换为小写字母
printf("The string str converted is:  %s  ", temp);
return 0;
}

运行结果:I AM VERY HAPPY

11)strlwr: 

原型:char *_strlwr(char *string);  

功能:将string中所有大写字母替换成相应的小写字母, 其它字符保持不变.  返回调整后的字符串的指针. 

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char str[100] = "I AM very happy";
char *temp = NULL;
temp = _strlwr(str);   //将字符串str中的大写字母转换为小写字母
printf("The string str converted is:  %s  ", temp);
return 0;
}

运行结果:i am very happy

12)strrchr:

原型:char *strrchr(const char *string, int c);  

功能:查找字符c在字符串string中最后一次出现的位置, 也就是对string进行反序搜索, 包含NULL结束符.  

返回一个指针, 指向字符c在字符串string中最后一次出现的位置, 如果没有找到, 则返回NULL. 

例子:

#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
char *ptr;
char c = 'e';
strcpy(str, "hello");
ptr = strrchr(str, c);
if (ptr)
printf("The charactr %c is at position:%s\n", c,ptr);
else
printf("The charactr was not found\n");
return 0;
}


运行结果:
The character e is at position:ello


13)strdup:

原型:char *strdup(const char *strSource);  

功能:函数运行中会自己调用malloc函数为复制strSource字符串分配存储空间, 然后再将strSource复制到分配到

空间中. 注意要及时释放这个分配的空间. 返回一个指针, 指向为复制字符串分配的空间; 如果分配空间失败, 则返回

NULL值. 

例子:

#include <stdio.h>
#include <string.h>
int main()
{
char *str;
char *string = "abcde";
str = _strdup(string);
printf("%s\n", str);
free(str); 
return 0;
}

运行结果:abcde


14)strtod

原型:double strtod(char *str,char **endptr);

功能:将字符串转换为double型值

例子:

#include<string.h>
#include<stdio.h>
int main()
{
char input[30];
char *endptr;
double value;
printf("Enter a floatint point number: ");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf\n", input, value);
return 0;
}


15)strset:

原型:char *strset(char *string, int c);  

功能:将string串的所有字符设置为字符c, 遇到NULL结束符停止. 函数返回内容调整后的string指针.

例子:

#include <stdio.h>
#include <string.h>
int main()
{
char str1[10] = "123456";
char str2 = 'c';
_strset(str1, str2);
printf("%s\n", str1);
return 0;
}

运行结果:cccccc

16)strnset: 
原型:char *strnset(char *string, int c, size_t count);  

功能:将string串开始count个字符设置为字符c, 如果count值大于string串的长度, 将用string的长度替换count值. 

函数返回内容调整后的string指针.

例子:

#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "1234567890";
char str2 = 'a';
_strnset(str1,str2,7);
printf("%s\n", str1);
return 0;
}

运行结果:aaaaaaa890

17)strspn: 

原型:size_t strspn(const char *string, const char *strCharSet);  

功能:查找任何一个不包含在strCharSet串中的字符 (字符串结束符NULL除外) 在string串中首次出现的位置序号.  返

回一个整数值, 指定在string中全部由characters中的字符组成的子串的长度. 如果string以一个不包含在strCharSet

中的字符开头, 函数将返回0值. 

例子:

#include<string.h>
#include<stdio.h>
int main()
{
char *arr1 = "1234567890";
char *arr2 = "123DC5";
int len;
len = strspn(arr1, arr2);
printf("%d\n", len);
return 0;
}


运行结果:3

18)strcspn:

原型:size_t strcspn(const char *string, const char *strCharSet);  

功能:查找strCharSet串中任何一个字符在string串中首次出现的位置序号, 包含字符串结束符NULL.  

返回一个整数值, 指定在string中全部由非characters中的字符组成的子串的长度. 如果string以一个包含在

strCharSet中的字符开头, 函数将返回0值.

例子:

#include <string.h>
#include <stdio.h>

void main( void )
{
   char string[] = "xyzabc";
   int  pos;

   pos = strcspn( string, "abc" );
   printf( "First a, b or c in %s is at character %d\n", 
           string, pos );
}

运行结果:

First a, b or c in xyzabc is at character 3


19)strspn: 

原型:char *strspn(const char *string, const char *strCharSet);  

功能:查找任何一个不包含在strCharSet串中的字符 (字符串结束符NULL除外) 在string串中首次出现的位置指针. 返

回一个指针, 指向非strCharSet中的字符在string中首次出现的位置.

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char string[] = "cabbage";
int  result;
result = strspn(string, "abc");
printf("The portion of '%s' containing only a, b, or c "
"is %d bytes long\n", string, result);
return 0;
}

运行结果:The portion of cabbage containing only a, b, or c "
"is 5 bytes long


20)strpbrk: 

原型:char *strpbrk(const char *string, const char *strCharSet);  

功能:查找strCharSet串中任何一个字符在string串中首次出现的位置, 不包含字符串结束符NULL.  返回一个指针, 指

向strCharSet中任一字符在string中首次出现的位置. 如果两个字符串参数不含相同字符, 则返回NULL值. 

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char *arr1 = "ahfjhfdfsfhg";
char *arr2 = "jh";
char *ptr;
ptr = strpbrk(arr1, arr2);
if (ptr)
{
printf("%c\n", *ptr);
}
else
{
printf("strpbrk is not find\n");
}
return 0;
}

运行结果:h

 21)stricmp:

原型:int stricmp(const char *string1, const char *string2);  

功能:比较字符串string1和string2大小,和strcmp不同, 比较的是它们的小写字母版本.返回值与strcmp相同.

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char *buf1 = "AAA";
char *buf2 = "aaa";
int ptr;
ptr = _stricmp(buf2, buf1);
if (ptr > 0)
{
printf("buf2 is greater than buf1\n");
}
else
{
printf("buf2 equals buf1\n");
}
return 0;
}

运行结果:buf2 equals buf1

22)strcmpi: 

原型:int strcmpi(const char *string1, const char *string2);  

功能:等价于stricmp函数, 只是提供一个向后兼容的版本. 

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char *str1 = "what are you doing";
char *str2 = "i am playing";   //str1与str2的大小写不一样,而且长度不同
int tmp;
tmp = _strcmpi(str1, str2);   //将字符串比较的返回值保存在int型变量inttemp中,用strcmpi函数
printf("%d\n", tmp);
return 0;
}

运行结果:14

23)strncmp:

原型:int strncmp(const char *string1, const char *string2, size_t count);  

功能:比较字符串string1和string2大小,只比较前面count个字符. 比较过程中, 任何一个字符串的长度小于count, 

则count将被较短的字符串的长度取代. 此时如果两串前面的字符都相等, 则较短的串要小. 

返回值< 0, 表示string1的子串小于string2的子串;  

返回值为0, 表示string1的子串等于string2的子串;  

返回值> 0, 表示string1的子串大于string2的子串. 

例子:

#include<string.h>
#include<stdio.h>
int main()
{
char *str1 = "Hello,I am a butterfly";
char *str2 = "Hello,I am a lazy cat";
int n = 7; //指定比较前13个字符
int tmp;
tmp = strncmp(str1, str2, n);   //将字符串比较的返回值保存在int型变量inttemp中
printf("%d\n", tmp);
return 0;
}

运行结果:0

24)strnicmp:

原型:int strnicmp(const char *string1, const char *string2, size_t count); 

功能:比较字符串string1和string2大小,只比较前面count个字符. 与strncmp不同的是, 比较的是它们的小写字母版

本.  返回值与strncmp相同. 

例子:

#include <string.h>
#include <stdio.h>
int main()
{
char *buf1 = "BBBccc";
char *buf2 = "bbbccc";
int ptr;
ptr = _strnicmp(buf2, buf1, 3);
printf("%d\n", ptr);
return 0;
}

运行结果:0


25)strtok:

原型:char *strtok(char *strToken, const char *strDelimit);  

功能:在strToken 串中查找下一个标记, strDelimit字符集则指定了在当前查找调用中可能遇到的分界符. 返回一个指

针, 指向在strToken中找到的下一个标记. 如果找不到标记, 就返回NULL值. 每次调用都会修改strToken内容, 用NULL

字符替换遇到的每个分界符.

例子:

#include<string.h>
#include<stdio.h>
int main()
{
char arr[15] = "abc,d";
    char *p;
p = strtok(arr, ",");
if (p)
{
printf("%s\n", p);
}
p = strtok(NULL, ",");
if (p)
{
printf("%s\n", p);
}
return 0;
}

运行结果:

abc

d

26)swab原型:void swab(char *from,char *to,int nbytes);

功能:交换字节

strerror原型:char *strerror(int errnum);

功能:返回指向错误信息字符串的指针







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值