C 中的字符串:如何在 C 中声明和初始化字符串变量
C 中的字符串是什么?
C 中的字符串只不过是字符的线性序列集合。‘C’ 始终将字符串视为单个数据,即使它包含空格。单个字符使用单引号表示。字符串使用双引号表示。
Example, "Welcome to the world of programming!"
‘C’ 提供了标准库 <string.h>,其中包含许多函数,可用于轻松地对 C 中的字符串执行复杂的操作。
如何在 C 中声明字符串?
C 字符串是 char 作为数据类型的简单数组。‘C’ 语言不支持字符串作为数据类型。因此,要在 C 中显示字符串,您需要使用字符数组。
在 C 中将变量声明为字符串的通用语法如下:
char string_variable_name [array_size];
字符串的经典声明如下:
char string_name[string_length] = "string";
声明 C 字符串变量时必须定义数组的大小,因为它用于计算将在 C 的字符串变量中存储多少个字符。以下是一些有效的字符串声明示例:
char first_name[15]; //declaration of a string variable char last_name[15];
上面的示例表示大小为 15 的字符串变量。这意味着给定的 C 字符串数组最多可以容纳 15 个字符。数组的索引从 0 开始,因此它将存储从 0-14 位置的字符。C 编译器会自动为创建的字符数组添加一个 NULL 字符 ‘\0’。
如何在 C 中初始化字符串?
让我们来学习 C 中的字符串初始化。以下示例演示了 C 中字符串的初始化:
char first_name[15] = "ANTHONY"; char first_name[15] = {'A','N','T','H','O','N','Y','\0'}; // NULL character '\0' is required at end in this declaration char string1 [6] = "hello";/* string size = 'h'+'e'+'l'+'l'+'o'+"NULL" = 6 */ char string2 [ ] = "world"; /* string size = 'w'+'o'+'r'+'l'+'d'+"NULL" = 6 */ char string3[6] = {'h', 'e', 'l', 'l', 'o', '\0'} ; /*Declaration as set of characters ,Size 6*/
在 string3 中,必须显式添加 NULL 字符,并且字符用单引号括起来。
‘C’ 也允许我们在不定义字符数组大小的情况下初始化字符串变量。可以这样做:
char first_name[ ] = "NATHAN";
C 中的字符串名称充当指针,因为它们基本上是数组。
C 字符串输入:C 程序读取字符串
在编写要求用户输入的交互式程序时,C 提供了 scanf()、gets() 和 fgets() 函数来查找用户输入的一行文本。
当我们使用 scanf() 读取时,我们使用“%s”格式说明符,而不使用“&”来访问变量地址,因为数组名称充当指针。
例如
#include <stdio.h> int main() { char name[10]; int age; printf("Enter your first name and age: \n"); scanf("%s %d", name, &age); printf("You entered: %s %d",name,age); }
输出
Enter your first name and age: John_Smith 48
scanf 函数的问题在于它从不读取完整的 C 字符串。一旦遇到空格、换页符、垂直制表符、换行符或回车符,它就会停止读取。假设我们输入“Guru99 Tutorials”,则 scanf 函数永远不会读取整个字符串,因为在两个名称之间会出现空格字符。scanf 函数只会读取Guru99。
为了读取包含空格的字符串,我们使用 gets() 函数。Gets 会忽略空格。它会停止
读取直到遇到换行符(按下 Enter 键)。
例如
#include <stdio.h> int main() { char full_name[25]; printf("Enter your full name: "); gets(full_name); printf("My full name is %s ",full_name); return 0; }
输出
Enter your full name: Dennis Ritchie My full name is Dennis Ritchie
gets() 的另一个更安全的替代方法是 fgets() 函数,该函数读取指定数量的字符。
例如
#include <stdio.h> int main() { char name[10]; printf("Enter your name plz: "); fgets(name, 10, stdin); printf("My name is %s ",name); return 0;}
输出
Enter your name plz: Carlos My name is Carlos
fgets() 的参数是
- 字符串名称,
- 要读取的字符数,
- stdin 表示从标准输入(即键盘)读取。
C 字符串输出:C 程序打印字符串
标准的 printf 函数用于在输出设备上打印或显示 C 中的字符串。使用的格式说明符是 %s
示例,
printf("%s", name);
字符串输出使用 fputs() 和 printf() 函数完成。
fputs() 函数
fputs() 需要字符串的名称以及指向您想显示文本的位置的指针。我们使用 stdout,它指的是标准输出,以便在屏幕上打印。
例如
#include <stdio.h> int main() {char town[40]; printf("Enter your town: "); gets(town); fputs(town, stdout); return 0;}
输出
Enter your town: New York New York
puts 函数
puts 函数用于在输出设备上打印 C 中的字符串,并将光标移回第一个位置。可以使用 puts 函数,如下所示:
#include <stdio.h> int main() { char name[15]; gets(name); //reads a string puts(name); //displays a string return 0;}
此函数的语法比其他函数相对简单。
字符串库
标准的 ‘C’ 库提供了各种函数来处理程序中的字符串。这些函数也称为字符串处理程序。所有这些处理程序都位于 <string.h> 头文件中。
函数 | 目的 |
---|---|
strlen() | 此函数用于查找字符串的长度。它返回字符串中字符的数量,不包括 NULL 字符。 |
strcat(str1, str2) | 此函数用于将两个字符串组合成一个字符串。它将 str2 附加或连接到 str1 的末尾,并返回指向 str1 的指针。 |
strcmp(str1, str2) | 此函数用于比较两个字符串。如果 str1 等于 str2,则返回 0;如果 str1 < str2,则返回小于 0 的值;如果 str1 > str2,则返回大于 0 的值。 |
让我们来看下面的程序,它演示了字符串库函数
#include <stdio.h> #include <string.h> int main () { //string initialization char string1[15]="Hello"; char string2[15]=" World!"; char string3[15]; int val; //string comparison val= strcmp(string1,string2); if(val==0){ printf("Strings are equal\n"); } else{ printf("Strings are not equal\n"); } //string concatenation printf("Concatenated string:%s",strcat(string1,string2)); //string1 contains hello world! //string length printf("\nLength of first string:%d",strlen(string1)); printf("\nLength of second string:%d",strlen(string2)); //string copy printf("\nCopied string is:%s\n",strcpy(string3,string1)); //string1 is copied into string3 return 0; }
输出
Strings are not equal Concatenated string:Hello World! Length of first string:12 Length of second string:7 Copied string is:Hello World!
其他重要的库函数是
- strncmp(str1, str2, n):如果 str1 的前 n 个字符等于 str2 的前 n 个字符,则返回 0;如果 str1 < str2,则返回小于 0 的值;如果 str1 > str2,则返回大于 0 的值。
- strncpy(str1, str2, n) 此函数用于从另一个字符串复制字符串。将 str2 的前 n 个字符复制到 str1
- strchr(str1, c):如果字符 c 在 str1 中第一次出现,则返回指向它的指针,如果找不到字符,则返回 NULL。
- strrchr(str1, c):它反向搜索 str1,并返回指向字符 c 在 str1 中的位置的指针,如果找不到字符,则返回 NULL。
- strstr(str1, str2):如果 str2 在 str1 中第一次出现,则返回指向它的指针,如果找不到 str2,则返回 NULL。
- strncat(str1, str2, n) 将 str2 的前 n 个字符附加(连接)到 str1 的末尾,并返回指向 str1 的指针。
- strlwr():将字符串转换为小写
- strupr():将字符串转换为大写
- strrev():反转字符串
将字符串转换为数字
在 C 编程中,我们可以将数字字符字符串转换为数字值,以防止运行时错误。stdio.h 库包含以下用于将字符串转换为数字的函数:
- int atoi(str) 表示 ASCII 转整数;它将 str 转换为等效的 int 值。如果第一个字符不是数字或未找到数字,则返回 0。
- double atof(str) 表示 ASCII 转浮点数,它将 str 转换为等效的 double 值。如果第一个字符不是数字或未找到数字,则返回 0.0。
- long int atol(str) 表示 ASCII 转长整数,将 str 转换为等效的长整数值。如果第一个字符不是数字或未找到数字,则返回 0。
以下程序演示了 atoi() 函数
#include <stdio.h> int main() {char *string_id[10]; int ID; printf("Enter a number: "); gets(string_id); ID = atoi(string_id); printf("you enter %d ",ID); return 0;}
输出
Enter a number: 221348 you enter 221348
- 像 char *string = “language” 这样的字符串指针声明是常量,不能修改。
摘要
- 字符串是存储在字符数组中的一系列字符。
- 字符串是包含在双引号中的文本。
- 诸如 ‘d’ 之类的字符不是字符串,它用单引号表示。
- ‘C’ 提供了标准库函数来处理程序中的字符串。字符串处理程序存储在 <string.h> 头文件中。
- 在使用字符串之前,必须先声明或初始化它。
- 有不同的字符串输入和输出函数,它们各自都有其特性。
- 别忘了包含字符串库来使用其函数
- 我们可以通过 atoi()、atof() 和 atol() 来将字符串转换为数字,这些函数对于编码和解码过程非常有用。
- 我们可以通过在 C 中定义字符串数组来操作不同的字符串。