Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输出句子中单词的第一个字母 我试图用C++来打印一个句子中每个单词的第一个字母。我的想法是先打印字符串的第一个字母,然后在空格后打印每个字母: #include <string.h> #include <stdio.h> #include <iostream> using namespace std; string sentence; int main(int argc, char const *argv[]) { cout << "Input your name!" << endl; cin >> sentence; //output first character cout << sentence[0] << endl; //output the rest of first characters in words for(int i = 0; i < sentence.length(); i++){ if (sentence[i] == ' ' && sentence[i+1]!= '\0'){ cout << sentence[i+1]<< endl; } } return 0; } #包括 #包括 #包括 使用名称空间std; 串句; int main(int argc,char const*argv[] { cout语句; //输出第一个字符 cout_C++_String - Fatal编程技术网

输出句子中单词的第一个字母 我试图用C++来打印一个句子中每个单词的第一个字母。我的想法是先打印字符串的第一个字母,然后在空格后打印每个字母: #include <string.h> #include <stdio.h> #include <iostream> using namespace std; string sentence; int main(int argc, char const *argv[]) { cout << "Input your name!" << endl; cin >> sentence; //output first character cout << sentence[0] << endl; //output the rest of first characters in words for(int i = 0; i < sentence.length(); i++){ if (sentence[i] == ' ' && sentence[i+1]!= '\0'){ cout << sentence[i+1]<< endl; } } return 0; } #包括 #包括 #包括 使用名称空间std; 串句; int main(int argc,char const*argv[] { cout语句; //输出第一个字符 cout

输出句子中单词的第一个字母 我试图用C++来打印一个句子中每个单词的第一个字母。我的想法是先打印字符串的第一个字母,然后在空格后打印每个字母: #include <string.h> #include <stdio.h> #include <iostream> using namespace std; string sentence; int main(int argc, char const *argv[]) { cout << "Input your name!" << endl; cin >> sentence; //output first character cout << sentence[0] << endl; //output the rest of first characters in words for(int i = 0; i < sentence.length(); i++){ if (sentence[i] == ' ' && sentence[i+1]!= '\0'){ cout << sentence[i+1]<< endl; } } return 0; } #包括 #包括 #包括 使用名称空间std; 串句; int main(int argc,char const*argv[] { cout语句; //输出第一个字符 cout,c++,string,C++,String,std::cin将在第一个空格后停止读取字符串。因此,如果您输入hello world,它将只在字符串中读取“hello”。相反,您可以使用std::getline将整行读取到字符串中: cout << "Input your name!" << endl; getline(cin, sentence); ... coutstd::cin将在第一个空格后停止读取字符串。因此,如果您输入hello world,它将只在字符串中读取“hello”。相反,您可以使用std:

std::cin
将在第一个空格后停止读取字符串。因此,如果您输入
hello world
,它将只在字符串中读取
“hello”
。相反,您可以使用
std::getline
将整行读取到字符串中:

cout << "Input your name!" << endl;
getline(cin, sentence);

...

cout
std::cin
将在第一个空格后停止读取字符串。因此,如果您输入
hello world
,它将只在字符串中读取
“hello”
。相反,您可以使用
std::getline
将整行读取到字符串中:

cout << "Input your name!" << endl;
getline(cin, sentence);

...

cout
std::cin
将在第一个空格后停止读取字符串。因此,如果您输入
hello world
,它将只在字符串中读取
“hello”
。相反,您可以使用
std::getline
将整行读取到字符串中:

cout << "Input your name!" << endl;
getline(cin, sentence);

...

cout
std::cin
将在第一个空格后停止读取字符串。因此,如果您输入
hello world
,它将只在字符串中读取
“hello”
。相反,您可以使用
std::getline
将整行读取到字符串中:

cout << "Input your name!" << endl;
getline(cin, sentence);

...

cout std::string中没有“\0”。如果使用
.at(),尝试访问
i+1
元素将导致
越界
错误
谢谢sudomakeinstall2的评论!我会记住std::string中没有“\0”。如果使用
.at(),尝试访问
I+1
元素将导致
越界
错误
谢谢sudomakeinstall2的评论!我会记住std::string中没有“\0”。如果使用
.at(),尝试访问
I+1
元素将导致
越界
错误
谢谢sudomakeinstall2的评论!我会记住std::string中没有“\0”。如果使用
.at(),尝试访问
I+1
元素将导致
越界
错误
谢谢sudomakeinstall2的评论!我会记住的谢谢你Xymostech!现在更有意义:)谢谢你Xymostech!现在更有意义:)谢谢你Xymostech!现在更有意义:)谢谢你Xymostech!现在更有意义:)谢谢你Xymostech!现在更有意义:)