在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