在C++中遍历字符串中的每一个字符的程序
在这个程序中,我们将看到如何在C++中迭代字符串的每个字符。要遍历每个字符,我们可以使用从0到(字符串长度-1)的循环。为了访问字符,我们可以使用字符串对象的子标运算符“[ ]”或at()函数。
Input: A string “Hello World” Output: “Hello World”
算法
Step 1: Get the string Step 2: Use R before string to make it raw string Step 3: End
示例代码
#include<iostream>
using namespace std;
main() {
string my_str = "Hello World";
for(int i = 0; i<my_str.length(); i++) {
cout << my_str.at(i) << endl; //get character at position i
}
}输出
H e l l o W o r l d
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP