亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C 中的字符串:如何声明变量、初始化、打印、示例

什么是 C 语言中的字符串?

C 中的字符串 只不过是线性序列中的字符集合。 “C”总是将字符串视为单个数据,即使它包含空格。使用单引号表示定义单个字符。字符串用双引号表示。

Example, "Welcome to the world of programming!"

‘C’提供了标准库,其中包含许多函数,可用于在C语言中轻松地对字符串执行复杂的操作。

在本教程中,您将学习-

如何在 C 中声明和初始化字符串

C 字符串是一个以 char 作为数据类型的简单数组。 “C”语言不直接支持字符串作为数据类型。因此,要在C中显示String,需要使用字符数组。

在C中将变量声明为String的一般语法如下,

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中的String初始化。下面的例子演示了C中String的初始化,

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 提供了 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()函数。获取忽略空格。到达换行符时停止读取(按下 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() 参数是:

字符串输出:打印/显示字符串

标准 printf 函数用于在输出设备上打印或显示 C 中的字符串。使用的格式说明符是 %s

例如,

printf("%s", name);

字符串输出是通过 fputs() 和 printf() 函数完成的。

fputs() 函数

fputs() 需要字符串的名称和指向要显示文本的位置的指针。我们使用标准输出来引用标准输出,以便打印到屏幕上。例如:

#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 函数用于在输出设备上打印 C 中的字符串并将光标移回第一个位置。 puts函数可以通过以下方式使用,

#include <stdio.h>
int main() {
char name[15];
gets(name);        //reads a string
puts(name);        //displays a string
return 0;}

这个函数的语法相对其他函数来说比较简单。

字符串库

标准的“C”库提供了各种函数来操作程序中的字符串。这些函数也称为字符串处理程序。所有这些处理程序都存在于 头文件中。

功能 目的 strlen() 此函数用于查找字符串的长度。它返回字符串中存在的字符数,不包括 NULL 字符。strcat(str1, str2) 该函数用于将两个字符串组合在一起形成一个字符串。它将 str2 追加或连接到 str1 的末尾并返回指向 str1 的指针。strcmp(str1, str2) 该函数用于比较两个字符串。如果 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。
  • strncpy(str1, str2, n) 该函数用于从另一个字符串中复制一个字符串。将 str2 的前 n 个字符复制到 str1
  • strchr(str1, c):它返回指向 str1 中第一次出现的字符 c 的指针,如果未找到字符,则返回 NULL。
  • strrchr(str1, c):反向搜索str1,返回一个指向char c在str1中位置的指针,如果没有找到则返回NULL。
  • strstr(str1, str2):返回指向str1中str2第一次出现的指针,如果没有找到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 to float,它将 str 转换为等效的 double 值。如果第一个字符不是数字或没有遇到数字,则返回 0.0。
  • long int atol(str) 代表 ASCII to long int,将 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’提供标准库函数来操作程序中的字符串。字符串操作符存储在 头文件中。
  • 在程序中使用之前,必须声明或初始化字符串。
  • 有不同的输入输出字符串函数,每一种都有其特点。
  • 不要忘记包含字符串库以使用其功能
  • 我们可以通过 atoi()、atof() 和 atol() 将字符串转换为数字,这对于编码和解码过程非常有用。
  • 我们可以通过在 C 中定义一个字符串数组来操作不同的字符串。

C语言

  1. Java 字符串
  2. Java 枚举字符串
  3. 如何在 VHDL 中创建字符串列表
  4. C++ 中的数组 |声明 |初始化 |指向数组示例的指针
  5. C++ 字符串:strcpy()、strcat()、strlen()、strcmp() 示例
  6. C 库中的 realloc() 函数:如何使用?语法和示例
  7. C 库中的 free() 函数:如何使用?通过示例学习
  8. Java 中的 String Length() 方法:如何通过示例查找
  9. Python 字符串:替换、连接、拆分、反转、大写和小写
  10. Java - 字符串类
  11. C# - 字符串
  12. Python - 字符串