Thread: ascertaining if input is in a string

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    6

    ascertaining if input is in a string

    Hi Peeps,
    please bare with me, I am a noob, but, I am trying.

    I have an assignment where I need to write a hangman program. I have almost all of it done, however, I am having trouble getting my head around checking to see if the letter guess is in the string and how many times. This is my code, the section in comments is where I am having the trouble.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
     
        char word[10];        //secret word array
     char wguess[10];       //word guess array
     char lguess;        //letter guess
     int i;
     int menu1 = 0;        //letter word guess menu looping
     int mtries = 0;        //maximum tries at guessing letter
     int compare;        //hold result of strcmp
     int slength;
     
       
     printf("\nThis is a hangman game for two players\n");
     printf("Player A enters a word, player B guesses the word\n\n");
     
     printf("Player A, enter the word : ");
     scanf("%10s",word);
     for (i = 0; i < 10 ; i++)      //ensure word array is lower case
     {
      word[i] = (char)tolower(word[i]);
     }
     
     printf("\n\n clearing screen\n\n");
     do
     {
      slength = strlen(word);
       
      printf("The word contains %d letters", slength);
       
      for(i=1; i <= slength; i++)
      {
       printf(" _");
      }
      printf("\n"); 
       
      //Menu letter or word
      do
      {
       printf("\nYou have two choices.\n");
       printf("Enter 1 to guess a letter \n");
       printf("Enter 2 to guess a word \n");
       scanf("%d%*c", &menu1);
       // Display users choice
       switch(menu1)
       {
        case 1:
          printf("You have selected to guess a letter: \n\n");
          break;
        case 2:
          printf("You have selected guess a word \n\n");
          break;
        default:
        { 
          printf("You haven't selected a valid option.\n");
          printf("------------------------------------\n");
        }
       }
      }while (menu1 < 1 || menu1 > 2);
     
      if (menu1 == 1)
      {
      
       printf("Please enter a letter: ");
       scanf("%C%*C", &lguess);
        
      //validate letter in word
      
      /**
      for (i=0; i <= slength; i++)
      { 
       if word[i] = lguess
        {
        
        }
       printf("lguess appears %c times\n\n", lguess);
      } 
      **/ 
      }
      
     }while (mtries <= 10 && menu1 !=2);
     
     if (menu1 == 2)         //word guessing
     {
      printf("Enter your guess of the word: ");
      scanf("%10s", wguess);
     }
     for (i = 0; i < 10 ; i++)      //ensures wguess array is lower case
     {
      wguess[i] = (char)tolower(wguess[i]);
     }
     
     compare = strcmp( word, wguess);    //compares arrays and assigns to variable
     if (compare == 0)        //displays appropriate message
     { 
      printf("You win, the answer is, %s:\n", word);
     } 
     else
      {
       printf("\nYour answer of %s is wrong\n", wguess);
       printf("The correct answer is, %s\n", word);
      } 
     
     
     return(0);
    }
    Any feedback, hints or pointers would be appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You need to include string.h header file to use functions like strcmp.
    Code:
    int nlguess = 0;
    for (i = 0; i < slength; ++i)
    {
        if (word[i] == lguess)
        {
            ++nlguess;
        }
    }
    printf("The letter %c appears %d times\n\n", lguess, nlguess);

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Ever heard of functions?

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    I hear you, and yes I have heard of them, however, the assignment came out before we started learning functions and the assignment didn't ask for them.

    I get the feeling that my lecturer can be rather sticky about including things that he hasn't taught us yet.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    Hi again Peeps,

    I was a bit concerned I would get flamed for being a noob, but y'all were cool.

    Thank you for the pointers, it also helped me find a couple of little mistakes that I had made.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 02-27-2013, 03:45 PM
  2. Replies: 1
    Last Post: 02-07-2013, 05:02 AM
  3. Replies: 10
    Last Post: 12-09-2011, 02:52 AM
  4. Replies: 13
    Last Post: 11-04-2011, 06:16 PM
  5. input a string in gcc
    By yogicoder in forum C Programming
    Replies: 5
    Last Post: 06-08-2008, 07:10 AM