Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. String Search In C++

String Search In C++

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialc++algorithmsregex
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hack Baba
    wrote on last edited by
    #1

    Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.

    void search()
    {

    char str2[100];

    char str[100];

    cout<<"write some text to search:";

    gets(str);

    ifstream i;

    i.open("stud.txt");

    while(!i.eof())
    {
    i>>str2;

    if(stricmp(str,str2)==0)
    
     cout<<"found !";
    
    else
    
     cout<<"not found !";
    

    }

    getch();

    }

    J L J D 4 Replies Last reply
    0
    • H Hack Baba

      Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.

      void search()
      {

      char str2[100];

      char str[100];

      cout<<"write some text to search:";

      gets(str);

      ifstream i;

      i.open("stud.txt");

      while(!i.eof())
      {
      i>>str2;

      if(stricmp(str,str2)==0)
      
       cout<<"found !";
      
      else
      
       cout<<"not found !";
      

      }

      getch();

      }

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      Edit your post and add what (the code) you have tried.

      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

      H 1 Reply Last reply
      0
      • J jeron1

        Edit your post and add what (the code) you have tried.

        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

        H Offline
        H Offline
        Hack Baba
        wrote on last edited by
        #3

        Thanks For Your help in the previous question bro but it didn't worked. I already put the comparison into the while loop as you said but it's still searching for the last word. I am stuck at this point bro i don't know what to do. I also have to move to other parts of the program and complete it but i am stuck at this major point.

        J 1 Reply Last reply
        0
        • H Hack Baba

          Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.

          void search()
          {

          char str2[100];

          char str[100];

          cout<<"write some text to search:";

          gets(str);

          ifstream i;

          i.open("stud.txt");

          while(!i.eof())
          {
          i>>str2;

          if(stricmp(str,str2)==0)
          
           cout<<"found !";
          
          else
          
           cout<<"not found !";
          

          }

          getch();

          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I suspect that the problem is that you are using i>>str2; where str2 is defined as a character array. If you add a print command after that line you can see exactly what is returned by the >> operator. You should use a string type to get each word. Alternatively use ordinary C library functions to read each line and tokenise into words.

          H 1 Reply Last reply
          0
          • L Lost User

            I suspect that the problem is that you are using i>>str2; where str2 is defined as a character array. If you add a print command after that line you can see exactly what is returned by the >> operator. You should use a string type to get each word. Alternatively use ordinary C library functions to read each line and tokenise into words.

            H Offline
            H Offline
            Hack Baba
            wrote on last edited by
            #5

            Thanks for taking time to reply to me bro. Now, I would if i could what you told me but the problem is that i HAVE to make this project in Turbo C++ only and i know that this is ancient and i will go to hell for using it but plz try to help me with this project with the turbo Compiler

            L 1 Reply Last reply
            0
            • H Hack Baba

              Thanks For Your help in the previous question bro but it didn't worked. I already put the comparison into the while loop as you said but it's still searching for the last word. I am stuck at this point bro i don't know what to do. I also have to move to other parts of the program and complete it but i am stuck at this major point.

              J Offline
              J Offline
              jeron1
              wrote on last edited by
              #6

              How about having a boolean variable, something like..

              bool FoundIt = false; //declare and initialize
              .
              .
              .
              while(!i.eof())
              {
              i>>str2;

              if(stricmp(str,str2)==0)   //match?
              {
                 FoundIt = true;     // yes, set our new boolean variable
                 break;              // get out of while loop
              }
              

              }

              if (FoundIt)
              cout<<"found ! \n";
              else
              cout<<"not found ! \n";

              "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

              1 Reply Last reply
              0
              • H Hack Baba

                Thanks for taking time to reply to me bro. Now, I would if i could what you told me but the problem is that i HAVE to make this project in Turbo C++ only and i know that this is ancient and i will go to hell for using it but plz try to help me with this project with the turbo Compiler

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                It makes little difference which compiler you use. You still need to list out the strings you are reading from the file to see whether they match with the one you have typed in.

                1 Reply Last reply
                0
                • H Hack Baba

                  Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.

                  void search()
                  {

                  char str2[100];

                  char str[100];

                  cout<<"write some text to search:";

                  gets(str);

                  ifstream i;

                  i.open("stud.txt");

                  while(!i.eof())
                  {
                  i>>str2;

                  if(stricmp(str,str2)==0)
                  
                   cout<<"found !";
                  
                  else
                  
                   cout<<"not found !";
                  

                  }

                  getch();

                  }

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

                  You need to do the same thing for each file. This means breaking up your function into multiple functions. Hint: the new function would start at ifstream.

                  H 1 Reply Last reply
                  0
                  • J Joe Woodbury

                    You need to do the same thing for each file. This means breaking up your function into multiple functions. Hint: the new function would start at ifstream.

                    H Offline
                    H Offline
                    Hack Baba
                    wrote on last edited by
                    #9

                    I used cout<

                    J 1 Reply Last reply
                    0
                    • H Hack Baba

                      Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.

                      void search()
                      {

                      char str2[100];

                      char str[100];

                      cout<<"write some text to search:";

                      gets(str);

                      ifstream i;

                      i.open("stud.txt");

                      while(!i.eof())
                      {
                      i>>str2;

                      if(stricmp(str,str2)==0)
                      
                       cout<<"found !";
                      
                      else
                      
                       cout<<"not found !";
                      

                      }

                      getch();

                      }

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      Hack Baba wrote:

                      Plz also tell me how to search for the entered word in multiple files.

                      Take your code from the ifstream declaration down and move to a separate function. Create a loop that iterates each filename you wish to process, calling the aforementioned function with each filename.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                      1 Reply Last reply
                      0
                      • H Hack Baba

                        I used cout<

                        J Offline
                        J Offline
                        Joe Woodbury
                        wrote on last edited by
                        #11

                        Think about the difference of using cout inside the loop and after the loop.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups