hangman need help with strings and pointers

This is a discussion on hangman need help with strings and pointers within the C Programming forums, part of the General Programming Boards category; I need some help with my code as I’m not very familiar with strings and pointers. I need to have ...

  1. #1
    Unregistered
    Guest

    hangman need help with strings and pointers

    I need some help with my code as I’m not very familiar with strings and pointers. I need to have 6 functions in my code. So far I have only written 3. I now need a function to

    a) Check if the guessed letter is in the word
    b) Compare the word after every guess with the inputted word.
    The problem is I must use pointers to search the string and compare the strings


    /* Program that uses pointers and functions to create a simple 2 player version of hangman. Player 1 enters a 9 letter word, which has to be guessed by player 2. The 9 letter word entered by player 1 is to be displayed initially using 9 asterisks(*). As a correct letter is guessed, it replaces the asterisk at its proper location in the word. 3 wrong guesses and player two loses. If player 2 completes the word with less than 3 wrong guesses, s/he wins. The user is given the choice of quitting or playing again after every go. */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXSIZE 9

    void intro( );
    void get_data (char *data);
    char get_letter( );
    void check_letter(char *data, char c, int * mistakes);
    void output();
    void main( )

    {
    char word[MAXSIZE], guess;
    int miss = 0;

    intro ( ) ;
    get_data (data);
    system("CLS");

    while (miss < 3)
    {
    guess = get_letter();
    check_letter(data, guess, &miss);
    }
    return 0;
    }






    /* FUNCTIONS*/

    void intro()
    {
    printf(" ********************************************\n");
    printf("Hangman \n");
    printf(" ********************************************\n\n") ;
    printf("Welcome to my game. Try to guess the word in the fewest possible goes. 3 wrong letter guesses and you lose! \n\n");
    }


    void get_data (char *data)
    {
    printf("Player 1, input 9 letter word for the other player to guess>");
    scanf(“%c”, data)

    while(data!=MAXSIZE)
    {
    printf(“ Error, invalid input. Enter a 9 letter word only\n”);
    scanf(“%c”, data)
    }
    return data;
    }

    char get_letter( )
    {
    char c;

    printf(“*********\n”);
    printf(“Guess a Letter>\n”);
    while ( getchar() != '\n' );
    scanf ( "%c", &c );
    return c;
    }

    void check_letter(char word[MAXSIZE], char c, int *mistakes)
    {
    int i = 0, m = 0, n = 0;

    do
    {
    if (word[i] != c)
    {
    printf("*");
    m += 1;
    }
    else
    {
    printf("%c", c);
    n += 1;
    }
    i += 1;

    }while(i <= strlen(word));
    *mistakes = m;

    printf("You uncovered %d letters\n\n", n);

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Well, for checking if the guessed letter is in the word, you should use the strchr function. The following code example is taken directly out of the MSDN Library and illustrates the use of this function:

    Code:
    Example
    
    /* STRCHR.C: This program illustrates searching for a character
     * with strchr (search forward) or strrchr (search backward).
     */
    
    #include <string.h>
    #include <stdio.h>
    
    int  ch = 'r';
    
    char string[] = "The quick brown dog jumps over the lazy fox";
    char fmt1[] =   "         1         2         3         4         5";
    char fmt2[] =   "12345678901234567890123456789012345678901234567890";
    
    void main( void )
    {
       char *pdest;
       int result;
    
       printf( "String to be searched: \n\t\t%s\n", string );
       printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
       printf( "Search char:\t%c\n", ch );
    
       /* Search forward. */
       pdest = strchr( string, ch );
       result = pdest - string + 1;
       if( pdest != NULL )
          printf( "Result:\tfirst %c found at position %d\n\n", 
                  ch, result );
       else
          printf( "Result:\t%c not found\n" );
    
       /* Search backward. */
       pdest = strrchr( string, ch );
       result = pdest - string + 1;
       if( pdest != NULL )
          printf( "Result:\tlast %c found at position %d\n\n", ch, result );
       else
          printf( "Result:\t%c not found\n" );
    }
    
    
    Output
    
    String to be searched: 
          The quick brown dog jumps over the lazy fox
                   1         2         3         4         5
          12345678901234567890123456789012345678901234567890
    
    Search char:   r
    Result:   first r found at position 12
    
    Result:   last r found at position 30
    As you can see, it uses pointers so this should be acceptable for your use.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42

    Re: hangman need help with strings and pointers

    .
    Last edited by Nit; 03-27-2002 at 08:16 AM.
    http://www.KBeutler.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 10:25 PM
  3. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  4. need more help with pointers and strings
    By bgbfflochp in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2002, 07:31 AM
  5. Pointers to pointers to strings
    By Natase in forum C Programming
    Replies: 2
    Last Post: 09-17-2001, 11:30 PM

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