Thread: string pattern recognition?

  1. #1
    Registered User
    Join Date
    Sep 2004

    string pattern recognition?


    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:

    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.

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

  2. #2
    Join Date
    Jul 2005
    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:

    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
    Join Date
    Jul 2005
    It has nothing to do with the problem at hand.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    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

    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()

    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
    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
    Join Date
    Jun 2005
    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.

    #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)
    	int punct = 0;
    	for(forward = tokens.begin(); forward != tokens.end(); forward++)
    		size = *(forward);
    		for(int i = 0; i != size.size()-1; i++)
    			size.erase(size.size()-1, 1);
    		if((size.size()-punct) == 5)
    			cout << size << endl;
    		punct = 0;
    	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, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM