replacing individual chars in a string array

This is a discussion on replacing individual chars in a string array within the C Programming forums, part of the General Programming Boards category; Hi everyone...I'm trying to right the function that puts the correct letters in the right positons being reported from my ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    5

    replacing individual chars in a string array

    Hi everyone...I'm trying to right the function that puts the correct letters in the right positons being reported from my previous compareletter function for my game of hang man and its not updating the word in progress array.
    Here's my compare letter function that's working correctly:

    Code:
    //function that returns the index of the letter that the user has guessed or 
    Code:
    //-1 if the letter isn't in the word
    int CompareLetter(char array[], char guess, int numLetters)
    {
     int i;
     for (i = 0;i < numLetters; i++)
        {
    
     if(array[i] == guess) {
     return i;
            }
        }    
     return -1;
    }
    So now that I have the correct position , I have an array with a series of asterisks that I would like to replace the position of the asterisk with that of the correct letter.

    Code:
    //updates the word in progress array with correct guesses
    void updateWIP (char array[], char guess, int result)
    {
    
    
     if (result !=-1)
     array[result] = guess;
    }
    However, this isn't changing any of the position of the asterisks in the word in progress array positions 0-3. Any ideas?

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The error is your belief that this function isn't changing the contents of array, because it is. Perhaps your letter guess isn't what you think it is (and thus CompareLetter is returning -1 instead of what you want), or perhaps you are discarding the result of CompareLetter instead of passing it to updateWIP, or perhaps you are feeding updateWIP the wrong array (perhaps the actual word instead of the array of asterisks) -- or maybe the other way around (feeding CompareLetter the array of asterisks instead of the actual word).

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    I've added
    Code:
    printf("GUESS=%c", guess);
    Code:
    	printf("WRESULT=%d", result);
    	printf("WIP ARRAY%s\n", array);




    to the function to make sure everything is being passed in as it should be. One thing thats happening though is that I pass the * array words in progress to a display array function that just has this line

    Code:
    printf("%s\n", array);
    and after I update the array with my updateWIP function, it no longer displays when I call it to display. It still shows four *'s inside of my updateWIP array however and I'm using the same printf statement just inside the updateWIP function.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And what are the values you see for guess, result, and array?

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    kind
    GUESS THIS WORD:


    Please enter a letter:k


    Letters guessed so far: k
    ****
    wresult=0GUESS=kWRESULT=0WIP ARRAY****
    GUESS THIS WORD:


    Please enter a letter:i


    Letters guessed so far: ki
    ****
    wresult=1GUESS=iWRESULT=1WIP ARRAY****
    GUESS THIS WORD:


    Please enter a letter:n


    Letters guessed so far: kin
    ****
    wresult=2GUESS=nWRESULT=2WIP ARRAY****
    GUESS THIS WORD:


    Please enter a letter:e


    Letters guessed so far: kine
    ****
    wresult=-1GUESS=eWRESULT=-1WIP ARRAY****

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    This is my complete program if it helps.

    Code:
    #include<stdio.h>#include<ctype.h>
    #include<string.h>
    #define S 25
    
    
    //function prototypes
    
    
    //displays instructions for user
    void Instructions();
    
    
    //displays character arrays 
    void DisplayArray(char array[]);
    
    
    //prompts user to enter a letter 
    char GuessLetter();
    
    
    //compares arrays to see if they match or not
    int CompareArray(char arraya[], char arrayb[]);
    
    
    //function that returns the index of the letter that the user has guessed or 
    //-1 if the letter isn't in the word
    int CompareLetter(char array[], char guess, int numLetters);
    
    
    //tells the player if they won the game or not 
    void wonGame(int theSame);
    
    
    //updates the word in progress array with correct guesses
    void updateWIP (char array[], char guess, int result);
    
    
    main()
    {
    //declare variables
    	FILE *fp;
     int i;
     int x;
     char wtbg[S] = {'\0'};
     char gl[S] = {'\0'};
     char wip[S] = {'\0'};
     char guess = ' ';
     int numguess = 0;
     int numLetters = 0;
     int theSame = 0;
     int wresult = 0;
    
    
    //displays instructions for user
    	Instructions();
    
    
    //get word from file
    	fp = fopen ("words.txt", "r");
    	fscanf(fp,"%s", wtbg);
    
    
    //copy length of wtbg to wip array
    	numLetters = strlen(wtbg);
    
    
    	DisplayArray(wtbg);
    
    
     while (numguess != 6)
    	{
    //printf("GUESS=%d", guess);
    
    
    //display wip
    	printf("GUESS THIS WORD:\n");
     for (i = 0;i < numLetters; i++)
    	{
    		wip[i] = '*';
    //printf("%c", wip[i]);
    	}
    	  wip[i] = '\0';
    
    
    	guess = tolower(GuessLetter());
    	gl[numguess] = (guess);
    	printf("\nLetters guessed so far: ");
    	DisplayArray(gl);
    	DisplayArray(wip);
    	wresult = CompareLetter(wtbg, guess, numLetters);
    	printf("wresult=%d", wresult);
    	updateWIP(wip, guess, wresult);
    //theSame = CompareArray(wtbg, wip, numLetters);
    //printf("\nAre they the same array? %d", theSame);
    //printf("NUM LETTERS = %d", numLetters);
    
    
    //update number of guesses
    //wresult = 0;
    	numguess++;}
    
    
    //printf("GUESS=%d", guess);}
    
    
    return 0;
    
    
    }
    //displays instructions for user
    void Instructions()
    {
    	printf("WELCOME TO THE ULTIMATE GAME OF HANGMAN!\n\n");
    	printf("PLEASE TAKE A LOOK AT THE FOLLOWING RULES/INSTRUCTIONS!\n\n");
    	printf("You will have the opportunity to guess a word.\n\n");
    	printf("Guess the letters one at a time.\n\n");
    	printf("You can have upto six wrong guesses.\n\n");
    	printf("The game ends when either you have guessed:\n");
    	printf("--all the letters in a word\n");
    	printf("--guessed wrong six times\n\n");
    	printf("LET THE GAMES COMMENCE...\n\n");
    }
    
    
    //displays character arrays 
    void DisplayArray(char array[])
    {
    	printf("%s\n", array);
    
    
    }
    
    
    //prompts user to enter a letter and stores it in guess array 
    char GuessLetter()
    {
     char guess;
    
    
    	printf("\nPlease enter a letter:");
    	scanf(" %c", &guess);
     return guess;
    }
    
    
    //compares arrays to see if they match or not
    int CompareArray(char arraya[], char arrayb[])
    {	
     int theSame;
    
    
    	theSame = strcmp(arraya,arrayb);
    
    
     return theSame;
    }
    
    
    //function that returns the index of the letter that the user has guessed or 
    //-1 if the letter isn't in the word
    int CompareLetter(char array[], char guess, int numLetters)
    {
     int i;
     for (i = 0;i < numLetters; i++)
        {
    //printf("numLetters=%d", numLetters);
     if(array[i] == guess) {
     return i;
            }
        }	//printf("numLetters=%d", numLetters);
     return -1;
    }
    
    
    
    //tells the player if they won the game or not 
    void wonGame(int theSame)
    {
     if (theSame == 0)
    	{
    	printf("\nCONGRATS...YOU WON THE GAME!!!");
    	}
    
    
     else if (theSame == 1)
    	{
    	printf("\nSORRY YOU DIDN'T WIN LOOSER...");	
    	}
    
    
    }
    
    
    //updates the word in progress array with correct guesses
    void updateWIP (char array[], char guess, int result)
    {
    	printf("GUESS=%c", guess);
    	printf("WRESULT=%d", result);
    	printf("WIP ARRAY%s\n", array);	
    
    
     if (result !=-1)
     array[result] = guess;
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So the error here is that you re-set the wip back to all asterisks after every guess (because it's inside your while loop), rather than just once at the beginning of the word.

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    Thanks ! I can't believe I missed that…guess it helps to take a break once in a while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2012, 10:36 PM
  2. Performing individual actions on chars in an array.
    By frankchester in forum C Programming
    Replies: 4
    Last Post: 12-04-2010, 10:57 AM
  3. sscanf replacing chars
    By paperbox005 in forum C Programming
    Replies: 4
    Last Post: 05-03-2005, 09:02 AM
  4. Replacing and finding chars in a string
    By xshapirox in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2004, 12:40 AM
  5. Replacing chars in Pointer String results in Segfault
    By discostu in forum C Programming
    Replies: 2
    Last Post: 03-08-2003, 03:38 PM

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