Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.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++ 计算由字母、\n、\t和空格字符组成的字符串中的单词_C++_String - Fatal编程技术网

C++ 计算由字母、\n、\t和空格字符组成的字符串中的单词

C++ 计算由字母、\n、\t和空格字符组成的字符串中的单词,c++,string,C++,String,给定由空格、\t、\n和字母组成的字符串,任务是计算空格、\t和\n用作分隔符的字数。 对于这个问题,我编写了以下代码,它不能正常工作,为什么 #include <iostream> #include <string> #include <cstring> using namespace std; bool isalpha(char ch) { if((ch>='A'&& ch<='Z')||(ch>='a'&

给定由空格、\t、\n和字母组成的字符串,任务是计算空格、\t和\n用作分隔符的字数。 对于这个问题,我编写了以下代码,它不能正常工作,为什么

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool isalpha(char ch)
{
    if((ch>='A'&& ch<='Z')||(ch>='a'&& ch<='z'))
       return true;
    return false;
}
int main() 
{
    int t;
    cin>>t;
    while(t--)
    {
        string str;
        int count=0;
        getline(cin,str);
        for(int i=0;i<str.length();i++)
        {
            if(isalpha(str[i]))
            {
                count++;
                while(str[i]!='\n'||str[i]!='\t'||str[i]!=' ')
                {
                    i++;
                }
            }
            else
               continue;
        }
        cout<<count<<endl;
    }
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
布尔·伊萨尔法(char-ch)
{
如果((ch>='A'&&ch='A'&&ch>t;
而(t--)
{
字符串str;
整数计数=0;
getline(cin,str);

对于(int i=0;i您为什么要重新实现?至于您的问题,我建议您花一些时间阅读Eric Lippert的文章。1.2017年6月29日<代码>是否算作一个单词?2.您将单词的计数存储在哪里?在<代码>i中?它只有字母、\n、\t和空格作为输入,计数存储在变量countSo中。1.如果输入是由字母组成的,则只有你为什么叫伊莎尔法?