Searching a keyword...

This is a discussion on Searching a keyword... within the C Programming forums, part of the General Programming Boards category; Im kinda atlost with programming with this: gven a keyword,it will search for any instances of it in a string ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    28

    Searching a keyword...

    Im kinda atlost with programming with this: gven a keyword,it will search for any instances of it in a string of Questions until the questions are null,if it finds an instance of it ,it will display the question..pls note that a string of question are seperated by spaces per word...Below are my declarations i just cant figure out how cld do the code....(by the way,am usin linked list on this)
    typedef char str20[21];
    typedef char str70[71];

    typedef struct
    {
    str70 strQuestions;
    str20 strChoices[4];
    char cAns;
    int nLevel;
    }struct_Game;
    struct structNode
    {
    struct_Game sGame;
    struct structNode *pGPrev,*pGNext;
    };
    typedef struct structNode structGNodeType;
    typedef structGNodeType *ptrGNode;
    only the fittest survive!!!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One possibility is using strstr().

    if (strstr(node -> strQuestions, keyword) != NULL) //keyword found

    If it should only match whole words and not part of a word, then use a keyword with spaces:

    strcpy(keyword," dog ");

    If you want it to match both lowercase and uppercase, then convert the question to either lowercase or uppercase before the search.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    28
    ive tried doing it but it still wont work..the code is as follows below......
    Code:
    int find(ptrGNode pGame)
    { ptrGNode pTemp;
    	int nCtr,i,j,k,l,m,nVal;
    	str70 strTemp;
    
    	char sKey[31];
    	int nFound=0,nWhere;
    	nCtr =0;
    		do{printf("\n\n Enter Keyword:>");
    	scanf("%s",sKey);
    	
    	if(strlen(sKey)>31)
    	
    	printf("Keyword must only be 30 characters or less");
    		
    	}while(strlen(sKey)>31);
    	 if(sKey[0]=='\0')
    		return(NOT_FOUND);
    	else	
    	{pTemp=pGame;
    
    	
    	while(pTemp!=NULL)
    	{	
    		j=0;
    	for(i=0;i<strlen(pTemp->sGame.strQuestions);i++)
    	{
    	while(pTemp->sGame.strQuestions[i]!='\0')
    	{
    	strTemp[j]=pTemp->sGame.strQuestions[i];
    	j++;
    	}			
    		strTemp[j]='\0';
    
    	if(!strcmp(strTemp,sKey))
    	{
    	nFound=1;
    	return(nCtr);	
    	}	
    	else
    	nCtr++;
    	pTemp=pTemp->pGNext;
    	}
    	}			
    }			
              	if(nFound==1)
    		nWhere=nCtr;
    		else
    		nWhere=NOT_FOUND;
    
    return(nWhere);	
    }
    	         	
    			
    	
    
    
    /*Function: view_Keyword						*
     * Purpose: this function displays the question matching the keyword	*
     * Note: Calls the function int find()					*/
    
    void view_Keyword(ptrGNode pGame,struct_Game sGame)
    {ptrGNode pTemp;
    		int nNum,nChoice;
    		
    		pTemp=pGame;
    		do			
    		{printf("\n ~ ~ ~ ~ ~ VIEW KEYWORD ~ ~ ~ ~ ~ \n");
    		nNum = find(pGame);
    		if( nNum==-1)
    		
    		printf("not found!");
    		else
    		
    		display_Question(pTemp->sGame);
    		printf(" Would you like to look up another(1-yes/0-no)?");
    		scanf("%d",&nChoice);
    		
    		if(nChoice==1)
    		view_Keyword(pGame,sGame);
    		else if(nChoice==0)
    		view_Question(pGame,sGame);
    		else
    		puts(ERROR);
    		}while(nChoice==1);
    	
    	
    
    }
    only the fittest survive!!!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use strstr().

    >if(!strcmp(strTemp,sKey))
    Change this to:
    if(strstr(strTemp,sKey) != NULL)

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    28
    Originally posted by swoopy
    Use strstr().

    >if(!strcmp(strTemp,sKey))
    Change this to:
    if(strstr(strTemp,sKey) != NULL)
    ive tried that too..but still its not workin...hmmm.what couldve possibly be wrong???
    do have any other alternative algo in mind??
    only the fittest survive!!!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >for(i=0;i<strlen(pTemp->sGame.strQuestions);i++)
    >{
    >while(pTemp->sGame.strQuestions[i]!='\0')

    You need to remove one of these loops, either the for-loop or the while loop. You do not need both loops. Probably just keep the for-loop. If you choose to leave in the while loop, make sure you increment i at the end of the loop.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    28
    i tried that thing too both --takin out the while then the for..but still it doesnt work.....?????HEEELLPPPP!

    also,whenever i enter lets say ( the dog)..it prints out a series of not_found messages!---whereas it i only type 1 word like dog it jsut prints out 1 not found msg...?????
    Last edited by kreyes; 03-10-2002 at 08:03 PM.
    only the fittest survive!!!

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    28

    how bout this one?

    i tried making a code diff.from the last one i posted....do you think this one's better?

    Code:
    void viewkey(ptrGNode pGame,structGame sGame)//searches the list of questions 
    {	ptrGNode pRun;
    	string70 skeyword;
    	int nx=0,ny=0,nz=0,nCount2=0,j=0;
    	char cflag='a';
    	printf("\nEnter keyword:");
    	scanf("%s",skeyword);
    	nCount=0;
    	pRun=pGame;
    	if(nCount1!=0)
    	{
    	while(skeyword[nx]!='\0')
    	{	skeyword[nx]=tolower(skeyword[nx]);
    		nx++;
    		nz++;
    	}
    	nx=0;
    	nCount2=0;
    	while(nCount2<nCount1)
    	{	
    		nx=0;
    		ny=0;
    
    		while(pRun!=NULL &&skeyword[nx]!='\0')
    		{	pRun=pRun->pGNext;
    		if(skeyword[nx]==(tolower(pRun->sGame.strQuestions[ny])))
    				nx++;
    			else
    				nx=0;
    			ny++;
    			
    		if(nx==nz)
    		{		
    				printf("\nQuestion #%d ",nCount2+1);
    			display_Question(pRun->sGame);
    			puts(pRun->sGame.strQuestion);
    			for(j=0;j<4;j++)
    				{	if(cflag=='e')
    						cflag='a';
    					printf("%c. ",cflag);
    					puts(pRun->sGame.strChoices[j]);
    					cflag++;
    				}
    				printf("correct answer:  %c",pRun->sGame.cAns);
    				printf("\ndificulty level: %d\n",pRun->sGame.nLevel);
    				nCount++;
    		}
    		if(nCount==3)
    		{	counter();
    			nCount=0;			}
    		}
    		nCount2++;
    		}
    	}
    	else if(nx!=ny && (nCount1==0 ||nCount2==nCount1))
    		printf("\nNo matches found.\n");
    }
    Last edited by kreyes; 03-10-2002 at 08:19 PM.
    only the fittest survive!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 09:41 PM
  4. keyword searching in C
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-08-2002, 01:26 AM

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