2017-08-25 38 views
0
cout << "Input text:\n"; 
cin >> tex; 

tex並將其放入任意大小的數組中?(C++)把一個字符串的每個字符串,並把它放入一個數組,並獲得所述數組的大小?

例如,如果我輸入"hello",得到一個數組,它是像

array[x] = {'h', 'e', 'l', 'l', 'o'} 

我會再有一個for循環,對每個字母的東西(我知道該怎麼做),但再怎麼我最終停下來嗎?

+1

如果*串*你的意思是*的std :: string *,那麼你可以使用[的std :: string ::數據()](HTTP:// EN。 cppreference.com/w/cpp/string/basic_string/data)。 –

+3

如果變量'tex'是一個'std :: string'對象,那麼你基本上已經擁有了你想要的東西。你能否詳細說明你的問題?你試圖解決的實際問題是什麼? –

回答

1

爲標準字符串,請在下面使用。

cout << "Input text:\n"; 
cin >> tex; 
for(std::string::iterator it = tex.begin(); it != tex.end(); ++it) { 
    //your work 
} 

for (int i = 0; i < tex.size(); i++){ 
    cout << tex[i]; 
} 
相關問題