如何用空格和换行符输​​入字符串(\ t和\ r \ n)

时间:2014-05-22 13:23:36

标签: c++ string newline

我正在处理http://www.spoj.com/problems/WORDCNT/,我已经提出了以下代码。 我觉得我的代码运行正常,但是在输入时存在一些问题。

我正在使用getline(cin,string);

#include <iostream>
#include <string>
using namespace std;

int main ()
{ 
    int i,j,test;
    cin>>test;
    cin.ignore();
    while(test--)
    {
        int d=0,c=0,big=0,cons=1,k=0,t=0;

        string name,temp;

        getline (cin,name);
        name=name+" ";

        for(i=0;i<name.length();i++)
        {
            if(name[i]!=' ')
            {
                t=1;
                ++c;
            }
            else
            {
                ++d;
                if(d==1)
                { 
                    k=c;
                    c=0;
                }

                else
                {
                    if(c==k)
                    {
                        cons++;
                        c=0;
                        if(cons>big)
                        {
                            big=cons;
                        }
                    }
                    else
                    {
                        k=c;
                        c=0;
                        cons=1;
                        if(cons>big)
                        {
                            big=cons;
                        }
                    }
                }
            }
        }
        if( (big==0) && (t==1))//to check for blank and single character string
        {
            big=1;
        }
        cout<<big<<"\n";
    }
    return 0;
}

0 个答案:

没有答案