c语言编程字符串
什么是字符串 ? (What are Strings?)
Strings are mostly considered difficult by many beginners but trust me, Strings are no big deal. A string is nothing but a group of characters stored in a character array. Character arrays are used in programming languages to manipulate words and sentences.
许多初学者通常认为字符串很困难,但请相信我,字符串没什么大不了的。 字符串不过是存储在字符数组中的一组字符。 字符数组在编程语言中用于操纵单词和句子。
A string is a constant in the form of the one-dimensional array of characters terminated by a null character which is \0. Example,
字符串是一维字符数组形式的常量,该数组以空字符\ 0终止。 例,
char city[] = {'T', 'O', 'K', 'Y', 'O', '\0'};
注意事项: (Points to be noted:)
Memory space occupied by each character is one byte and the last character has to be null \0.
每个字符占用的存储空间为一个字节,最后一个字符必须为null \ 0 。
\0 should not be confused with the digit 0 since the ASCII value of null is 0 whereas that of ‘0’ is 48.
The members of the array are stored in contiguous memory locations as shown below.
数组的成员存储在连续的内存位置中,如下所示。
The null character at the end is very important because that is the only way for the compiler to know where the string ends. But to be noted, the null declaration is not necessary. It is inserted by the compiler automatically as in the following example: char city[] = "TOKYO";
最后的空字符非常重要,因为这是编译器知道字符串结尾的唯一方法。 但是要注意,空声明不是必需的。 如以下示例所示,它由编译器自动插入: char city [] =“ TOKYO”;
The format specification used for printing and receiving a string is "%s".
用于打印和接收字符串的格式规范为“%s” 。
Now let us see some of the examples:
现在让我们看一些例子:
Example 1: Declare string and print character by character
示例1:声明字符串并逐个字符打印
#include <stdio.h>
int main()
{
//declaring string
char city[] = "Tokyo" ;
//loop counter
int i = 0 ;
//printing string one by one character
while ( i <= 4 )
{
printf ( "%c", city[i] ) ;
i++ ;
}
return 0;
}
Output
输出量
Tokyo
Simple program, where we are treating the string as a character array and we use %c in printf() statement. Also, the length of the word is taken to be 4 because the value of the first element in an array is always assigned 0, as we all know already. But this solution is not profitable if we have a bigger string.
简单的程序,我们将字符串视为字符数组,并在printf()语句中使用%c 。 同样,单词的长度也取为4,因为众所周知,数组中第一个元素的值始终分配为0 。 但是,如果我们使用更大的字符串,此解决方案将无济于事。
So, here is another way to do it:
因此,这是另一种方法:
Example 2: Declare string and print character by character till NULL not found
示例2:声明字符串并逐字符打印,直到找不到NULL
#include <stdio.h>
int main()
{
//declaring string
char city[] = "Tokyo" ;
//loop counter
int i = 0 ;
//printing string one by one character
//till NULL ('\0') not found
while ( city[i] != '\0' )
{
printf ( "%c", city[i] ) ;
i++ ;
}
return 0;
}
Output
输出量
Tokyo
Example 3: Declaring string and printing as string (using "%s" format specifier)
示例3:声明字符串并打印为字符串(使用“%s”格式说明符)
#include <stdio.h>
int main()
{
//declaring string
char city[] = "Tokyo" ;
//print as string
//using "%s" format specifier
printf("%s",city);
return 0;
}
Output
输出量
Tokyo
This was the simplest way to print a string using the general specification of %s.
这是使用%s的一般规范来打印字符串的最简单方法。
Example 4: Reading string using scanf() function
示例4:使用scanf()函数读取字符串
#include <stdio.h>
int main()
{
//declaring string/character array
char city[15] ;
//read string
printf ( "Enter a city: " ) ;
scanf ( "%s", city ) ;
//printing string
printf ( "Good Morning %s!", city) ;
return 0;
}
Output
输出量
Enter a city: Mumbai
Good Morning Mumbai!
Here, the declaration char city[15] stores 15 bytes of memory under the array city[]. Whereas, the scanf() function fills the characters entered till the enter key is pressed by the user. After that, scanf() automatically adds a null character leaving the further memory blank.
此处,声明char city [15]在数组city []下存储15个字节的内存。 而scanf()函数将填充输入的字符,直到用户按下Enter键。 之后, scanf()自动添加一个空字符,将其他内存留空。
Points to be kept in mind while using scanf():
使用scanf()时要记住的几点:
The length of the string entered by the user should not exceed the value declared in the array since C compilers do not show any Array index out of bound error for this. It will simply result in the overwriting of important data.
用户输入的字符串的长度不应超过数组中声明的值,因为C编译器不会为此显示任何数组索引超出范围的错误。 这只会导致重要数据的覆盖。
Using scanf(), we can’t enter a multi-word string, for example, "New York". This problem can be resolved by using the function gets() and puts() as shown below,
使用scanf() ,我们无法输入多字字符串,例如“ New York”。 可以使用下面的函数gets()和puts()解决此问题,
Use of gets( ) and puts( ):
使用gets()和puts():
#include <stdio.h>
int main()
{
//declaring string/character array
char city[15] ;
//read the string
printf("Enter a city: ");
gets(city)
//printing the string
puts("Good Morning!");
puts(city);
return 0;
}
Output
输出量
Enter a city: New York
Good Morning!
New York
Here, puts() can display only one string at a time therefore, two puts() statements are used. Also, unlike printf(), it places the cursor automatically to the next line.
在这里, puts()一次只能显示一个字符串,因此,使用了两个puts()语句。 此外,与printf()不同,它会自动将光标置于下一行。
Though gets() can receive only one string at a time, still it is preferable as it can receive multi-word strings.
尽管gets()一次只能接收一个字符串,但还是可以接受的,因为它可以接收多字字符串 。
Author's note:
作者注:
This was a basic of what strings actually are. In the next article, I will write about string manipulations and standard library string functions. For better understanding, practice these codes on your own by changing the values and outputs. I’m ready to help if the case of queries. Happy coding!
这是字符串实际上是什么的基础 。 在下一篇文章中,我将写有关字符串操作和标准库字符串函数的文章 。 为了更好地理解,请通过更改值和输出自行练习这些代码。 如有查询,我随时可以提供帮助。 编码愉快!
c语言编程字符串