string函數用法

string函數用法

@函數名稱:      strdup
函數原型:      char *strdup(const char *s)
函數功能:      字符串拷貝,目的空間由該函數分配
函數返回:      指向拷貝後的字符串指針
參數說明:      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;
}


@函數名稱:      strcpy
函數原型:      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;
}


@函數名稱:      strncpy
函數原型:      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 string[10];
     char *str1="abcdefghi";
     strncpy(string,str1,3);
     string[3]='/0';
     printf("%s",string);
     return 0;
}


@函數名稱:      strcat
函數原型:      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;
}


@函數名稱:      strncat
函數原型:      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;
}


@函數名稱:      strcmp
函數原型:      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 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;
}


@函數名稱:      strncmp
函數原型:      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()
{
     int ptr;
     char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";
     ptr=strncmp(buf2,buf1,3);
     if (ptr>0)
         printf("buffer 2 is greater than buffer 1");
     else
         printf("buffer 2 is less than buffer 1");
         ptr=strncmp(buf2,buf3,3);
     if (ptr>0)
         printf("buffer 2 is greater than buffer 3");
     else
         printf("buffer 2 is less than buffer 3");
     return(0);
}


@函數名稱:      strpbrk
函數原型:      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";

   while(p)
   {
     printf("%s/n",p);
     p=strpbrk(p+1,"aeiouAEIOU");
   }
   return 0;
}


@函數名稱:      strcspn
函數原型:      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"));
     printf("%d/n",strcspn("xxxbcadef","cba"));
     printf("%d/n",strcspn("123456789","cba"));
     return 0;
}


@函數名稱:      strspn
函數原型:      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("out to lunch","aeiou"));
     printf("%d/n",strspn("out to lunch","xyz"));
     return 0;
}


@函數名稱:      strchr
函數原型:      char* strchr(char* str,char ch);
函數功能:      找出str指向的字符串中第一次出現字符ch的位置
函數返回:      返回指向該位置的指針,如找不到,則返回空指針
參數說明:      str-待搜索的字符串,ch-查找的字符
所屬文件:      <string.h>

#include <string.h>
#include <stdio.h>
int main()
{
     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;
}


@函數名稱:      strrchr
函數原型:      char *strrchr(const char *s, int c)
函數功能:      得到字符串s中最後一個含有c字符的位置指針
函數返回:      位置指針
參數說明:
所屬文件:      <string.h>

#include <string.h>
#include <stdio.h>
int main()
{
     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",c,ptr-string);
     else
         printf("The character was not found");
     return 0;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章