2016-10-17 64 views
0

我写了一个功能空间&之前删除从一个句子的空间,但其只考虑字符不考虑整个句子。 如果我跑了这一点卖出期权我已经写了一个函数从一个句子中的空格,但其只考虑字符的空间之前和不考虑整个森泰斯

Enter the string with space/ tab to trim 
IamDoing very fine 
Length of the String is : 8 
IamDoing 

我什么样的变化,我需要做得到“IamDoingveryfine” 请帮帮忙,代码如下......”

void removeSpace(const string &str) 
{ 
    string tempStr; 
    string p_str = str; 
    int l_length = p_str.length(); 
    cout<<"Length of the String is : "<<l_length<<endl; 
    for (int i=0; i<l_length; i++) 
    { 
     if(p_str[i] != ' ' || p_str[i] != '\t') 
     { 
      tempStr +=p_str[i]; //to add chars into tempstr 
     } 
    } 
    cout<<tempStr<<'\n'; 
    return ; 
} 
+0

请你能张贴在那里你字符串中读取的代码,你应该做'的std ::函数getline()'取代'的std :: CIN >>读入。而你的'||'应该是'&&'。 –

+0

所有字符与空格和制表符中的至少一个不同。 “如果字符不是空格或制表符”,则将其翻译为“如果字符不是空格且字符不是制表符”。 – molbdnilo

+0

德摩根定理:“如果它是一个空格或制表符,则将其删除”与“如果它不是空格**和**而不是制表符”保持一致。这就是说,这是一个容易犯的错误。 –

回答

1

的问题是不该removespace()问题,当你正在服用的输入

它应该是这样的:

std::string line; 
std::cout << "Enter the string with space/ tab to trim" << std::endl; 
std::cin.getline (line); 
0

我用 std :: getline(std :: cin,name); 输入问题与此解决。但在修剪功能,即使即时检查'''&''吨'我不能够阻止白色空间。

removeSpace应该丢失一些东西。

+0

编辑您的问题,请勿将更新发布为答案。 –

0

我修改函数来获得预期的答案如下

void removeSpace(const string &str) 
{ 
    string tempStr; 
    string p_str = str; 
    int l_length = p_str.length(); 
    cout<<"Length of the String is : "<<l_length<<endl; 
    for (int i=0; i<l_length; i++) 
    { 
     if((p_str[i] == ' ') || (p_str[i] == '\t')) 
     { 
      continue; 
     } 
     else 
     { 
      cout<<p_str[i]<<endl; 
      tempStr +=p_str[i]; //to add chars into tempstr 
     } 
    } 
    cout<<tempStr<<'\n'; 
    return ; 
} 

但是当我用IF(p_str [I]!= '' || p_str [I]!= '\ t')函数无法丢弃的空间,任何人可以告诉我为什么这种差异

相关问题