C语言字符操作函数大全

1、字符串分割函数
头文件:#include <string.h>

定义函数:char * strtok(char *s, const char *delim);

函数说明:strtok()用来将字符串分割成一个个片段. 参数s 指向欲分割的字符串, 参数delim 则为分割字符串,当strtok()在参数s 的字符串中发现到参数delim 的分割字符时则会将该字符改为\0 字符. 在第一次调用时,strtok()必需给予参数s 字符串, 往后的调用则将参数s 设置成NULL. 每次调用成功则返回下一个分割后的字符串指针.

返回值:返回下一个分割后的字符串指针, 如果已无从分割则返回NULL.

范例
#include <string.h>
main()
{
    char s[] = "ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z";
    char *delim = "-: ";
    char *p;
    printf("%s ", strtok(s, delim));
    while((p = strtok(NULL, delim)))
        printf("%s ", p);
        printf("\n");
}
执行结果:
ab cd ef;gh i jkl;mnop;qrs tu vwx y;z //-与:字符已经被\0 字符取代

2、字符串查找函数
头文件:#include <string.h>
 
定义函数:char *strstr(const char *haystack, const char * needle);
 
函数说明:strstr()会从字符串haystack 中搜寻字符串needle, 并将第一次出现的地址返回.
 
返回值:返回指定字符串第一次出现的地址, 否则返回0.
 
范例
 #include <string.h>
 main()
 {
     char * s = "012345678901234567890123456789";
     char *p;
     p = strstr(s, "901");
     printf("%s\n", p);
 }
 
执行结果:
 9.01E+21


3、字符查找函数

头文件:#include <string.h>
 
定义函数:size_t strspn(const char *s, const char * accept);
 
函数说明:strspn()从参数s 字符串的开头计算连续的字符, 而这些字符都完全是accept 所指字符串中的字符.简单的说, 若strspn()返回的数值为n, 则代表字符串s 开头连续有n 个字符都是属于字符串accept 内的字符.
 
返回值:返回字符串s 开头连续包含字符串accept 内的字符数目.
 
范例
 #include <string.h>
 main()
 {
     char *str = "Linux was first developed for 386/486-based PCs. ";
     char *t1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
     printf("%d\n", strspn(str, t1));
 }
 执行结果:
 5 //计算大小写字母. 不包含" ", 所以返回Linux 的长度.


4、定位字符串中最后出现的指定字符
头文件:#include <string.h>

定义函数:char * strrchr(const char *s, int c);

函数说明:strrchr()用来找出参数s 字符串中最后一个出现的参数c 地址, 然后将该字符出现的地址返回.

返回值:如果找到指定的字符则返回该字符所在地址, 否则返回0.

范例
#include <string.h>
main()
{
    char *s = "0123456789012345678901234567890";
    char *p;
    p = strrchr(s, '5');
    printf("%s\n", p);
}

执行结果:
567890

5、定位字符串中第一个出现的指定字符
头文件:#include <include.h>

定义函数:char *strpbrk(const char *s, const char *accept);

函数说明:strpbrk()用来找出参数s 字符串中最先出现存在参数accept 字符串中的任意字符.

返回值:如果找到指定的字符则返回该字符所在地址, 否则返回0.

范例
#include <string.h>
main()
{
    char *s = "0123456789012345678901234567890";
char *p; p = strpbrk(s, "a1 839"); //1 会最先在s 字符串中找到
    printf("%s\n", p);
    p = strprk(s, "4398"); //3 会最先在s 字符串中找到
    printf("%s\n", p);
}

执行结果:
1.23E+29

6、复制字符串

头文件:#include <string.h>

定义函数:char * strncpy(char *dest, const char *src, size_t n);

函数说明:strncpy()会将参数src 字符串拷贝前n 个字符至参数dest 所指的地址.

返回值:返回参数dest 的字符串起始地址.

范例
#inclue <string.h>
main()
{
    char a[30] = "string(1)";
    char b[] = "string(2)";
    printf("before strncpy() : %s\n", a);
    printf("after strncpy() : %s\n", strncpy(a, b, 6));
}
执行结果:
before strncpy() : string(1)
after strncpy() : string(1)

7、字符串连接

头文件:#inclue <string.h>

定义函数:char * strncat(char *dest, const char *src, size_t n);

函数说明:strncat()会将参数src 字符串拷贝n 个字符到参数dest 所指的字符串尾. 第一个参数dest 要有足够的空间来容纳要拷贝的字符串.

返回值:返回参数dest 的字符串起始地址.

范例
#include <string.h>
main()
{
    char a[30] = "string(1)";
    char b[] = "string(2)";
    printf("before strncat() :%s\n", a);
    printf("after strncat() :%s\n", strncat(a, b, 6));
}

执行结果:
before strncat() : string(1)
after strncat() : string(1) string

8、字符串比较(忽略大小写)

头文件:#include <string.h>

定义函数:int strncasecmp(const char *s1, const char *s2, size_t n);

函数说明:strncasecmp()用来比较参数s1 和s2 字符串前n 个字符, 比较时会自动忽略大小写的差异.

返回值:若参数s1 和s2 字符串相同则返回0. s1 若大于s2 则返回大于0 的值, s1 若小于s2 则返回小于0 的值.

范例
#include <string.h>
main()
{
    char *a = "aBcDeF";
    char *b = "AbCdEf";
    if(!strncasecmp(a, b))
    printf("%s =%s\n", a, b);
}

执行结果:
aBcDef=AbCdEf

9、字符串长度计算

头文件:#include <string.h>

定义函数:size_t strlen (const char *s);

函数说明:strlen()用来计算指定的字符串s 的长度, 不包括结束字符"\0".

返回值:返回字符串s 的字符数.

范例:
/*取得字符串str 的长度 */
#include <string.h>
main()
{
    char *str = "12345678";
    printf("str length = %d\n", strlen(str));
}

执行结果:
str length = 8

10、复制字符串

定义函数:char * strdup(const char *s);

函数说明:strdup()会先用maolloc()配置与参数s 字符串相同的空间大小, 然后将参数s 字符串的内容复制到该内存地址, 然后把该地址返回. 该地址最后可以利用free()来释放.

返回值:返回一字符串指针, 该指针指向复制后的新字符串地址. 若返回NULL 表示内存不足.

范例
#include <string.h>
main()
{
    char a[] = "strdup";
    char *b;
    b = strdup(a);
    printf("b[]=\"%s\"\n", b);
}

执行结果:
b[]="strdup"

11、复制字符串

定义函数:char *strcpy(char *dest, const char *src);

函数说明:strcpy()会将参数src 字符串拷贝至参数dest 所指的地址.

返回值:返回参数dest 的字符串起始地址.

附加说明:如果参数 dest 所指的内存空间不够大, 可能会造成缓冲溢出(buffer Overflow)的错误情况, 在编写程序时请特别留意, 或者用strncpy()来取代.

范例
#include <string.h>
main()
{
    char a[30] = "string(1)";
    char b[] = "string(2)";
    printf("before strcpy() :%s\n", a);
    printf("after strcpy() :%s\n", strcpy(a, b));
}

执行结果:
before strcpy() :string(1)
after strcpy() :string(2)

12、字符串比较

定义函数:int strcmp(const char *s1, const char *s2);

函数说明:strcmp()用来比较参数s1 和s2 字符串. 字符串大小的比较是以ASCII 码表上的顺序来决定, 此顺序亦为字符的值. strcmp()首先将s1 第一个字符值减去s2 第一个字符值, 若差值为0 则再继续比较下个字符, 若差值不为0 则将差值返回. 例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(-33).

返回值:若参数s1 和s2 字符串相同则返回0. s1 若大于s2 则返回大于0 的值. s1 若小于s2 则返回小于0 的值.

范例
#include <string.h>
main()
{
    char *a = "aBcDeF";
    char *b = "AbCdEf";
    char *c = "aacdef";
    char *d = "aBcDeF";
    printf("strcmp(a, b) : %d\n", strcmp(a, b));
    printf("strcmp(a, c) : %d\n", strcmp(a, c));
    printf("strcmp(a, d) : %d\n", strcmp(a, d));
}
执行结果:
strcmp(a, b) : 32
strcmp(a, c) :-31
strcmp(a, d) : 0

13、小写换大写

头文件:#include <ctype.h>

定义函数:int toupper(int c);

函数说明:若参数 c 为小写字母则将该对应的大写字母返回.

返回值:返回转换后的大写字母, 若不须转换则将参数c 值返回.

范例 /* 将s 字符串内的小写字母转换成大写字母 */
#include <ctype.h>
main()
{
    char s[] = "aBcDeFgH12345;!#$";
    int i;
    printf("before toupper() : %s\n", s);
    for(i = 0; I < sizeof(s); i++)
        s[i] = toupper(s[i]);
        printf("after toupper() : %s\n", s);
}

执行结果:
before toupper() : aBcDeFgH12345;!#$
after toupper() : ABCDEFGH12345;!#$

14、大写换小写

头文件:#include <stdlib.h>

定义函数:int tolower(int c);

函数说明:若参数 c 为大写字母则将该对应的小写字母返回.

返回值:返回转换后的小写字母, 若不须转换则将参数c 值返回.

范例
/* 将s 字符串内的大写字母转换成小写字母 */
#include <ctype.h>
main()
{
    char s[] = "aBcDeFgH12345;!#$";
    int i;
    printf("before tolower() : %s\n", s);
    for(i = 0; I < sizeof(s); i++)
         s[i] = tolower(s[i]);
         printf("after tolower() : %s\n", s);
}

执行结果:
before tolower() : aBcDeFgH12345;!#$
after tolower() : abcdefgh12345;!#$

15、字符串换成整数

头文件:#include <stdlib.h>

定义函数:int atoi(const char *nptr);

函数说明:atoi()会扫描参数nptr 字符串, 跳过前面的空格字符, 直到遇上数字或正负符号才开始做转换, 而再遇到非数字或字符串结束时('\0')才结束转换, 并将结果返回.返回值返回转换后的整型数.

附加说明:atoi()与使用strtol(nptr, (char**)NULL, 10); 结果相同.

范例
/* 将字符串a 与字符串b 转换成数字后相加 */
#include <stdlib.h>
main()
{
    char a[] = "-100";
    char b[] = "456";
    int c;
    c = atoi(a) + atoi(b);
    printf("c=%d\n", c);
}

执行结果:
c=356

16、字符串换成浮点数

头文件:#include <stdlib.h>

定义函数:double atof(const char *nptr);

函数说明:atof()会扫描参数nptr 字符串, 跳过前面的空格字符, 直到遇上数字或正负符号才开始做转换, 而再遇到非数字或字符串结束时('\0')才结束转换, 并将结果返回. 参数nptr 字符串可包含正负号、小数点或E(e)来表示指数部分, 如123. 456 或123e-2.

返回值:返回转换后的浮点型数.

附加说明:atof()与使用strtod(nptr, (char**)NULL)结果相同.

范例:
/* 将字符串a 与字符串b 转换成数字后相加 */
#include <stdlib.h>
main()
{
    char *a = "-100.23";
    char *b = "200e-2";
    float c;
    c = atof(a) + atof(b);
    printf("c=%.2f\n", c);
}

执行结果:
c=-98.23

17、字符串转换为长整型

头文件:#include <stdlib.h>

定义函数:long atol(const char *nptr);

函数说明:atol()会扫描参数nptr 字符串, 跳过前面的空格字符, 直到遇上数字或正负符号才开始做转换, 而再遇到非数字或字符串结束时('\0')才结束转换, 并将结果返回.

返回值:返回转换后的长整型数.

附加说明:atol()与使用strtol(nptr, (char**)NULL, 10); 结果相同.

范例
/*将字符串a 与字符串b 转换成数字后相加 */
#include <stdlib.h>
main()
{
    char a[] = "1000000000";
    char b[] = " 234567890";
    long c;
    c = atol(a) + atol(b);
    printf("c=%d\n", c);
}

执行结果:
c=1234567890

18、字符串查找函数(返回最后一次出现的位置)
头文件:#include <string.h>

定义函数:char * rindex(const char *s, int c);

函数说明:rindex()用来找出参数s 字符串中最后一个出现的参数c 地址, 然后将该字符出现的地址返回. 字符串结束字符(NULL)也视为字符串一部分.

返回值:如果找到指定的字符则返回该字符所在的地址, 否则返回0.

范例
#include <string.h>
main()
{
    char *s = "0123456789012345678901234567890";
    char *p;
    p = rindex(s, '5');
    printf("%s\n", p);
}

执行结果:
567890

19、字符串查找函数(返回首次出现的位置)

头文件:#include <string.h>

定义函数:char * index(const char *s, int c);

函数说明:index()用来找出参数s 字符串中第一个出现的参数c 地址, 然后将该字符出现的地址返回. 字符串结束字符(NULL)也视为字符串一部分.

返回值:如果找到指定的字符则返回该字符所在地址, 否则返回0.

范例
#include <string.h>
main()
{
    char *s = "0123456789012345678901234567890";
    char *p;
    p = index(s, '5');
    printf("%s\n", p);
}

执行结果:
5.68E+25

20、函数名: 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);
}

 

posted @ 2012-09-26 16:41  江伟  阅读(545)  评论(0编辑  收藏  举报