2017-08-25 22 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]; 
} 
相关问题