Thread: assignment help strcpy?

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    assignment help strcpy?

    This for loop replaces the stars ******** in an array that contains a word to be guessed with the correct letter (c) that the user inputs if the word contains that letter.

    Problem: After every guess the same hidden string of stars ******* is displayed instead of ex: ***W**** (assuming W was entered by the user)

    How can I update the old ******** string in the Stars array with the new string that includes the correct letters chosen, so after every correct guess at a letter in the word a new string is displayed including the correct letters chosen?

    I'm pretty sure I have to use strcpy but not sure how to implement it using a loop.

    I hope I make sense

    Code:
    for(i = 0; i < strlen(unscrambledWord); i++)
    				{
    					if(unscrambledWord [i] == c)
    						{
    						Stars[i] = c;
    						
    						}
    				}

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You need to show more of your program because the code as posted works as expected:
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char Stars[] = "*******";
        char unscrambledWord[] = "fooWbar";
        char c = 'W';
        int i;
    
        printf("before: %s\n", Stars);
        for(i = 0; i < strlen(unscrambledWord); i++)
        {
            if (unscrambledWord[i] == c)
            {
                Stars[i] = c;
            }
        }
        printf("after: %s\n", Stars);
        
        return 0;
    }
    $ ./foo
    before: *******
    after: ***W***
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. strcpy without new
    By sawer in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2007, 04:34 AM
  4. strcpy
    By Tibo in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 07:02 AM