您的位置:宽带测速网 > 编程知识 > c语言怎么读取字符串中的字符

c语言怎么读取字符串中的字符

2025-06-13 13:50来源:互联网 [ ]

在C语言中,可以使用循环结构来读取字符串中的每个字符。

例如,可以使用一个for循环来遍历字符串,并逐个读取每个字符:

#include <stdio.h>int main() {char str[] = "Hello, World!";for (int i = 0; str[i] != '\0'; i++) {printf("%c ", str[i]);}return 0;}

在上面的例子中,使用一个for循环遍历字符串str,直到遇到字符串的结束符’\0’为止。在每次循环中,使用printf函数打印出字符串中的每个字符。