Thread: how to search a keyword in C?

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

    Unhappy how to search a keyword in C?

    im tryin to make a code that given a keyword it will search for its instances in all the Questions(take note that words in the question are seperated by a space)-my code here runs but doesnt seem to get any match though im pretty sure the keyword i entered has a match.anybody help??

    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;
    j=0;

    while(pTemp!=NULL)
    {
    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!!!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >while(pTemp!=NULL)
    >{

    Add the following line here:
    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';

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    28
    ok ill try that code ...anyway,i have inlcuded my typedef declarations for the above code of mine:

    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;

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. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM