Finding similar words from 2 strings

This is a discussion on Finding similar words from 2 strings within the C++ Programming forums, part of the General Programming Boards category; Hi all C++ gurus help is desperately needed here... basically i need to capture 2 user inputs for example sentence1: ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    10

    Finding similar words from 2 strings

    Hi all C++ gurus
    help is desperately needed here...
    basically i need to capture 2 user inputs
    for example

    sentence1: man
    sentence2: this is a man

    sentence 1 is part of sentence2

    sentence1: boy
    sentence2: this is a man

    sentence 1 is not part of sentence 2


    i need to use a while loop (end of sentence 2 or match found)
    as well as a if loop to compare first char of sentence 1 to any match char in sentence 2
    then use a while loop to test the rest of string 1 string2
    i came up with the below code but somehow the loop doesn't work..it will always say sentence is not part of sentence 2 even though i type 2 same words

    please help!!
    ================================================== ==========================================

    Code:
    #include <vcl.h>
    #include "MT262io.h"
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    
    AnsiString first;
    AnsiString second;
    bool flag = false;
    int counter;
    int count;
    
    
    first = ReadStringPr("Enter 1st sentence: ");
    second = ReadStringPr("Enter 2nd sentence: ");
    
    int y = Length (first);
    int x = Length(second);
    
    while (count == x) //loop through 2nd string
                    {
    
                        if ( first[1] == second [counter]) //to check if first letter of first string matches any char in 2nd string
                            {
                                counter = counter +1;
    
                                 while ( first[2] == second[counter] )
                                 {
                                    flag ==true;
                                    WriteStringCr (" 1st sentence is part of 2nd sentence");
    
                                 }
                             }
    
    
    
    
    }
    
            WriteStringCr("1st sentence is not part of second sentence" );
    
    
    
    
            getchar();
            return 0;
    }

  2. #2
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    my first guess is that your loop will mostly never work because you never initialized your count variable. what are the chances of it being equal to x before the loop starts? might as well play the lottery instead... you might win something
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    10
    thanks for the reply
    i m new to C++

    i m not sure with how the while sentence shd be protrayed...
    so shd i be doing this??

    int count = 1;

    while (count<= x) // loop through sentence 2 or when match found)

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    If you aren't required to use the method that you're using (ie. it's for a class and the assignment calls for it), then consider using the std::string class (though I think AnsiString has the same function) and its find() function

    Code:
    std::string foo = "This is a man";
    std::string bar = "man";
    std::string baz = "boy";
    
    if(foo.find(bar) != std::string::npos)
       std::cout << bar << " is in the string \"" << foo << "\""
                 << std::endl;
    else
       std::cout << bar << " is not in the string \"" << foo << "\""
                 << std::endl;
    if(foo.find(baz) != std::string::npos)
       std::cout << baz << " is in the string \"" << foo << "\""
                 << std::endl;
    else
       std::cout << baz << " is not in the string \"" << foo << "\""
                 << std::endl;
    Either way, converting from AnsiString to String and back is a fairly simple procedure.
    Last edited by SlyMaelstrom; 02-24-2006 at 11:36 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    10
    thank you so much sly

    yup i used the method of yours but becoz we r tested on using loops
    there i have to compare char instead of using the std::string class which is so obvious so much easier...

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Also, if you had to maintain your AnsiString objects as are, you could perform the same operation relatively easy with the SubString() function.

    Code:
    AnsiString foo = "This is a man";
    AnsiString bar = "man";
    
    int i, found = 0;
    for (i = 0; i < foo.Length() - bar.Length(); i++) {
       if (foo.SubString(i, bar.Length() == bar) {
          std::cout << bar << " is in the string \"" << foo << "\""
                    << std::endl;
          found++;
       }
    }
       if (found == 0)
          std::cout << bar << " is not in the string \"" << foo << "\""
                    << std::endl;
    This is provided AnsiString doesn't have a find function, which I think it might.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    10
    anyone??

    still stuck at the while loop i think...
    i mean how to initialise it so that it reads till the end of setence 2??

  8. #8
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    think about your count variable. what does it count up to? it should count 'til the size of the second string (or i'd even count up to the size of the second string minus the size of the first string because what good is it to search for a word starting 3 characters from the end of string two when the word you're looking for is 8 characters?) so while count is less than size of string 2 minus size of string 1, loop. see if you can make any progress from there
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  2. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM
  4. using strlen and finding shortest and longest words
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-30-2001, 06:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21