string pattern recognition?

This is a discussion on string pattern recognition? within the C++ Programming forums, part of the General Programming Boards category; hi, the user inputs a line of like a hundred characters into one string and i'm using getline. what's the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    84

    string pattern recognition?

    hi,

    the user inputs a line of like a hundred characters into one string and i'm using getline.

    what's the logically simple way to look for 5 letter words?

    currently my program has a for loop inside which contains a large if statement that says:

    Code:
    if ( isspace(line[i-1]) && isalpha(line[i]) && isalpha(line[i+1]) && isalpha(line[i+2]) && isalpha(line[i+3]) && isalpha(line[i+4]) && isspace(line[i+5]) )
    i just think that there might be a simpler string member function that would serve the same purpose if not handle it better.

    thanks,
    barneygumble742
    Last edited by barneygumble742; 07-16-2005 at 10:00 PM.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Uh, right. And what exactly is he supposed to do with this function, search for every possible five-character word, with spaces on both sides? And what good does searching for "of" do in the string "Professor Hoffman offs lofty offers."?

    I would go with something along the lines of:

    Code:
    int last_nonalph = -1;
    int e = str.size() + 1;
    for (int i = 0; i < e; ++i) {
        if (i == e  ||  !isalpha(str[i])) {
            if (i - last_nonalph == 6) {
                /* Do whatever you want with the range (last_nonalph + 1 .. i - 1) */
            }
            last_nonalph = i;
        }
    }
    Last edited by Rashakil Fol; 07-16-2005 at 10:50 PM.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    It has nothing to do with the problem at hand.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    if by 5 letter words you mean words that have 5 characters, all seperated by spaces, then you could use the strtok() function in a loop

    http://www.cppreference.com/stdstring/strtok.html

    note that won't work on lines like: "wow!! later" because that will count as two 5-letter words. what you could do, is after you find where the 5-letter word wannabe is, you could test each character with isalpha()

    http://www.cppreference.com/stdstring/isalpha.html

    that still doesn't guarantee that it's a real word, but it makes sure that it contains 5 characters from the alphabet (A-z).
    Last edited by major_small; 07-16-2005 at 11:01 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i worked on several options, cause this interested me
    and the code i choose to use at the end is this,
    and it account for punctuation during a word
    and at the end of the sentence.

    Code:
    #include <iostream>
    #include <ctype.h>
    #include <string>
    #include <sstream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
    	string mystring;
    	string mystring2;
    	string size;
    	vector<string> tokens;
    	vector<string>::iterator forward;
    	cout << "Enter a sentence : ";
    	getline(cin, mystring, '\n');
    	stringstream ss(mystring);
    	while(ss >> mystring2)
    	{
    		tokens.push_back(mystring2);
    	}
    	int punct = 0;
    	for(forward = tokens.begin(); forward != tokens.end(); forward++)
    	{
    		size = *(forward);
    		for(int i = 0; i != size.size()-1; i++)
    		{
    			if(ispunct(size[i]))
    			{
    				punct++;
    			}
    		}
    		if(ispunct(size[size.size()-1]))
    		{
    			size.erase(size.size()-1, 1);
    		}
    		if((size.size()-punct) == 5)
    		{
    			cout << size << endl;
    		}
    		punct = 0;
    	}
    	cin.get();
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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