跳轉到內容

C 語言入門/C 字串函式庫

來自華夏公益教科書,自由的教學讀本

字串函式庫需要宣告

   #include <string.h>

最重要的字串函式如下

   strlen()    Get length of a string.
   strcpy()    Copy one string to another.
   strcat()    Link together (concatenate) two strings.
   strcmp()    Compare two strings.
   strchr()    Find character in string.
   strstr()    Find string in string.
   strlwr()    Convert string to lowercase.
   strupr()    Convert string to uppercase.

"strlen()" 函式給出字串的長度,不包括末尾的 NUL 字元

   /* strlen.c */

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

   int main()
   {
     char *t = "XXX";
     printf( "Length of <%s> is %d.\n", t, strlen( t ));
   }

這會列印

   Length of <XXX> is 3.

"strcpy" 函式從另一個字串複製一個字串。例如

   /* strcpy.c */

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

   int main()
   {
     char s1[100],
          s2[100];
     strcpy( s1, "xxxxxx 1" );
     strcpy( s2, "zzzzzz 2" );

     puts( "Original strings: " );
     puts( "" );
     puts( s1 );
     puts( s2 );
     puts( "" );

     strcpy( s2, s1 );

     puts( "New strings: " );
     puts( "" );
     puts( s1 );
     puts( s2 );
   }

這將列印

   Original strings:

   xxxxxx 1
   zzzzzz 2

   New strings:

   xxxxxx 1
   xxxxxx 1

請注意此程式的兩個特點

  • 此程式假設 "s2" 有足夠的空間來儲存最終的字串。如果情況並非如此,"strcpy()" 函式不會檢查,並會給出錯誤的結果。
  • 字串常量可以用作源字串,而不是字串變數。當然,使用字串常量作為目標沒有任何意義。

這些註釋適用於大多數其他字串函式。

strncpy()

[編輯 | 編輯原始碼]

"strcpy" 的變體形式名為 "strncpy",它將複製源字串的 "n" 個字元到目標字串,假設源字串中有那麼多個字元可用。例如,如果在示例程式中進行以下更改

   strncpy( s2, s1, 5 );

——那麼結果將變為

   New strings:

   xxxxxx 1
   xxxxxz 2

注意,引數 "n" 被宣告為 "size_t",它在 "string.h" 中定義。因為在頭 5 個字元中沒有空位元組,所以 strncpy 在複製後不會新增 '\0'。

"strcat()" 函式連線兩個字串

   /* strcat.c */

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

   int main()
   {
     char s1[50],
          s2[50];
     strcpy( s1, "Tweedledee " );
     strcpy( s2, "Tweedledum" );
     strcat( s1, s2 );
     puts( s1 );
   }

這會列印

   Tweedledee Tweedledum

strncat()

[編輯 | 編輯原始碼]

"strcat()" 的變體版本名為 "strncat()",它將向目標字串追加源字串的 "n" 個字元。如果上面的示例使用了 "strncat()" 且長度為 7

   strncat( s1, s2, 7 );

——結果將是

   Tweedledee Tweedle

同樣,長度引數的型別為 "size_t"。

"strcmp()" 函式比較兩個字串

   /* strcmp.c */

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

   #define ANSWER "blue"

   int main()
   {
     char t[100];
     puts( "What is the secret color?" );
     gets( t );
     while ( strcmp( t, ANSWER ) != 0 )
     {
       puts( "Wrong, try again." );
       gets( t );
     }
     puts( "Right!" );
   }

"strcmp()" 函式在比較成功時返回 0,否則返回非零值。比較區分大小寫,因此回答 "BLUE" 或 "Blue" 不會有效。

"strcmp()" 有三種替代形式

strncmp()

[編輯 | 編輯原始碼]
  • "strncmp()" 函式,顧名思義,它比較源字串和目標字串中的 "n" 個字元
 "strncmp( s1, s2, 6 )".

stricmp()

[編輯 | 編輯原始碼]
  • "stricmp()" 函式忽略比較中的大小寫。

strnicmp()

[編輯 | 編輯原始碼]
  • "strncmp" 的不區分大小寫的版本稱為 "strnicmp"。

"strchr" 函式在字串中查詢字元的第一次出現。如果找到,它返回指向該字元的指標,如果沒有找到,則返回 NULL。例如

   /* strchr.c */

   #include <stdio.h>

   #include <string.h>

   int main()
   {
     char *t = "MEAS:VOLT:DC?";
     char *p;
     p = t;
     puts( p );
     while(( p = strchr( p, ':' )) != NULL )
     {
       puts( ++p );
     }
   }

這會列印

   MEAS:VOLT:DC?
   VOLT:DC?
   DC?

字元被定義為字元常量,C 將其視為 "int"。注意示例程式如何在使用它之前遞增指標 ("++p"),以便它不指向 ":",而是指向它後面的字元。

strrchr()

[編輯 | 編輯原始碼]

"strrchr()" 函式與 "strchr()" 幾乎相同,只是它搜尋字串中字元的最後一次出現。

"strstr()" 函式類似於 "strchr()",只是它搜尋字串,而不是字元。它也返回一個指標

  char *s = "Black White Brown Blue Green";
  ...
  puts( strstr( s, "Blue" ) );

strlwr() 和 strupr()

[編輯 | 編輯原始碼]

"strlwr()" 和 "strupr()" 函式只是對源字串執行小寫或大寫轉換。例如

   /* casecvt.c */

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

   int main()
   {
     char *t = "Hey Barney hey!";
     puts( strlwr( t ) );
     puts( strupr( t ) );
   }

——列印

   hey barney hey!
   HEY BARNEY HEY!

這兩個函式只在某些編譯器中實現,不是 ANSI C 的一部分。

進一步閱讀

[編輯 | 編輯原始碼]
華夏公益教科書