C 字符串

2018-05-19 16:35 更新

學習C - C字符串

字符串常量是一對雙引號字符之間的字符序列。

一對雙引號之間的任何內(nèi)容都由編譯器解釋為字符串。

以下語句說明了這一點:

printf("This is a string.");
printf("This is on\ntwo lines!");
printf("For \" you write \\\".");

我們可以在字符串末尾添加一個 \0 字符。


#include <stdio.h>

int main(void)
{
  printf("The character \0 is used to terminate a string.");
  return 0;
}

上面的代碼生成以下結(jié)果。


存儲字符串的變量

C沒有字符串變量類型,沒有處理字符串的特殊操作符。

C標準庫提供一系列處理字符串的函數(shù)。

您使用一個類型為char的數(shù)組來容納字符串。

你可以這樣聲明一個數(shù)組變量:

char my_string[20];

此變量可以容納最多包含19個字符的字符串,因為必須允許一個元素作為終止字符。

您可以使用此數(shù)組來存儲不是字符串的20個字符。

編譯器會自動將 \0 添加到每個字符串常量的末尾。

您可以在聲明它時初始化一個字符串變量:

char my_string[] = "This is a string.";

這里你沒有明確定義數(shù)組維度。

編譯器將為維度賦值一個值,該值足以容納初始化字符串常量。

在這種情況下,它將是18.17個元素是字符,一個額外的元素是為終止\0。

您可以使用字符串初始化char類型的元素數(shù)組的一部分。

例如:

char str[40] = "To be";

編譯器會將前五個元素str [0]初始化為str [4],其中字符串的字符為常量,str [5]將包含空字符'\ 0'。

為陣列的所有40個元素分配內(nèi)存空間。

初始化char數(shù)組并將其聲明為常量是處理標準消息的好方法:

const char message[] = "This is a test.";

要引用存儲在數(shù)組中的字符串,請使用數(shù)組名稱。

例如,要使用printf()函數(shù)輸出存儲在消息中的字符串,可以這樣寫:

printf("\nThe message is: %s", message);

%s 規(guī)范用于輸出一個以null結(jié)尾的字符串。

字符串格式化


  #include <stdio.h> 
  #define BLURB "Authentic imitation!" 
  int main(void) 
  { 
      printf("[%2s]\n", BLURB); 
      printf("[%24s]\n", BLURB); 
      printf("[%24.5s]\n", BLURB); 
      printf("[%-24.5s]\n", BLURB); 
   
      return 0; 
  }    

上面的代碼生成以下結(jié)果。

例子

以下代碼顯示如何獲取字符串的長度。


#include <stdio.h>
int main(void)     {
  char str1[] = "This is a test.";
  char str2[] = "This is another test.";
  unsigned int count = 0;                // Stores the string length

  while (str1[count] != "\0")            // Increment count till we reach the
    ++count;                             // terminating character.

  printf("The length of the string \"%s\" is %d characters.\n", str1, count);

  count = 0;                             // Reset count for next string
  while (str2[count] != "\0")            // Count characters in second string
    ++count;

  printf("The length of the string \"%s\" is %d characters.\n", str2, count);
  return 0;
}

上面的代碼生成以下結(jié)果。

字符串數(shù)組

您可以存儲一大堆字符串,并通過單個變量名稱引用其中的任何一個。

char my_strings[3][32] = {
                        "Hi",
                        "Hello.",
                        "How are you."
                      };

雖然您必須在字符串數(shù)組中指定第二個維度,但您可以將其留給編譯器以確定有多少個字符串。您可以將以前的定義寫成:

char my_strings[][32] = {
                       "Hi",
                        "Hello.",
                        "How are you."
                     };

您可以使用以下代碼輸出三個my_strings:

for(unsigned int i = 0 ; i < sizeof(my_strings)/ sizeof(my_strings[0]) ; ++i)
  printf("%s\n", my_strings[i]);

以下代碼顯示了如何查找二維數(shù)組中的字符串數(shù)和每個字符串的長度:


    #include <stdio.h>

    int main(void)
    {
      char str[][70] =  {
                  "hi",
                  "m.o2fo.com",
                  "this is a test.",
                        };
      unsigned int count = 0;                              // Length of a string
      unsigned int strCount = sizeof(str)/sizeof(str[0]);  // Number of strings
      printf("There are %u strings.\n", strCount);

      // find the lengths of the strings
      for(unsigned int i = 0 ; i < strCount ; ++i)
      {
        count = 0;
        while (str[i][count])
          ++count;

        printf("The string:\n    \"%s\"\n contains %u characters.\n", str[i], count);
      }
      return 0;
    }

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號