一些常用的字符串函数

void *memset(void *dest, int c, size_t count); 
dest前面count个字符置为字符c.

返回dest的值.

void *memmove(void *dest, const void *src, size_t count); 

src复制count字节的字符到dest.如果srcdest出现重叠,函数会自动处理.

返回dest的值.

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

src复制count字节的字符到dest.memmove功能一样,只是不能处理srcdest出现重叠.

返回dest的值.

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

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

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

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

src复制0个或多个字节的字符到dest.当字符c被复制或者count个字符被复制时,复制停止.

如果字符c被复制,函数返回这个字符后面紧挨一个字符位置的指针.否则返回NULL.

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

比较buf1buf2前面count个字节大小.

返回值< 0,表示buf1小于buf2;
返回值为0, 表示buf1等于buf2;
返回值> 0, 表示buf1大于buf2.

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

比较buf1buf2前面count个字节.memcmp不同的是,它不区分大小写.

返回值同上.

 

//========================================

size_t strlen(const char *string); 

获取字符串长度,字符串结束符NULL不计算在内.

没有返回值指示操作错误.

char *strrev(char *string); 

将字符串string中的字符顺序颠倒过来. NULL结束符位置不变.

返回调整后的字符串的指针.

char *_strupr(char *string); 

string中所有小写字母替换成相应的大写字母,其它字符保持不变.

返回调整后的字符串的指针.

char *_strlwr(char *string); 

string中所有大写字母替换成相应的小写字母,其它字符保持不变.

返回调整后的字符串的指针.

char *strchr(const char *string, int c); 

查找字符c在字符串string中首次出现的位置, NULL结束符也包含在查找中.

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

char *strrchr(const char *string, int c); 

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

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

char *strstr(const char *string, const char *strSearch); 

在字符串string中查找strSearch子串.

返回子串strSearchstring中首次出现位置的指针.如果没有找到子串strSearch,则返回NULL.如果子串strSearch为空串,函数返回string.

char *strdup(const char *strSource); 

函数运行中会自己调用malloc函数为复制strSource字符串分配存储空间,然后再将strSource复制到分配到的空间中.注意要及时释放这个分配的空间.

返回一个指针,指向为复制字符串分配的空间;如果分配空间失败,则返回NULL.

char *strcat(char *strDestination, const char *strSource); 

将源串strSource添加到目标串strDestination后面,并在得到的新串后面加上NULL结束符.源串strSource的字符会覆盖目标串strDestination后面的结束符NULL.在字符串的复制或添加过程中没有溢出检查,所以要保证目标串空间足够大.不能处理源串与目标串重叠的情况.

函数返回strDestination.

char *strncat(char *strDestination, const char *strSource, size_t count); 

将源串strSource开始的count个字符添加到目标串strDest.源串strSource的字符会覆盖目标串strDestination后面的结束符NULL.如果count大于源串长度,则会用源串的长度值替换count.得到的新串后面会自动加上NULL结束符.strcat函数一样,本函数不能处理源串与目标串重叠的情况.

函数返回strDestination.

char *strcpy(char *strDestination, const char *strSource); 

复制源串strSource到目标串strDestination所指定的位置,包含NULL结束符.不能处理源串与目标串重叠的情况.

函数返回strDestination.

char *strncpy(char *strDestination, const char *strSource, size_t count); 

将源串strSource开始的count个字符复制到目标串strDestination所指定的位置.如果count值小于或等于strSource串的长度,不会自动添加NULL结束符目标串中,count大于strSource串的长度时,则将strSourceNULL结束符填充补齐count个字符,复制到目标串中.不能处理源串与目标串重叠的情况.

函数返回strDestination.

char *strset(char *string, int c); 

string串的所有字符设置为字符c,遇到NULL结束符停止.

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

char *strnset(char *string, int c, size_t count); 

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

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

size_t strspn(const char *string, const char *strCharSet); 

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

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

size_t strcspn(const char *string, const char *strCharSet); 

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

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

char *strspnp(const char *string, const char *strCharSet); 

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

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

char *strpbrk(const char *string, const char *strCharSet); 

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

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

int strcmp(const char *string1, const char *string2); 

比较字符串string1string2大小.

返回值< 0,表示string1小于string2;
返回值为0, 表示string1等于string2;
返回值> 0, 表示string1大于string2.

int stricmp(const char *string1, const char *string2); 

比较字符串string1string2大小,和strcmp不同,比较的是它们的小写字母版本.

返回值与strcmp相同.

int strcmpi(const char *string1, const char *string2); 

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

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

比较字符串string1string2大小,只比较前面count个字符.比较过程中,任何一个字符串的长度小于count,count将被较短的字符串的长度取代.此时如果两串前面的字符都相等,则较短的串要小.

返回值< 0,表示string1的子串小于string2的子串;
返回值为0, 表示string1的子串等于string2的子串;
返回值> 0, 表示string1的子串大于string2的子串.

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

比较字符串string1string2大小,只比较前面count个字符.strncmp不同的是,比较的是它们的小写字母版本.

返回值与strncmp相同.

char *strtok(char *strToken, const char *strDelimit); 

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

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

 

 

 

 

函数名: stpcpy
  :拷贝一个字符串到另一个
  : char *stpcpy(char *destin, char *source);
程序例:

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

int main(void)
{
   char string[10];
   char *str1 = "abcdefghi";

   stpcpy(string, str1);
   printf("%s\n", string);
   return 0;
}
 
 
 

函数名: strcat
  :字符串拼接函数
  : char *strcat(char *destin, char *source);
程序例:

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

int main(void)
{
   char destination[25];
   char *blank = " ", *c = "C++", *Borland = "Borland";

   strcpy(destination, Borland);
   strcat(destination, blank);
   strcat(destination, c);

   printf("%s\n", destination);
   return 0;
}
 
 
 

函数名: strchr
  :在一个串中查找给定字符的第一个匹配之处\
  : char *strchr(char *str, char c);
程序例:

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

int main(void)
 {
    char string[15];
    char *ptr, c = 'r';

    strcpy(string, "This is a string");
    ptr = strchr(string, c);
    if (ptr)
       printf("The character %c is at position: %d\n", c, ptr-string);
    else
       printf("The character was not found\n");
    return 0;
 }
 
 
 

函数名: strcmp
  :串比较
  : int strcmp(char *str1, char *str2);
Asic码,str1>str2,返回值> 0;两串相等,返回0
程序例:

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

int main(void)
 {
    char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
    int ptr;

    ptr = strcmp(buf2, buf1);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 1\n");
    else
       printf("buffer 2 is less than buffer 1\n");

    ptr = strcmp(buf2, buf3);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 3\n");
    else
       printf("buffer 2 is less than buffer 3\n");

    return 0;
 }
 
 
 

函数名: strncmpi
  :将一个串中的一部分与另一个串比较,不管大小写
  : int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:

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

int main(void)
{
   char *buf1 = "BBB", *buf2 = "bbb";
   int ptr;

   ptr = strcmpi(buf2, buf1);

   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1\n");

   if (ptr < 0)
      printf("buffer 2 is less than buffer 1\n");

   if (ptr == 0)
      printf("buffer 2 equals buffer 1\n");

   return 0;
}
 
 
 

函数名: strcpy
  :串拷贝
  : char *strcpy(char *str1, char *str2);
程序例:

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

int main(void)
 {
    char string[10];
    char *str1 = "abcdefghi";

    strcpy(string, str1);
    printf("%s\n", string);
    return 0;
 }
 
 
 

函数名: strcspn
  :在串中查找第一个给定字符集内容的段
  : int strcspn(char *str1, char *str2);
程序例:

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

int main(void)
 {
    char *string1 = "1234567890";
    char *string2 = "747DC8";
    int length;

    length = strcspn(string1, string2);
    printf("Character where strings intersect is at position %d\n", length);

    return 0;
 }
 
 
 

函数名: strdup
  :将串拷贝到新建的位置处
  : char *strdup(char *str);
程序例:

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

int main(void)
 {
    char *dup_str, *string = "abcde";

    dup_str = strdup(string);
    printf("%s\n", dup_str);
    free(dup_str);

    return 0;
 }
 
 
 

函数名: stricmp
  :以大小写不敏感方式比较两个串
  : int stricmp(char *str1, char *str2);
程序例:

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

int main(void)
{
   char *buf1 = "BBB", *buf2 = "bbb";
   int ptr;

   ptr = stricmp(buf2, buf1);

   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1\n");

   if (ptr < 0)
      printf("buffer 2 is less than buffer 1\n");

   if (ptr == 0)
      printf("buffer 2 equals buffer 1\n");

   return 0;
}
 
 

函数名: strerror
  :返回指向错误信息字符串的指针
  : char *strerror(int errnum);
程序例:

#include <stdio.h>
#include <errno.h>

int main(void)
{
   char *buffer;
   buffer = strerror(errno);
   printf("Error: %s\n", buffer);
   return 0;
}
 
 
 

函数名: strcmpi
  :将一个串与另一个比较,不管大小写
  : int strcmpi(char *str1, char *str2);
程序例:

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

int main(void)
{
   char *buf1 = "BBB", *buf2 = "bbb";
   int ptr;

   ptr = strcmpi(buf2, buf1);

   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1\n");

   if (ptr < 0)
      printf("buffer 2 is less than buffer 1\n");

   if (ptr == 0)
      printf("buffer 2 equals buffer 1\n");

   return 0;
}
 
 
 

函数名: strncmp
  :串比较
  : int strncmp(char *str1, char *str2, int maxlen);
程序例:

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

int  main(void)

{
   char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
   int ptr;

   ptr = strncmp(buf2,buf1,3);
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1\n");
   else
      printf("buffer 2 is less than buffer 1\n");

   ptr = strncmp(buf2,buf3,3);
   if (ptr > 0)
      printf("buffer 2 is greater than buffer 3\n");
   else
      printf("buffer 2 is less than buffer 3\n");

   return(0);
}
 
 

函数名: strncmpi
  :把串中的一部分与另一串中的一部分比较,不管大小写
  : int strncmpi(char *str1, char *str2);
程序例:

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

int main(void)
{
   char *buf1 = "BBBccc", *buf2 = "bbbccc";
   int ptr;

   ptr = strncmpi(buf2,buf1,3);

   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1\n");

   if (ptr < 0)
      printf("buffer 2 is less than buffer 1\n");

   if (ptr == 0)
      printf("buffer 2 equals buffer 1\n");

   return 0;
}
 
 

函数名: strncpy
  :串拷贝
  : char *strncpy(char *destin, char *source, int maxlen);
程序例:

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

int main(void)
{
   char string[10];
   char *str1 = "abcdefghi";

   strncpy(string, str1, 3);
   string[3] = '\0';
   printf("%s\n", string);
   return 0;
}
 
 

函数名: strnicmp
  :不注重大小写地比较两个串
  : int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:

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

int main(void)
{
   char *buf1 = "BBBccc", *buf2 = "bbbccc";
   int ptr;

   ptr = strnicmp(buf2, buf1, 3);

   if (ptr > 0)
      printf("buffer 2 is greater than buffer 1\n");

   if (ptr < 0)
      printf("buffer 2 is less than buffer 1\n");

   if (ptr == 0)
      printf("buffer 2 equals buffer 1\n");

   return 0;
}
 
 
 

函数名: strnset
  :将一个串中的所有字符都设为指定字符
  : char *strnset(char *str, char ch, unsigned n);
程序例:

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

int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz";
   char letter = 'x';

   printf("string before strnset: %s\n", string);
   strnset(string, letter, 13);
   printf("string after  strnset: %s\n", string);

   return 0;
}
 
 

函数名: strpbrk
  :在串中查找给定字符集中的字符
  : char *strpbrk(char *str1, char *str2);
程序例:

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

int main(void)
{
   char *string1 = "abcdefghijklmnopqrstuvwxyz";
   char *string2 = "onm";
   char *ptr;

   ptr = strpbrk(string1, string2);

   if (ptr)
      printf("strpbrk found first character: %c\n", *ptr);
   else
      printf("strpbrk didn't find character in set\n");

   return 0;
}
 
 
 

函数名: strrchr
  :在串中查找指定字符的最后一个出现
  : char *strrchr(char *str, char c);
程序例:

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

int main(void)
{
   char string[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\n", c, ptr-string);
   else
      printf("The character was not found\n");
   return 0;
}
 
 
 

函数名: strrev
  :串倒转
  : char *strrev(char *str);
程序例:

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

int main(void)
{
   char *forward = "string";

   printf("Before strrev(): %s\n", forward);
   strrev(forward);
   printf("After strrev():  %s\n", forward);
   return 0;
}
 

函数名: strset
  :将一个串中的所有字符都设为指定字符
  : char *strset(char *str, char c);
程序例:

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

int main(void)
{
   char string[10] = "123456789";
   char symbol = 'c';

   printf("Before strset(): %s\n", string);
   strset(string, symbol);
   printf("After strset():  %s\n", string);
   return 0;
}
 
 
 

函数名: strspn
  :在串中查找指定字符集的子集的第一次出现
  : int strspn(char *str1, char *str2);
程序例:

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

int main(void)
{
   char *string1 = "1234567890";
   char *string2 = "123DC8";
   int length;

   length = strspn(string1, string2);
   printf("Character where strings differ is at position %d\n", length);
   return 0;
}
 
 

函数名: strstr
  :在串中查找指定字符串的第一次出现
  : char *strstr(char *str1, char *str2);
程序例:

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

int main(void)
{
   char *str1 = "Borland International", *str2 = "nation", *ptr;

   ptr = strstr(str1, str2);
   printf("The substring is: %s\n", ptr);
   return 0;
}
 
 

函数名: strtod
  :将字符串转换为double型值
  : double strtod(char *str, char **endptr);
程序例:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   char input[80], *endptr;
   double value;

   printf("Enter a floating point number:");
   gets(input);
   value = strtod(input, &endptr);
   printf("The string is %s the number is %lf\n", input, value);
   return 0;
}
 
 
 

函数名: strtok
  :查找由在第二个串中指定的分界符分隔开的单词
  : char *strtok(char *str1, char *str2);
程序例:

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

int main(void)
{
   char input[16] = "abc,d";
   char *p;

   /* strtok places a NULL terminator
   in front of the token, if found */
   p = strtok(input, ",");
   if (p)   printf("%s\n", p);

   /* A second call to strtok using a NULL
   as the first parameter returns a pointer
   to the character following the token  */
   p = strtok(NULL, ",");
   if (p)   printf("%s\n", p);
   return 0;
}
 
 
 

函数名: strtol
  :将串转换为长整数
  : long strtol(char *str, char **endptr, int base);
程序例:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   char *string = "87654321", *endptr;
   long lnumber;

   /* strtol converts string to long integer  */
   lnumber = strtol(string, &endptr, 10);
   printf("string = %s  long = %ld\n", string, lnumber);

   return 0;
}
 

函数名: strupr
  :将串中的小写字母转换为大写字母
  : char *strupr(char *str);
程序例:

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

int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;

   /* converts string to upper case characters */
   ptr = strupr(string);
   printf("%s\n", ptr);
   return 0;
}
 
 
 

函数名: swab
  :交换字节
  : void swab (char *from, char *to, int nbytes);
程序例:

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

char source[15] = "rFna koBlrna d";
char target[15];

int main(void)
{
   swab(source, target, strlen(source));
   printf("This is target: %s\n", target);
   return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值