Article directory
1. Storage of strings
-
The stored string must have the end mark '\0'
-
The length of the array storing the string is at least the effective length of the string + 1
Read the code below carefully to understand:
#include<stdio.h>
int main()
{
char str1[6] = {
'c', 'h', 'i', 'n', 'a' };
printf("%c\n", str1[5]); // 输出空字符
printf("%d\n", str1[5] == '\0'); // 判断是否含有空字符
char str2[5] = {
'c', 'h', 'i', 'n', 'a' };
printf("%c\n", str2[5]); // 错误输出
printf("%d\n", str2[5] == '\0'); // 判断是否含有空字符
char str3[6] = {
'c', 'h', 'i', 'n', 'a', '\0' };
printf("%c\n", str3[5]); // 输出空字符
printf("%d\n", str3[5] == '\0'); // 判断是否含有空字符
char str4[6] = {
"china" };
printf("%c\n", str4[5]); // 输出空字符
printf("%d\n", str4[5] == '\0'); // 判断是否含有空字符
char str5[5] = {
"china" };
printf("%c\n", str5[5]); // 错误输出
printf("%d\n", str5[5] == '\0'); // 判断是否含有空字符
char str6[6] = "china";
printf("%c\n", str6[5]); // 输出空字符
printf("%d\n", str6[5] == '\0'); // 判断是否含有空字符
return 0;
}
Output result:
According to the output result:
- 1 3 4 6 is equivalent and correct
- 2 5 is wrong
2. String input and output
1. Comparison of several input and output strings
function | Format | the difference |
---|---|---|
scanf() | scanf("%s", character array name) | You cannot enter a string with spaces. You can use multiple format control characters %s to enter multiple strings. When a space is encountered to end the input, you need to add the end of string mark '\0' manually. |
gets() | get(character array name) | Only one string can be entered. The input ends when a carriage return character is encountered, and the carriage return character '\n' is automatically converted to '\0' |
printf() | printf("%s", character array name) | A printf() function can output multiple strings without automatically wrapping lines after output. |
puts() | puts(character array name) | A puts() function can only output a string, and it will automatically wrap after output. |
2. Character-by-character input and output
- Use the standard input and output functions scanf() and printf() with the %c format descriptor
- A fixed number of characters must be entered
#include<stdio.h>
int main()
{
int i;
char str[11];
printf("请输入十个字符:");
for (i = 0; i < 10; i++)
scanf("%c", &str[i]);
str[i] = '\0';
i = 0;
while (str[i] != '\0')
{
printf("%c", str[i]);
i++;
}
return 0;
}
- Use the getchar() and putchar() functions to input and output strings
- Just enter the number of characters within the specified limit and end when you enter the carriage return character.
#include<stdio.h>
int main()
{
int i=0;
char str[80];
printf("请输入一串字符:");
while ((str[i] = getchar()) != '\n')
i++;
str[i] = '\0';
for (i = 0; str[i] != '\0'; i++)
printf("%c", str[i]);
return 0;
}
3. Overall input of string
- Use the standard input function scanf() with the %s format descriptor
- Calling format: scanf("%s", character array name);
- When using the %s format control character to input a string, the scanf() function will automatically add '\0' after the string.
#include<stdio.h>
int main()
{
char str[10];
printf("请输入一串字符:");
scanf("%s", str);//不用& str就是地址
printf("%s\n", str);
return 0;
}
When you need to input a string containing spaces in the middle, you need to use the scanf() function with multiple input parameters to match the input.
#include<stdio.h>
int main()
{
char str1[5],str2[5], str3[5];
scanf("%s%s%s", str1, str2, str3);
printf("%s %s %s\n", str1, str2, str3);
return 0;
}
result:
how are you
how are you
- Use the function gets() to enter a complete string
- Calling format: gets(character array name);
- This function can store a line of characters entered by the keyboard into a character array in the form of a string.
- When entering a line of characters, the Enter key is used as the end character, and the carriage return character '\n' is automatically converted to '\0' as the end mark of the string.
#include<stdio.h>
int main()
{
char str[50];
gets(str);
printf("%s\n", str);
return 0;
}
result:
how are you
how are you
4. String output
- Use the standard output function printf() with the %s format descriptor
- Calling format: printf("%s", character array name);
#include<stdio.h>
int main()
{
char str1[]="How are you?";
char str2[] = "I am fine.Thank you.";
printf("%s\n%s\n", str1,str2);
return 0;
}
result:
How are you?
I am fine.Thank you.
- Use the function puts() to output a complete string
- Calling format: puts(character array name);
- This function can output a string (a sequence of characters ending with '\0') to the screen
- Will automatically wrap after output
#include<stdio.h>
int main()
{
char str[]="How are you?";
puts(str);
printf("I am fine.Thank you.");
return 0;
}
result:
How are you?
I am fine.Thank you.//可看出换了行