我想从用户那里获取输入字符串,以避免使用gets_s的空格并输出此错误:
char str[] = "", symb;
int count = 0;
cout << "Please enter String: ";
gets_s(str);
cout << "Which character you want to find: ";
symb = getchar();
for (int i = 0; i < strlen(str); i++)
{
if (str[i]==symb)
{
count++;
}
}
cout << "Character: " << symb << " found " << count << " times.";
答案 0 :(得分:0)
在C ++中,常见的字符串输入惯用法涉及std::string
和std::getline
:
string text;
cout << "Please enter String: ";
getline(cin, text);
string
包含搜索方法,例如find
。