c语言string.h中的函数详解

char *strdup(const char *s)

函数功能: string duplicate字符串拷贝,目的空间由该函数分配
函数返回: 指向拷贝后的字符串指针
参数说明: src-待拷贝的源字符串
所属文件: <string.h>

#include <stdio.h>   
#include <string.h>   
#include <alloc.h>   
int main()   
{   
  char *dup_str, *string="abcde";   
  dup_str=strdup(string);   
  printf("%s", dup_str);   
  free(dup_str);   
  return 0;   
}  

char* strcpy(char* str1,char* str2)

函数功能: 把str2指向的字符串拷贝到str1中去
函数返回: 返回str1,即指向str1的指针
参数说明:
所属文件: <string.h>

#include <stdio.h>   
#include <string.h>   
int main()   
{   
  char string[10];   
  char *str1="abcdefghi";   
  strcpy(string,str1);   
  printf("the string is:%s\n",string);   
  return 0;   
}  

char *strncpy(char *dest, const char *src,int count)

函数功能: 将字符串src中的count个字符拷贝到字符串dest中去
函数返回: 指向dest的指针
参数说明: dest-目的字符串,src-源字符串,count-拷贝的字符个数
所属文件: <string.h>

#include<stdio.h>   
#include<string.h>   
int main()   
{   
   char*src = "bbbbbbbbbbbbbbbbbbbb";//20 'b's  
   char dest[50] ="aaaaaaaaaaaaaaaaaaaa";//20 'a's  
   puts(dest);  //输出:aaaaaaaaaaaaaaaaaaaa 
   strncpy(dest, src, 10);  
   puts(dest);    //输出:bbbbbbbbbbaaaaaaaaaa 
   return0; 
    //注意:strncpy只复制指定长度的字符,不会自动在末尾加'\0'。
    //若指定长度超过源字符串长度,不够的部分补‘\0’,
} 

char* strcat(char * str1,char * str2)

函数功能: 把字符串str2接到str1后面,str1最后的’\0’被取消
函数返回: str1
参数说明:
所属文件: <string.h>

#include <stdio.h>   
#include <string.h>  
int main()   
{   
  char buffer[80];  
  strcpy(buffer,"Hello ");   
  strcat(buffer,"world");   
  printf("%s\n",buffer);   
  return 0;   
}  

char *strncat(char *dest, const char *src, size_t maxlen)

函数功能: 将字符串src中前maxlen个字符连接到dest中
函数返回:
参数说明:
所属文件: <string.h>

#include <stdio.h>   
#include <string.h>  
char buffer[80];  
int main()   
{   
 strcpy(buffer,"Hello ");   
 strncat(buffer,"world",8);   
 printf("%s\n",buffer);   
 strncat(buffer,"*************",4);   
 printf("%s\n",buffer);   
 return 0; 
 //注意:与strncpy不同的是,strncat会自动在末尾加‘\0’
 //若指定长度超过源字符串长度,则只复制源字符串长度即停止
}  

int strcmp(char * str1,char * str2)

函数功能: 比较两个字符串str1,str2.
函数返回: str1<str2,返回负数;str1=str2,返回 0;str1>str2,返回正数.
参数说明:
所属文件: <string.h>

#include <string.h>   
#include <stdio.h>   
int main()   
{   
 char *buf1="aaa", *buf2="bbb",*buf3="ccc";   
 int ptr;   
 ptr=strcmp(buf2, buf1);   
 if(ptr>0)   
 {
   printf("buffer 2 is greater thanbuffer 1\n"); 
 }  
 else   
 {
   printf("buffer 2 is less thanbuffer 1\n");  
 }
 ptr=strcmp(buf2, buf3);   
 if(ptr>0)   
   printf("buffer 2 is greater thanbuffer 3\n");   
 else   
   printf("buffer 2 is less thanbuffer 3\n");   
 return 0;   
}  

int strncmp(char *str1,char *str2,int count)

函数功能: 对str1和str2中的前count个字符按字典顺序比较
函数返回: 小于0:str1<str2,等于0:str1=str2,大于0:str1>str2
参数说明: str1,str2-待比较的字符串,count-比较的长度
所属文件: <string.h>

#include<string.h>   
#include<stdio.h>   
int main()   
{   
  char str1[] ="aabbc";//  
  char str2[] = "abbcd";//  
  //为使测试程序更简练,此处假定了strncmp只返回-1,0,1三个数  
  char res_info[] = {'<','=','>'};  
  int res;  
  //前1个字符比较  
  res = strncmp(str1, str2, 1);  
  printf("1:str1%c str2\n", res_info[res+1]);  //输出:1:str1= str2 
   //前3个字符比较  
  res = strncmp(str1, str2, 3);  
  printf("3:str1%c str2\n", res_info[res+1]);  //输出:3:str1< str2 
} 

char *strpbrk(const char *s1, const char *s2)

函数功能: 得到s1中第一个“同时也出现在s2中”字符的位置指针
函数返回: 位置指针
参数说明:
所属文件: <string.h>

#include<stdio.h>   
#include<string.h>   
int main()   
{   
   char *p="Find all vowels";  
   p=strpbrk(p+1,"aeiouAEIOU");  
   while(p)  
   {  
      printf("%s\n",p);  
      p=strpbrk(p+1,"aeiouAEIOU");  
   }  
   return 0;   
}  

//输出:
//ind all vowels 
//all vowels 
//owels 
//els 

int strcspn(const char *s1, const char *s2)

函数功能: 统计s1中从头开始直到第一个“来自s2中的字符”出现的长度
函数返回: 长度
参数说明:
所属文件: <string.h>

#include<stdio.h>   
#include<string.h>  
int main()   
{   
 printf("%d\n",strcspn("abcbcadef","cba"));   //输出:0
 printf("%d\n",strcspn("xxxbcadef","cba"));   //输出:3
 printf("%d\n",strcspn("123456789","cba"));   //输出:9
  return 0;   
} 

int strspn(const char *s1, const char *s2)

函数功能: 统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度
函数返回: 位置指针
参数说明:
所属文件: <string.h>

#include<stdio.h>   
#include<string.h>   
#include<alloc.h>   
int main()   
{   
   printf("%d\n",strspn("abcbcadef","cba"));  //输出:6
   printf("%d\n",strspn("xxxbcadef","cba"));  //输出:0
   printf("%d\n",strspn("123456789","cba"));  //输出:0
   return 0;   
} 

char* strchr(char* str,char ch);

函数功能: 找出str指向的字符串中第一次出现字符ch的位置
函数返回: 返回指向该位置的指针,如找不到,则返回空指针
参数说明: str-待搜索的字符串,ch-查找的字符
所属文件: <string.h>

#include<string.h>   
#include<stdio.h>   
int main()   
{   
   char *str = "This is a string!";  
   char ch;  
   char *p;  

   while(1)  
   {  
      printf("Please input a char:");  
      ch = getchar();  
      p = strchr(str, ch);  
      if(p)  
      { 
          printf("%c is the %d character of\"%s\"\n",ch, (int)(p-str+1),str);  
      }
      else  
      { 
          printf("Not found!\n");  
      }
      printf("Press ESC to quit!\n\n");  
      if(27 == getch())  
      {
          break;
      }  
      fflush(stdin);  
   }  
  return 0;   
}  
/*输出:
Please input achar:i 
i is the 3character of "This is a string!" 
Press ESC to quit! 

Please input achar:l 
Not found! 
Press ESC to quit! 

Please input achar:s 
s is the 4character of "This is a string!" 
Press ESC to quit! 
*/

char *strrchr(const char *s, int c)

函数功能: 得到字符串s中最后一个含有c字符的位置指针
函数返回: 位置指针
参数说明:
所属文件: <string.h>

#include<string.h>   
#include<stdio.h>   
int main()   
{   
  charstring[15];   
  char*ptr,c='r';   
  strcpy(string,"This is a string");   
  ptr=strrchr(string,c);   
  if (ptr)   
  {
      printf("The character %c is at position:%d",c,ptr-string);
  }   
  else   
  {
      printf("The character was not found");   
  }
  return 0;   
}  

char* strstr(char* str1,char* str2);

函数功能: 找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)
函数返回: 返回该位置的指针,如找不到,返回空指针
参数说明:
所属文件: <string.h>

#include<stdio.h>   
#include<string.h>   
int main()   
{   
  char*str1="Open Watcom C/C++",*str2="Watcom",*ptr;   
 ptr=strstr(str1,str2);   
 printf("The substring is:%s\n",ptr);   //输出:The substringis:Watcom C/C++
  return 0;   
}  

char *strcasestr(const char *haystack, const char *needle)

函数功能: 用于在c串haystack中查找c串needle,忽略大小写。
函数返回: 如果找到则返回needle串在haystack串中第一次出现的位置的char指针.
所属文件: <string.h>
在<string.h>中定义了用于进行忽略大小写的字符串比较的函数strcasecmp及strncasecmp

char *strrev(char *s)

函数功能: 将字符串中的所有字符颠倒次序排列**
函数返回: 指向s的指针
参数说明:
所属文件: <string.h>

#include<string.h>   
#include<stdio.h>   
int main()   
{   
 char forward[]="string"; //原文中定义为char*是不对的,指向代码段的指针内容是不可变的  
 printf("Before strrev():%s",forward);   
 strrev(forward);   
 printf("Afterstrrev(): %s",forward);   
 return 0;   
} 
//输出:
//Beforestrrev():string 
//After strrev():gnirts 

char *strnset(char *s, int ch, size_t n)

函数功能: 将字符串s中前n个字符设置为ch的值
函数返回: 指向s的指针
参数说明:
所属文件: <string.h>

#include<stdio.h>   
#include<string.h>   
int main()   
{   
  charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";  
  char letter='x';  
  printf("string before strnset:%s\n",string);  
  strnset(string,letter,10);  
  printf("string after strnset:  %s\n",string);  
  return 0;   
} 
//输出:
//string beforestrnset: aaaaaaaaaaaaaaaaaaaaaaa 
//string afterstrnset:  xxxxxxxxxxaaaaaaaaaaaaa 

char *strset(char *s, int ch)

函数功能: 将字符串s中所有字符设置为ch的值**
函数返回: 指向s的指针
参数说明:
所属文件: <string.h>

#include<stdio.h>   
#include<string.h>   
int main()   
{   
  charstring[10]="123456789";   
  charsymbol='c';   
 printf("Before strset(): %s", string);   
 strset(string, symbol);   
 printf("After strset(): %s", string);   
  return 0;   
}  

char *strtok(char *s1, const char *s2)

函数功能: 分解s1字符串为用特定分隔符分隔的多个字符串(一般用于将英文句分解为单词)**
函数返回: 字符串s1中首次出现s2中的字符前的子字符串指针
参数说明: s2一般设置为s1中的分隔字符 ;
规定进行子调用时(即分割s1的第二、三及后续子串)第一参数必须是NULL;
每次匹配成功后,将s1中分割出的子串位置替换为NULL,因此s1被破坏了;
函数会记忆指针位置以供下一次调用;
所属文件: <string.h>

#include<string.h>   
#include<stdio.h>   
int main()   
{   
  char *p;   
  char*buffer;   
  char*delims={ " .," };  
  buffer=strdup("Find words, all of them.");   
  printf("%s\n",buffer);   
  p=strtok(buffer,delims);   
  while(p!=NULL)
  {   
  	printf("word: %s\n",p);   
   	p=strtok(NULL,delims);   
  }   
	printf("%s\n",buffer);   
  	return 0;   
}
//根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字符串的分隔  

char*strsep(char **stringp, const char *delim)

函数功能: strsep(),作为strtok的升级版,是一个很有用的字符串处理函数

Be cautious when using this function. If you do use it, note that:  
This function modifies its first argument.  
This function cannot be used on constant strings.  
The identity of the delimiting character is lost.

所属文件: <string.h>

#include <stdio.h>   
#include <string.h>  
int main(void)  
{     
    char s[] = "hello world!";  
    char *p = s;   
    char *d = " ";    
    printf("%s\n", strsep(&p, d));      
    printf("%s\n", p);    
    return 0;  
	//输出:helloworld!  
} 

char *strupr(char *s)

函数功能: 将字符串s中的字符变为大写
函数返回:
参数说明:
所属文件: <string.h>

#include <stdio.h>   
#include <string.h>   
int main()   
{   
  char string[]="abcdefghijklmnopqrstuvwxyz",*ptr; //会影响原字符串的内存,用char[]来声明  
  ptr=strupr(string);   
  printf("%s",ptr);   
  return 0;   
}  

char *strlwr(char *s)

函数功能: 将字符串中的字符变为小写字符
函数返回: 指向s的指针
参数说明:
所属文件: <string.h>

#include<string.h>   
int main()   
{   
 char str[]="HOW TO SAY?";   
 printf("%s",strlwr(str));   
 return 0;   
}  

char *strerror(int errnum)

函数功能: 得到错误信息的内容信息
函数返回: 错误提示信息字符串指针
参数说明: errnum-错误编号
所属文件: <string.h>

#include <stdio.h>   
#include <errno.h>   
int main()   
{   
 char *buffer;   
 buffer=strerror(errno);   
 printf("Error: %s",buffer);   
 return 0;   
}  

void *memcpy(void *dest, const void *src, size_t n)

函数功能: 字符串拷贝
函数返回: 指向dest的指针
参数说明: src-源字符串,n-拷贝的最大长度
所属文件: <string.h>,<mem.h>

#include <stdio.h>   
#include <string.h>   
int main()   
{   
  char src[]="******************************";   
  char dest[]="abcdefghijlkmnopqrstuvwxyz0123456709";   
  char *ptr;   
  printf("destination before memcpy:%s\n",dest);   
  ptr=memcpy(dest,src,strlen(src));   
  if (ptr)   
    printf("destination after memcpy:%s\n",dest);   
  else   
    printf("memcpy failed");   
  return 0;   
}  
//输出:
//destination before memcpy:abcdefghijlkmnopqrstuvwxyz0123456709 
//destination after memcpy:******************************456709

void *memccpy(void *dest, const void *src, int c, size_t n)

函数功能: 字符串拷贝,到指定长度或遇到指定字符时停止拷贝
函数返回:
参数说明: src-源字符串指针,c-中止拷贝检查字符,n-长度,dest-拷贝底目的字符串指针
所属文件: <string.h>,<mem.h>

#include <string.h>   
#include <stdio.h>   
int main()   
{   
  char *src="This is the source string";   
  char dest[50];   
  char *ptr;   
  ptr=memccpy(dest,src,'c',strlen(src));   
  if (ptr)   
  {   
    *ptr='\0';   
    printf("The character wasfound:%s",dest);   
  }   
  else   
  {
	printf("The character wasn'tfound");     
  }  
  return 0;   
  //输出:The character was found:This is the sourc 
  //PS:指定字符被复制到dest中,memccpy返回了dest中指定字符的下一处的地址,返回NULL表示未遇到指定字符
}  

void *memchr(const void *s, int c, size_t n)

函数功能: 在字符串中第开始n个字符中寻找某个字符c的位置
函数返回: 返回c的位置指针,返回NULL时表示未找到
参数说明: s-要搜索的字符串,c-要寻找的字符,n-指定长度
所属文件: <string.h>,<mem.h>

#include <string.h>   
#include <stdio.h>   
int main()   
{   
  char str[17];   
  char *ptr;   
  strcpy(str,"This is a string");   
  ptr=memchr(str,'r',strlen(str));   
  if (ptr)   
  {
      printf("The character 'r' is at position:%d",ptr-str);
  }   
  else   
  {
      printf("The character was not found"); 
  }  
  return 0;   
} 
//memrchr 和上面类似只不过是返回ch的结尾。

int memcmp(const void *s1, const void *s2,size_t n)

函数功能: 按字典顺序比较两个串s1和s2的前n个字节
函数返回: <0,=0,>0分别表示s1<,=,>s2
参数说明: s1,s2-要比较的字符串,n-比较的长度
所属文件: <string.h>,<mem.h>

#include <stdio.h>   
#include <string.h>   
int main()    
{    
  char *buf1="ABCDE123";    
  char *buf2="abcde456";    
  int stat;    
  stat=memcmp(buf1,buf2,5);    
  printf("The strings to position 5 are");    
  if(stat) printf("not ");    
  printf("the same\n");    
  return 0;    
}   

int memicmp(const void *s1, const void *s2, size_t n)

函数功能: 按字典顺序、不考虑字母大小写对字符串s1,s2前n个字符比较
函数返回: <0,=0,>0分别表示s1<,=,>s2
参数说明: s1,s2-要比较的字符串,n-比较的长度
所属文件: <string.h>,<mem.h>

#include <stdio.h>   
#include <string.h>   
int main()   
{   
  char *buf1="ABCDE123";   
  char *buf2="abcde456";   
  int stat;   
  stat=memicmp(buf1,buf2,5);   
  printf("The strings to position 5 are");   
  if(stat) printf("not");   
  printf("the same");   
  return 0;   
  //输出:The strings to position 5 are the same 
} 

void *memmove(void *dest, const void *src, size_t n)

函数功能: 字符串拷贝
函数返回: 指向dest的指针
参数说明: src-源字符串,n-拷贝的最大长度
所属文件: <string.h>,<mem.h>

#include <string.h>   
#include <stdio.h>   
int main()   
{   
  chardest[40]="abcdefghijklmnopqrstuvwxyz0123456789";   
  printf("destination prior tomemmove:%s\n",dest);   
  memmove(dest+1,dest,35);   
  printf("destination aftermemmove:%s",dest);   
  return 0;   
}  
//PS:与memcpy不同的是,memmove可以处理目的字符串与源字符串地址空间出现重叠的情况,
//可保证待复制的内容不被破坏。

void *memset(void *s, int c, size_t n)

函数功能: 字符串中的n个字节内容设置为c
函数返回:
参数说明: s-要设置的字符串,c-设置的内容,n-长度
所属文件: <string.h>,<mem.h>

#include <string.h>  
#include <stdio.h>  
#include <mem.h>  
int main()  
{  
  charbuffer[]="Hello world";  
 printf("Buffer before memset:%s/n",buffer);  
 memset(buffer,'*',strlen(buffer)-1);  
 printf("Buffer after memset:%s",buffer);  
  return 0;  
}  
  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值