在字符串頭文件string下有很多常用的方法,主要包括:
<1> 復制
(1) memcpy
函數原型 void * memcpy ( void * destination, const void * source, size_t num );
參數
destination: 目標字符串
source: 待復制的字符串
size_t: 需要復制的長度
#include <stdio.h> #include <string.h> int main () { //待復制的字符串 char str1[] = "almost every programmer should know memset!"; //目標字符串 char str2[] = "I LOVE SHANGHAI"; //將待復制的字符串的前十個字符復制到目標字符串中 memcpy(str2, str1, 10); // 輸出復制后的目標字符串 puts(str2); return 0; }
執行結果:almost eveNGHAI
(2)memmove
函數原型 void * memmove ( void * destination, const void * source, size_t num );
功能:將待移除字符串中的前size_t個字符移動到目標字符串后面
參數
destination: 目標字符串
source: 待移動的字符串
size_t: 需要移動的長度
#include <stdio.h> #include <string.h> int main () { char str[] = "memmove can be very useful......"; memmove (str+20,str+15,11); puts (str); return 0; }
執行結果:memmove can be very very useful.
(3)strcpy
函數原型 char * strcpy ( char * destination, const char * source );
參數
destination:目標字符串
source:源字符串
#include <stdio.h> #include <string.h> int main () { char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful"); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); return 0; }
執行結果: str1: Sample string
str2: Samplestring
str3: copy successful
(4)strncpy
函數原型 char * strncpy ( char * destination, const char * source, size_t num );
參數
destination:目標字符串
source:源字符串
size_t:復制源字符串size_t長度字符
#include <stdio.h> #include <string.h> int main () { char str1[]= "To be or not to be"; char str2[40]; char str3[40]; /* copy to sized buffer (overflow safe): */ strncpy ( str2, str1, sizeof(str2) ); /* partial copy (only 5 chars): */ strncpy ( str3, str2, 5 ); str3[5] = '\0'; /* null character manually added */ puts (str1); puts (str2); puts (str3); return 0; }
執行結果:
To be or not to be
To be or not to be
To be
<2> 連接
(1)strcat
函數原型 char * strcat ( char * destination, const char * source );
參數
destination:目標字符串
source:源字符串
#include <stdio.h> #include <string.h> int main () { char str[80]; strcpy (str,"these "); strcat (str,"strings "); strcat (str,"are "); strcat (str,"concatenated."); puts (str); return 0; }
執行結果:these strings are concatenated.
(2)strncat
函數原型 char * strncat ( char * destination, const char * source, size_t num );
參數
destination:目標字符串
source:源字符串
size_t:源字符串中需要連接的長度
#include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; strcpy (str1,"To be "); strcpy (str2,"or not to be"); strncat (str1, str2, 6); puts (str1); return 0; }
執行結果:To be or not
<3> 比較
(1)memcmp
函數原型 int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#include <stdio.h> #include <string.h> int main () { char buffer1[] = "DWgaOtP12df0"; char buffer2[] = "DWGAOTP12DF0"; int n; n=memcmp ( buffer1, buffer2, sizeof(buffer1) ); if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2); else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2); else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2); return 0; }
執行結果:'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.
(2) strcmp
函數原型 int strcmp ( const char * str1, const char * str2 );
#include <stdio.h> #include <string.h> int main () { char buffer1[] = "DWgaOtP12df0"; char buffer2[] = "DWGAOTP12DF0"; int n; n=strcmp( buffer1, buffer2); if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2); else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2); else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2); return 0; }
執行結果:'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.
(3)strncmp
函數原型 int strncmp ( const char * str1, const char * str2, size_t num );
#include <stdio.h> #include <string.h> int main () { char buffer1[] = "DWgaOtP12df0"; char buffer2[] = "DWGAOTP12DF0"; int n; n=strncmp( buffer1, buffer2, 2); if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2); else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2); else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2); return 0; }
執行結果:'DWgaOtP12df0' is the same as 'DWGAOTP12DF0'.
<4> 查找
(1)memchr
函數原型 void * memchr ( void * ptr, int value, size_t num );
#include <stdio.h> #include <string.h> int main () { char * pch; char str[] = "Example string"; pch = (char*) memchr (str, 'p', strlen(str)); if (pch!=NULL) printf ("'p' found at position %d.\n", pch-str+1); else printf ("'p' not found.\n"); return 0; }
執行結果:'p' found at position 5.
(2)strchr
函數原型:char * strchr ( char * str, int character );
#include <stdio.h> #include <string.h> int main () { char str[] = "This is a sample string"; char * pch; printf ("Looking for the 's' character in \"%s\"...\n",str); pch=strchr(str,'s'); while (pch!=NULL) { printf ("found at %d\n",pch-str+1); pch=strchr(pch+1,'s'); } return 0; }
執行結果: Looking for the 's' character in "This is a sample string"... found at 4 found at 7 found at 11 found at 18
(3)strrchr
函數原型 char * strrchr ( char * str, int character );
#include <stdio.h> #include <string.h> int main () { char str[] = "This is a sample string"; char * pch; pch=strrchr(str,'s'); printf ("Last occurence of 's' found at %d \n",pch-str+1); return 0; }
執行結果:Last occurrence of 's' found at 18
(4)strstr
函數原型 char * strstr ( char * str1, const char * str2 );
#include <stdio.h> #include <string.h> int main () { char str[] ="This is a simple string"; char * pch; pch = strstr (str,"simple"); strncpy (pch,"sample",6); puts (str); return 0; }
執行結果:This is a sample string
<5> 其他
(1)memset
函數原型 void * memset ( void * ptr, int value, size_t num );
#include <stdio.h> #include <string.h> int main () { char str[] = "almost every programmer should know memset!"; memset (str,'-',6); puts (str); return 0; }
執行結果:------ every programmer should know memset!
(2)strlen
函數原型 size_t strlen ( const char * str );
#include <stdio.h> #include <string.h> int main () { char szInput[256]; printf ("Enter a sentence: "); gets (szInput); printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput)); return 0; }
執行結果: Enter sentence: just testing The sentence entered is 12 characters long.