C++ 遍历char
可以使用双重循环遍历char**,第一重循环遍历每个char*,第二重循环遍历当前char*所指向的字符串中的每个字符。
下面是一个示例代码:
#include <iostream>
using namespace std;
int main() {
char* arr[] = {"hello", "world", "c++"};
int size = sizeof(arr) / sizeof(char*);
for (int i = 0; i < size; i++) {
char* str = arr[i];
for (int j = 0; str[j] != '\0'; j++) {
cout << str[j] << " ";
}
cout << endl;
}
return 0;
}
该代码定义了一个char**类型的数组arr,包含三个字符串。使用sizeof运算符获取数组大小,然后使用两重循环遍历每个字符串中的每个字符,并输出到控制台。输出结果如下:
h e l l o
w o r l d
c + +
``
原文地址: https://www.cveoy.top/t/topic/g9oT 著作权归作者所有。请勿转载和采集!