Thread: C++ Exercise Problem: Find word in text that the user has entered

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question C++ Exercise Problem: Find word in text that the user has entered

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std; 
    
    
    int main() {
    	
    	string text, ord;
    	cout << "Type a sentence: ";
    	getline(cin, text);
    	
    	cout << "Type a word: ";
    	getline(cin, ord);
    	
    	// Find the first letter in the word 
    	int pos = text.find(ord.at(0), 0);
    	
    	// Find the last letter in the word
    	int ord_langd = ord.length();
    	
    	string ordet;
    	
    	// Get word
    	ordet = text.substr(pos, ord_langd);
    	
    	// Compare the text sentence to see
            // if the word the user entered exists in it
    	// This is the problem that I struggle with. 
    	if () { // I can't complete the if statement
    		cout << "Word does not exist" << endl;
    
    
    	}
    	else {
    		cout << "Index: " << ord.at(0) << " " << pos << endl;
    		cout << "Word: " << ordet << endl;
    	}
    	
    	return 0;
    }
    I want the user to be able to input a full sentence like "How are you" and then the user is asked to enter a word "are". If the word "are" is in the sentence then the program should tell the user. If the word "are" isn't in the sentence then the program should tell the user that the word doesn't exist within the string.

    Example:

    Type a sentence: How are you
    Type a word: are
    The word "are" exists in your sentence

    Example:
    Type a sentence: How are you
    Type a word: bye
    The word "bye" doesn't exist in your sentence

    I hope I'm clear enough so that you get what I mean. Otherwise ask me to clarify.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are the possible return values for text.substr(pos, ord_langd)?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Guest
    Guest
    Unless your exercise demands it, I think it would be easiest if you searched the text string for the ord string directly. It doesn't check for word boundaries though. Maybe you could just surround ord with spaces prior to checking.
    Code:
    auto result = text.find(ord);
    std::string::find returns the position if there is a match and std::string::npos otherwise. So you could for instance create an if condition to compare result with that "no match" value. I'll leave that to you
    Last edited by Guest; 09-30-2015 at 10:57 AM.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Well, According to my exercise and according to Chapter 4 - Strings in my text book. We've only seen the string functions:

    find(word, start)
    substr(start, end)
    at(index)
    length(), size()

    So I don't think I'm allowed to use:

    Code:
    auto int result = text.find(ord);
    Since the text book have only covered these string functions and
    actually nothing about the keyword auto.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DecoratorFawn82
    We've only seen the string functions:

    find(word, start)
    substr(start, end)
    at(index)
    length(), size()
    find(word) is equivalent to find(word, 0).

    And uh, I zeroed in on the wrong part of your code in my previous post as I did not expect that you would have a different approach. My point is that "std::string::find returns the position if there is a match and std::string::npos otherwise."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Guest
    Guest
    Quote Originally Posted by DecoratorFawn82 View Post
    (...)
    Since the text book have only covered these string functions and
    actually nothing about the keyword auto.
    Yah ok, that's fair enough. So from what I understand, you:
    1. Try to find the first letter of ord in text and store that location in pos.
    2. Get a substring (ordet) from text with the length of ort, starting at pos.
    3. Compare ordet to ort to see if they are actually the same word and don't just share the same first and last letter.

    If that's what you want to do, then you can just use the == operator to compare those two strings, the same way you would e.g. compare two integers.

    Note that you should generally use 64-bit unsigned integers to handle container positions. Using int could lead to unexpected behaviour later on.
    Code:
    size_t pos = text.find(ord.at(0)); // size_t can hold any value find() returns, int cannot
    size_t ord_langd = ord.length();
    As a matter of style, I would advise to initialize your variables directly if possible.
    Code:
    int ord_langd = ord.length();
    string ordet = text.substr(pos, ord_langd); // we assign to ordet on declaration
    You may also notice that ord_langd gets used immediately, so you could get rid of it unless it hurts clarity.
    Code:
    string ordet = text.substr(pos, ord.length());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find word and insert another word after that in text
    By xiromdim in forum C Programming
    Replies: 8
    Last Post: 12-20-2013, 11:54 AM
  2. Replies: 3
    Last Post: 05-02-2011, 11:43 PM
  3. How to find the longest word in text file?
    By alionas in forum C Programming
    Replies: 7
    Last Post: 03-07-2011, 01:05 PM
  4. find word in a text file
    By 26friends26 in forum C++ Programming
    Replies: 7
    Last Post: 03-01-2010, 09:37 PM
  5. Replies: 1
    Last Post: 09-01-2001, 10:33 AM