Thread: Checking words in a string.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Checking words in a string.

    Hi,

    So an exercise I have is to read each line from a input file to determine if an arbitrary words is used in each of the lines. I have read the file in and used getline to store each line in a string. However, Im not sure how to then loop through the string to see if the word is there? Do I have to break the string down and store in a vector? The code so far is:

    Code:
    cout << "Enter file name: ";
    	string name;
    	cin >> name;
    
    	ifstream ist(name.c_str());
    
    	cout << "Enter a word to search for: ";
    	string word;
    	cin >> word;
    
    	
    	string s;
    
    	while(!ist.eof()){
    		getline(ist,s);
    		
    		
    		
    	}
    Thanks.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I think i've found it now. Should I be using the find member function. Such as s.find(word)?

    Thanks.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by darren78 View Post
    I think i've found it now. Should I be using the find member function. Such as s.find(word)?

    Thanks.
    Nope, thats not working. Any suggestions?

    Thanks.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    find should work. Also note that you should always use getline. Do not mix cin >> and getline. It will only bring trouble.
    I suggest you post your attempt with find if you can't get it to work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    find should work. Also note that you should always use getline. Do not mix cin >> and getline. It will only bring trouble.
    I suggest you post your attempt with find if you can't get it to work.
    So when needing input from just the console, should I also use getline?

    The code for find is as follows

    Code:
    string s;
    	int counter = 0;
    
    	while(!ist.eof()){
    		getline(ist,s);
    
    		if(s.find(word))
    			cout << word << " has been found on line " << counter
    			<< ": " << s << endl;
    
    		++counter;	
    		
    	}
    The output is not what I am expecting. There are 3 lines in the file, with the word I am looking for only on the first line. Instead it disregards my first line completely, then says that it has been found on lines 2 and 3 and then prints two blank "has been found on line"?

    Any suggestions?

    Thanks again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you look up the function find in your local C++ reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    I suggest you look up the function find in your local C++ reference.
    I've done as you suggested and it now works.

    Code:
    string::size_type index = s.find(word);
    		if(index != string::npos)
    			cout << s.substr (index) << endl;
    Thanks.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    9
    Quote Originally Posted by darren78 View Post
    So when needing input from just the console, should I also use getline?
    cin.getline -- used with c-style character arrays
    getline(cin ...) used with c++ std::string.
    cin -- used with int's, double's, chars,etc. Security Issue (Allows for buffer over-run - no delimiter checking).

    If possible, use cin.getline(VAR, DELIMITER) where 'var' is where to store the input, and DELIMITER is where/when to stop - such as '\n' or 10.

    [s]I wish I could provide a resolution to the other issues[/s] Good job Elysia
    Last edited by camelCase; 07-02-2010 at 05:21 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I've never found any real good use for cin >>. Now, string streams >> operator can be rather handy at times, however, when extracting data from a string.
    If possible, use std::getline to avoid C-style strings. It also supports the delimiter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by darren78 View Post
    I've done as you suggested and it now works.

    Code:
    string::size_type index = s.find(word);
    		if(index != string::npos)
    			cout << s.substr (index) << endl;
    Thanks.
    Can I just check with you that I fully understand what this is doing. Am I correct in thinking that the size_type index, will (if the word is there) store where the word starts and if its not found it has the value end of string?
    Then by checking that the index isnt equal to end of string (npos), it prints out the line? I dont understand what s.substr(index) does though. I mean, I know that its printing the line, but how?

    Thanks.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, find returns the index where the string you're looking for begins, and if not found, it returns std::string::npos. So you check against this to see if it found it or not.
    std::string::substr is a function that returns a substring, ie a part of another string. It takes two arguments, the first of which indicates the start position of the substring. Since you are specifying the second parameter, it takes everything in the string from the index position and returns it.

    For example:
    Code:
    std::string str("Hello World!");
    auto index = str.find("World!"); // C++0x feature
    std::cout << str.substr(index); // Prints out "World!".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks, thats crystal clear now. And thanks CC for your help too.

    I'm going to be starting a 12 month placement in 8 weeks so doing some intensive cramming now ready for it. So I may be on quite a bit over the next couple of weeks

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    9
    Quote Originally Posted by darren78 View Post
    Thanks, thats crystal clear now. And thanks CC for your help too.

    I'm going to be starting a 12 month placement in 8 weeks so doing some intensive cramming now ready for it. So I may be on quite a bit over the next couple of weeks
    Check up on the C++ book recommendations (I suggest C++ Without fear && || Sams Teach Yourself C++ in an hour a day). First work on general or procedural based programming, then move towards structured programming and then of course object oriented programming (OOP).

    Incase your wondering:

    -Procedural programming: The code is listed 'as is' in the fashion that it will be run. This code is listed from a-z (a will be run first, z will be run last).

    -Structured programming: Splitting apart your code into more manageable piece of code, usually by taking chunks of procedure based code and putting it into reasonable functions.

    -Object orientated programming: An 'object' way of performing tasks. You may go from a to c, to f to z, then back to a. In OOP you enforce the rule of usability and DRY methods. Dry stands for 'do not repeat yourself'.

    I've turned this into a lecture :P

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest Accelerated C++ because it simply teaches C++ first and the C parts of C++ later. Simply put, it is the best novice book I know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM