Cprogramming 简明教程

Strings in C

Strings in C

string in C 是 char 类型的一维数组,数组中的最后一个字符是表示为 '\0' 的“空字符”。因此,C 中的一个字符串可以定义为 char 类型值的一个以 null 结尾的序列。

A string in C is a one-dimensional array of char type, with the last character in the array being a "null character" represented by '\0'. Thus, a string in C can be defined as a null-terminated sequence of char type values.

Creating a String in C

让我们创建一个字符串“Hello”。它包含五个 char 值。在 C 中,char 类型的文字表示法使用单引号符号 − 例如 'H'。这五个字母放在单引号中,后跟用 '\0' 表示的空字符,被分配给一个 char 类型的数组。该数组的大小为五个字符加上空字符 − 六个。

Let us create a string "Hello". It comprises five char values. In C, the literal representation of a char type uses single quote symbols − such as 'H'. These five alphabets put inside single quotes, followed by a null character represented by '\0' are assigned to an array of char types. The size of the array is five characters plus the null character − six.

Example

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Initializing String Without Specifying Size

C 允许你在不声明大小的情况下初始化一个数组,在这种情况下编译器会自动确定数组大小。

C lets you initialize an array without declaring the size, in which case the compiler automatically determines the array size.

Example

char greeting[] = {'H', 'e', 'l', 'l', 'o', '\0'};

在内存中创建的数组可以按如下方式进行概要说明:

The array created in the memory can be schematically shown as follows −

string representation

如果字符串没有以“\0”结尾,则会导致行为不确定。

If the string is not terminated by "\0", it results in unpredictable behavior.

Note: 字符串的长度不包括空字符。库函数 strlen() 返回此字符串的长度为 5。

Note: The length of the string doesn’t include the null character. The library function strlen() returns the length of this string as 5.

Loop Through a String

您可以遍历一个字符串(字符数组)以使用 for loop 或任何其他 loop statements 访问和操作字符串的每个字符。

You can loop through a string (character array) to access and manipulate each character of the string using the for loop or any other loop statements.

Example

在下面的示例中,我们正在打印字符串中的字符。

In the following example, we are printing the characters of the string.

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

int main (){

   char greeting[] = {'H', 'e', 'l', 'l', 'o', '\0'};

   for (int i = 0; i < 5; i++) {
      printf("%c", greeting[i]);
   }

   return 0;
}

Output

它将生成如下输出:

It will produce the following output −

Hello

Printing a String (Using %s Format Specifier)

C 提供了一个格式说明符 %s ,它用于在您使用 printf()fprintf() 函数时打印字符串。

C provides a format specifier "%s" which is used to print a string when you’re using functions like printf() or fprintf() functions.

Example

“%s”说明符告诉函数遍历数组,直到它遇到空终止符 (\0) 并打印每个字符。这有效地打印了字符数组表示的整个字符串,而无需使用循环。

The "%s" specifier tells the function to iterate through the array, until it encounters the null terminator (\0) and printing each character. This effectively prints the entire string represented by the character array without having to use a loop.

#include <stdio.h>

int main (){

   char greeting[] = {'H', 'e', 'l', 'l', 'o', '\0'};
   printf("Greeting message: %s\n", greeting );

   return 0;
}

它将生成如下输出:

It will produce the following output −

Greeting message: Hello

您可以声明一个超大数组并分配较少数量的字符,对此 C compiler 没有问题。但是,如果该大小小于初始化中的字符,则输出中可能会有垃圾值。

You can declare an oversized array and assign less number of characters, to which the C compiler has no issues. However, if the size is less than the characters in the initialization, you may get garbage values in the output.

char greeting[3] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("%s", greeting);

Constructing a String using Double Quotes

C 允许您通过在双引号中括住字符来构造字符串,而不用在单引号中构造单个 char 值的 char 数组,并使用“\0”作为最后一个元素。这种初始化字符串的方法更加方便,因为编译器会自动将“\0”添加为最后一个字符。

Instead of constructing a char array of individual char values in single quotation marks, and using "\0" as the last element, C lets you construct a string by enclosing the characters within double quotation marks. This method of initializing a string is more convenient, as the compiler automatically adds "\0" as the last character.

Example

#include <stdio.h>

int main() {
  // Creating string
  char greeting[] = "Hello World";

  // Printing string
  printf("%s\n", greeting);

  return 0;
}

它将生成如下输出:

It will produce the following output −

Hello World

String Input Using scanf()

声明空终止字符串会造成困难,如果你想让用户输入一个字符串。您可以借助一个 for 循环,一次接收一个字符来存储在数组的每个下标中 −

Declaring a null-terminated string causes difficulty if you want to ask the user to input a string. You can accept one character at a time to store in each subscript of an array, with the help of a for loop −

Syntax

for(i = 0; i < 6; i++){
   scanf("%c", &greeting[i]);
}
greeting[i] = '\0';

Example

在下面的示例中,您可以使用 scanf() function 输入一个字符串,在输入特定字符后(在以下示例中为 5 个),我们分配空值 ( '\0' ) 来结束字符串。

In the following example, you can input a string using scanf() function, after inputting the specific characters (5 in the following example), we are assigning null ('\0') to terminate the string.

printf("Starting typing... ");

for (i = 0; i < 5; i++) {
  scanf("%c", &greeting[i]);
}

// Assign NULL manually
greeting[i] = '\0';

// Printing the string
printf("Value of greeting: %s\n", greeting);

运行代码并检查其输出:

Run the code and check its output −

Starting typing... Hello
Value of greeting: Hello

Example

输入 "\0"(空字符串)是不可能的,因为它是一个不可打印的字符。要克服此问题,在 scanf() 语句中使用 "%s" 格式说明符 −

It is not possible to input "\0" (the null string) because it is a non-printable character. To overcome this, the "%s" format specifier is used in the scanf() statement −

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

int main (){

   char greeting[10];

   printf("Enter a string:\n");
   scanf("%s", greeting);

   printf("You entered: \n");
   printf("%s", greeting);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Enter a string:
Hello
You entered:
Hello

Note: 如果数组的大小小于输入字符串的长度,则可能导致垃圾、数据损坏等情况。

Note: If the size of the array is less than the length of the input string, then it may result in situations such as garbage, data corruption, etc.

String Input with Whitespace

scanf("%s") 读取字符,直到遇到空格(空格、制表符、换行符等)或 EOF。因此,如果您尝试输入一个包含多个单词(以空格分隔)的字符串,那么 C 程序会将第一个空格之前的字符作为字符串的输入。

scanf("%s") reads characters until it encounters a whitespace (space, tab, newline, etc.) or EOF. So, if you try to input a string with multiple words (separated by whitespaces), then the C program would accept characters before the first whitespace as the input to the string.

Example

请看以下示例:

Take a look at the following example −

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

int main (){

   char greeting[20];

   printf("Enter a string:\n");
   scanf("%s", greeting);

   printf("You entered: \n");
   printf("%s", greeting);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Enter a string:
Hello World!

You entered:
Hello

String Input Using gets() and fgets() Functions

要接受包含空格的字符串输入,我们应该使用 gets() function 。它被称为非格式化控制台输入函数,在 "stdio.h" header file 中定义。

To accept a string input with whitespaces in between, we should use the gets() function. It is called an unformatted console input function, defined in the "stdio.h" header file.

Example: String Input Using gets() Function

请看以下示例:

Take a look at the following example −

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

int main(){

   char name[20];

   printf("Enter a name:\n");
   gets(name);

   printf("You entered: \n");
   printf("%s", name);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Enter a name:
Sachin Tendulkar

You entered:
Sachin Tendulkar

在更新版本的 C 中,gets() 已弃用。它可能是一个危险的函数,因为它不执行边界检查,可能会导致缓冲区溢出。

In newer versions of C, gets() has been deprecated. It is potentially a dangerous function because it doesn’t perform bound checks and may result in buffer overflow.

相反,建议使用 fgets() 函数。

Instead, it is advised to use the fgets() function.

fgets(char arr[], size, stream);

fgets() function 可用于接受来自任何输入流的输入,例如 stdin(键盘)或 FILE(文件流)。

The fgets() function can be used to accept input from any input stream, such as stdin (keyboard) or FILE (file stream).

Example: String Input Using fgets() Function

以下程序使用 fgets() 并接受用户输入的多单词。

The following program uses fgets() and accepts multiword input from the user.

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

int main(){

   char name[20];

   printf("Enter a name:\n");
   fgets(name, sizeof(name), stdin);

   printf("You entered: \n");
   printf("%s", name);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Enter a name:
Virat Kohli

You entered:
Virat Kohli

Example: String Input Using scanf("%[^\n]s")

您还可以使用 scanf("%[^\n]s") 作为替代方案。它读取字符,直到遇到换行符 ("\n")。

You may also use scanf("%[^\n]s") as an alternative. It reads the characters until a newline character ("\n") is encountered.

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

int main (){

   char name[20];

   printf("Enter a name: \n");
   scanf("%[^\n]s", name);

   printf("You entered \n");
   printf("%s", name);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Enter a name:
Zaheer Khan

You entered
Zaheer Khan

Printing String Using puts() and fputs() Functions

我们一直使用带 %s 说明符的 printf() function 来打印字符串。我们还可以使用 puts() function (在 C11 和 C17 版本中不推荐使用)或 fputs() function 作为替代方案。

We have been using printf() function with %s specifier to print a string. We can also use puts() function (deprecated in C11 and C17 versions) or fputs() function as an alternative.

Example

请看以下示例:

Take a look at the following example −

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

int main (){

   char name[20] = "Rakesh Sharma";

   printf("With puts(): \n");
   puts(name);

   printf("With fputs(): \n");
   fputs(name, stdout);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

With puts():
Harbhajan Singh

With fputs():
Harbhajan Singh