如何按字符掃描字符串並在單獨的行中打印每個字符,我正在考慮將字符串存儲在數組中,並使用for循環進行打印,但我不不知道如何....請幫助!如何在C++中掃描字符串
這裏是我的代碼:
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str;
char option;
cout << "Do you want to enter a string? \n";
cout << " Enter 'y' to enter string or 'n' to exit \n\n";
cin >> option ;
while (option != 'n' & option != 'y')
{
cout << "Invalid option chosen, please enter a valid y or n \n";
cin >> option;
}
if (option == 'n')
return 1;
else if (option == 'y')
{
cout << "Enter a string \n";
cin >> str;
cout << "The string you entered is :" << str << endl;
}
system("pause");
return 0;
}
請妥善格式化您的問題。 – XDnl 2014-09-27 18:25:01
stdio.h中定義的c中的getchar()函數在C++中運行良好,可逐字符讀取輸入字符。 http://stackoverflow.com/questions/3659109/string-input-using-getchar – 2014-09-27 18:34:24