954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

string: get token from string and compare token from text file

i need to write a code on how can break a sentence into word and find out the word belong to.
example :
when user input a sentence cindy walk slow.
1.break the sentence into cindy,walk,slow.
2.get each word belong to and display.
- cindy is a noun
- walk is a verb
- slow is a adjective.

i had make 2 file text:
1. verb .txt
2. adjective

verb.txt

eat
walk
ran 
sleep

adjective

fast
slow
pretty

this is the code i had done:

const size=100; //size of string
   char string[size],temp[size][32];
   char *tokenPtr;
   int i=0;

   cout<<"input string\n";
   cin.getline(string,size);//read line of string

   /*function char *strtok(char *s1,const char *s2) breaks s1 into token(word)
   	separated by character in s2
	*/

   tokenPtr =strtok(string," ");//pointer to first token

   while (tokenPtr != NULL)
   	{
      	strcpy(temp[i],tokenPtr);//save the token to array
         i++;
      	tokenPtr=strtok(NULL," ");
      }

   	for(int j=0;j<i;j++)
   {
   	 char word1[30],word2[30];

       ifstream dictionary1("verb.txt");
       ifstream dictionary2("adjective.txt");

         while (dictionary1>>word1)
         	if (strcmp(word1,temp[j])==0)
   				 cout<<temp[j]<<" is a verb \n";
         	while (dictionary2>>word2)
            	if (strcmp(word2,temp[j])==0)
               	cout<<temp[j]<<" is a adjective \n";

   }
   return 0;
}

the output:
input string
ali walk slow
walk is a verb
slow is a adjective


what i want to know is how i can get word ali belong to?
i didn't want to make a text file that contain of the noun.
if the word not in the verb.txt and adjective.txt,therefore it is a noun..

please help me..
anyway thank you.

amen
Newbie Poster
17 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

set a boolean flag to indicate if the word is found in one of the two files. If not set then it must be a noun. Something like this sudo-code. Note: search second file ONLY if the word is not in the first file.

set flag to false
search verb.txt file for word
if word found in verb.txt then set flag to true
if flag still false
begin
    search adjective.txt file for word
    if word found in adjective.txt file set flag to true
end if
if flag still false
   word is a noun
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thank you..i think i get it on..let me try first..

amen
Newbie Poster
17 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You