2016-10-17 3 views
0

を考慮していません全体の文章。 私は私が「IamDoingveryfine」 助けてくださいを取得行う必要がある変更内容のプットは私が文からスペースを削除する機能を書かれているが、そのだけ私が文からスペースを削除する機能を書かれたが、その空間のみ&前に文字を取って検討していないスペースの前に文字を取っ&全体の一文に含ま

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

でこれを実行すると、以下のように、コードがある...」

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 :: cinの代わりに' std :: getline() 'を実行しているはずですので、 >> 'を読んでください。あなたの' || 'は' && 'でなければなりません。 –

+0

すべての文字は、スペースとタブの少なくとも1つとは異なります。 「文字がスペースではなく、文字がタブでない場合は、」「の文字がスペースまたはタブでない場合は、」ロジックに変換されます。 – 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、名前)を使用しました。 これで入力の問題が解決されます。しかし、トリム機能でイムをチェックするにもかかわらず、「」&「\ t」のイム空白をブロックすることができません。

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' の)機能を使用しながら、スペースを捨てることができませんでした。なぜこの違いがあるのか​​教えてください。

関連する問題