字符串查找未找到预期的子字符串

时间:2015-10-13 19:56:37

标签: c++ arrays

我正在创建一个代码,让用户可以从菜单中选择他们想要执行的操作。当我运行我的代码时,我选择了选择' E'我输入了我想要搜索的单词。结果是这个词不在句子中,即使它是。有什么理由吗?提前谢谢

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;
int main()
{

    string s;
    char selection;
    string w;

    cout << "Enter a paragraph or a sentence : " ;

    getline(cin, s);

    int sizeOfString = s.length(); 

    //cout << "The paragraph has " << sizeOfString << " characters. " << endl; ***Dummy call to see if size works. 

    //cout << "You entered " << s << endl; *** Dummy function !!

    cout << "" << endl;

    cout << "                 Menu          " << endl;
    cout <<"        ------------------------" << endl;
    cout << "" << endl;
    cout << "A -- Convert paragraph to all caps " << endl;
    cout << "B -- Convert paragraph to all lowercase " << endl;
    cout << "C -- Delete whitespaces " << endl;
    cout << "D -- Split words & remove duplicates " << endl;
    cout << "E -- Search a certain word " << endl;
    cout << "" << endl;
    cout << "Please select one of the above: " ;
    cin >> selection;
    cout << "" << endl;
    switch (selection) //Switch statement
    {
        case 'a':
        case 'A': cout << "You chose to convert the paragraph to all uppercase" << endl;
                  cout << "" << endl;
                  for(int i=0; s[i]!='\0'; i++)
                    {
                        s[i]=toupper(s[i]);
                    }
                    cout << "This is it: " << s << endl;
                  break;
        case 'b':
        case 'B': cout << "You chose to convert the paragragh to all lowercase" << endl;
                  cout << "" << endl;
                  for (int i=0; s[i] !='\0'; i++)
                  {
                      s[i]=tolower(s[i]);
                  }
                    cout << "This is it: " << s << endl;
                    break;
        case 'c':
        case 'C': cout << "You chose to delete the whitespaces in the paragraph" << endl;
                  cout << "" << endl;
                  for(int i=0; i<s.length(); i++)
                    if(s[i] == ' ') s.erase(i,1);
                  cout <<"This is it: " << s << endl;
                  break;
        case 'd':
        case 'D': cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
                  cout << "" << endl;

                  /*char arrayOne[] = s;
                    for (int i=0; i< s.length; i++)
                    {
                        cout << arrayOne[i] << endl;
                    }*/


        case 'e':
        case 'E': cout << "You chose to search for a certain word in the paragraph. " << endl;
                  cout << "" << endl;
                  cout << "Enter the word you want to search for: ";
                  cin >> w;

                  s.find(w);
                  if (s.find(w) == true)
                  {
                      cout << w << " was found in the paragraph. " << endl;

                  }
                  else if (s.find(w) != true);
                  {
                        cout << w << " was not found in the paragraph. " << endl;
                  }




    }


    return 0;
}

3 个答案:

答案 0 :(得分:0)

你的问题在于

if (s.find(w) == true)

find()返回找到的字符串的位置,不是true或false。如果你想检查它是否找到单词use

if (s.find(w) != std::string::npos)

答案 1 :(得分:0)

std::string::find返回子字符串位置,而不是布尔值。检查the documentation

如果等于std::string::npos,则找不到实例,否则,如果(大于或等于零)找到它。

所以:

if ( s.find( w ) )

应该是:

if ( s.find( w ) != std::string::npos )

顺便说一下:

s.find(w);
if (s.find(w) == true)
{
    cout << w << " was found in the paragraph. " << endl;
}
else if (s.find(w) != true);
{
    cout << w << " was not found in the paragraph. " << endl;
}

应该是:

if ( s.find( w ) != std::string::npos )
    cout << w << " was found in the paragraph. " << endl;
else // no need to test again!
    cout << w << " was not found in the paragraph. " << endl;

答案 2 :(得分:0)

我想建议以下内容来简化您的开发/测试工作:

bool DEVELOPEMENT_MODE = true;


int t247(void)
{
   std::string s;
   char selection;
   std::string w;

   std::cout << "Enter a paragraph or a sentence : " ;

   // instead of ONLY run-time fetch of paragraph or sentence
   if(DEVELOPEMENT_MODE)
   {
      // for developement use:
      std::stringstream ss;

      // TEST STRING
      ss << "the quick brown fox jumped over the lazy dog";

      s = ss.str();  // fill in test input

      size_t sizeOfString = s.length();        // note: not int

      // ***Dummy unit test to see if size works.
      std::cout << "The paragraph has " << sizeOfString 
                << " characters. " << std::endl; 

      // ***Dummy unit test
      std::cout << "You entered " << s 
                << std::endl;          
   }
   else
   {
      // for final test use run time entry of sentence
      getline(std::cin, s);
   }

   //...

当您使每个函数都正常工作时,请为测试字符串添加更多复杂性(使用您通常使用的编辑器)。

我经常使用argc或argv [1](main)来选择要运行的测试。

更新:

顺便说一下,你会发现很难用getline()读取一个没有循环的段落。

但是,DEVELOPMENT_MODE代码(使用stringstream)可以轻松创建一个段落。

 ss << "the quick brown fox jumped over the lazy dog";

可以轻松扩展... c ++自动神奇地将行连接成一行:

 ss << "the quick brown fox jumped over the lazy dog"
       "quick brown fox jumped over the lazy dog the"
       "brown fox jumped over the lazy dog the brown";

但上面还不是段落。

以下是3行段落:

 ss << "the quick brown fox jumped over the lazy dog\n"
       "quick brown fox jumped over the lazy dog the\n"
       "brown fox jumped over the lazy dog the brown\n";