partially checking a string against a dictionary

This is a discussion on partially checking a string against a dictionary within the C++ Programming forums, part of the General Programming Boards category; hey guys, any suggestions on how to check a strings against a dictionary. To check the whole string I got, ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    partially checking a string against a dictionary

    hey guys, any suggestions on how to check a strings against a dictionary. To check the whole string I got, but lets say that the string has five characters (T O A D N) and is ended with a '\0', only the first four make a word. The code that I'm in the process of figuring out is below. It returns the word fine, as long as it is a value of the total string. Also, there are a maximum of characters in each string.

    Code:
    void matchWord(char word1[], char word2[], char dictionary[][MAX_LENGTH + 1])
    {
    	int i;
    	int j;
    	if ( word1[2] == '\0')
    	{
    		for ( i=0; i<7000; i++) 
    		{
    			if ( strcmp( word1, dictionary[i]) == 0 )
    			{
    				 cout << endl << word1 << endl;
    				 break;      // quit looking
    			}
    		}
    	}
    	else if( word1[3] == '\0')
    	{
    		for ( i=0; i<7000; i++) 
    		{
    			if ( strcmp( word1, dictionary[i]) == 0 )
    			{
    				 cout << endl << word1 << endl;
    				 break;      // quit looking
    			}
    		}
    	}
    	else if ( word1[4] == '\0')
    	{
    		for ( i=0; i<7000; i++) 
    		{
    			if ( strcmp( word1, dictionary[i]) == 0 )
    			{
    				 cout << endl << word1 << endl;
    				 break;      // quit looking
    			}
    		}
    	}
    	else if ( word1[5] == '\0')
    	{
    		for ( i=0; i<7000; i++) 
    		{
    			if ( strcmp( word1, dictionary[i]) == 0 )
    			{
    				 cout << endl << word1 << endl;
    				 break;      // quit looking
    			}
    			word1[4] == '\0';
    			if ( strcmp( word1, dictionary[i]) == 0 )
    			{
    				 cout << endl << word1 << endl;
    				 break;      // quit looking
    			}
    
    		}
    	}
    }
    the dictionary is stored in a 2D array. any suggestions?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I would pass my strings into your function as std::string types. If you store your dictionary as an array of strings (rather than as a 2D array of chars) it will simplify memory management and avoid the need for a maximum word length.

    I'm not sure what the word2 argument in function is for, but I presume it is to return the result. If you were to use the string class, you could return the result directly. So your function prototype may look like this:

    Code:
    std::string matchWord(const std::string& word1, std::string* dictionary)
    In order to compare partial string against those in the dictionary, you could use the find method of string. Something like (DICT_SIZE is the number of strings in the dictionary array):

    Code:
    for(int n =0; n < DICT_SIZE; n++)
      if (word1.find( dictionary[n]) == 0)
        return dictionary[n];
    
    // Nothing found
    return "";

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    I believe that .find has something to do with classes, and I haven't got that far yet in c++. I bet it will makes sense to me in a couple of weeks, for now I have to do it just like I have it there. I think I figured out how to get a partial word...just got back from school so after I type it up, I'll post,

    thanks for the help,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

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. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

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