分析用户输入c ++

Bad*_*hah 0 c++ char

我想制作程序,分析程序用户编写的文本的每个字母.例如,某人键入"你好".有没有办法将所有这些字母放在某个数组中?例如,myArray [0] = h,myArray [1] = e,...谢谢

chr*_*ris 6

你可以std::string像数组一样使用它,但它是动态的,知道它的大小,并且更加灵活.

//string is like a dynamic array of characters
std::string input; 

//get a whole line of input from stdin and store it
std::getline (std::cin, input); 

//you can manipulate it like an array, among other things
input [0] = 'i'; //now "iello there"
Run Code Online (Sandbox Code Playgroud)

有关完整参考std::string,请参见此处.

  • @ColeJohnson由于遗留原因,字符串文字在C和C++中输入`char*`.它不允许修改它们,因此使用`const char*`指向它们是一个好习惯.`std :: string`是另一双鞋,它是C++特有的,更灵活.如果需要,可以使用`c_str()`从中获取`const char*`. (2认同)