温馨提示×

C语言怎么遍历所有字符

小亿
173
2024-05-14 18:45:14
栏目: 编程语言

在C语言中,可以使用循环来遍历字符串中的所有字符。以下是一个简单的示例代码:

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

在上面的代码中,我们定义了一个字符串 str,然后使用 for 循环遍历字符串中的字符,直到遇到字符串的结尾符 \0。在循环中,我们使用 %c 来打印每个字符。

运行上面的代码,输出将是:

H e l l o , W o r l d ! 

0