字符串处理函数

来源:互联网 发布:筑巢软件怎么样 编辑:程序博客网 时间:2024/06/05 20:11

1. strcpy()函数

功能:拷贝一个字符串

用法:char *strcpy(char *dest, char *source);

示例:

#include “stdio.h”

#include “string.h”

Int main(void)

{

            char string[10];

            char *str1 = “abcdefgh”;

 

             strcpy(string, str1);

             printf(“%s\n”,string);

             return 0;

}

 

 

2. strncpy()函数

功能:串拷贝

用法:char *strncpy(char *dest, char *source, maxlen);

示例:

#include “stdio.h”

#include “string.h”

int main(void)

{

         char string[10];

         char *str = “abcdedf”;

         strncpy(string, str, 3);

         string[3]=’\0’;

         printf(“%s\n”, string);

         return 0;

}

 

3. strcat()函数

功能:字符串拼接函数

用法:char *strcat(char *dest, char *source);

示例:

#include “stdio.h”

#include “string.h”

Int main(void)

{

         char dest[25];

         char *bank=” “,*c=”C++”,*Borland=”Borland”;

         strcpy(dest, Borland);

         strcat(dest, bank);

         strcat(dest, c);

         printf(“%s\n”, dest);

         return 0;

}

 

4. strchr()、strrchr()函数

功能:strchr()在一个字符串中查找给定字符的第一个匹配之处;strrchr()函数返回一个指针,指向字符串中字符最后一次出现的位置

用法:char *strchr(char *str, char c);

示例:

#include “stdio.h”

#include “string.h”

Int main(void)

{

         char string[15];

         char *ptr, c=’r’;

         strcpy(string, “This is string”);

         ptr = strchr(string, c);

         if(ptr)

                   printf(“The character %c is %s\n”, c, ptr);

         else

                   printf(“The character was not found\n”);

         return 0;

}

函数返回:ring

 

5. strcmp()函数

功能:串比较

用法:int strcmp(char *str1, char *str2);

示例:

#include “stdio.h”

#include “string.h”

Int main(void)

{

         char *buf1 = “aaa”, *buf2 = “bbb”, *buf3 = “ccc”;

         int ptr;

         ptr = strcmp(buf2, buf1);

         if(ptr>0)

                   printf(“buf2 is great then buf1\n”);

         else

                   printf(“buf2 is less then buf1\n”);

        

         return 0;

}

 

5. strcnmp()函数

功能:串比较

用法:int strncmp(char *str1, char *str2, int maxlen);

示例:

#include “stdio.h”

#include “string.h”

Int main(void)

{

         char *buf1 = “aaabbb”, *buf2 = “bbbccc”, *buf3 = “ccc”;

         int ptr;

         ptr = strncmp(buf2, buf1, 3);

         if(ptr>0)

                   printf(“buf2 is greater then buf1”);

         else

                   printf(“buf2 is less then buf1”);

 

         return 0;

}

 

7. strstr()函数

功能:查找串中指定字符串首次出现的位置

用法:char *strstr(char *str1, char *str2);

示例:

#include “stdio.h”

#include “string.h”

Int main(void)

{

         char *str1 = “international”, *str2 = “nation”, *ptr;

         ptr = strstr(str1, str2);

         printf(“the substring is : %s\n”, ptr);

         return 0;

}

返回值:national

 

8. strtok()函数

功能:查找由在第二个串中指定的分割符分隔开的单词

用法:char *strtok(char *str1, char *str2);

示例:

#include “stdio.h”

#include “string.h”

Int main(void)

{

         char input[16] = “abc,d”;

char *p;

p = strtok(input, “,”);

printf(“%s\n”, p);

return 0;

}

返回值:abc

 

9. ctype.h

 

原创粉丝点击