程序在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
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短