Thread: Help with assigning character arrays

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    5

    Post Help with assigning character arrays

    Hello,
    I working on a big program, and it involves character arrays. What I want my function to do is create a seperate array that consists of the same number of "*" as there are letters in the word from the text file. For example, if the word sarc was up, the function show make ****. This was my attempt at it. I managed to reach this far. I can only fit in one "*". This is part of jumble game program I'm trying to make, and its in C language.

    insert
    Code:
    void partialWord(char current[]){
        int n;
        int i;
    
    
        n = strlen(current);
    
    
    
    
        for (i = 0; i < n; i++){
            current[i] = strcpy(current,"*");
        }
    
    
        printf("%s\n", current);
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try
    current[i] = '*';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    I tried that already, but i keep getting a random repetition of letters. It says, "pppppppp"....

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Please someone help me.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You should post what you tried, because what I said works for me.
    Code:
    #include <stdio.h>
    #include <string.h>
    void partialWord(char current[]){
        int n;
        int i;
        n = strlen(current);
        for (i = 0; i < n; i++){
            current[i] = '*';
        }
        printf("%s\n", current);
    }
    
    int main()
    {
        char buff[] = "hello world";
        printf("%s\n",buff);
        partialWord(buff);
        return 0;
    }
    
    
    # gcc -o main *.c                                                                                                                             
    # ./main                                                                                                                                        
    hello world                                                                                                                                         
    ***********
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #define MAX_GUESSES 3
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    
    
    //This function will explain the game rules
    void gameRules();
    
    
    //This will print *'s that will be the same as length as the word.
    void partialWord(char[]);
    
    
    //This Funtion
    int playJumble(char[]);
    
    
    
    
    
    
    int main(){
    	FILE*txtWord;
    
    
    	char solution[255];
    
    
    
    
    	gameRules();
    
    
    	txtWord = fopen("getWords.txt", "r");
    	fscanf(txtWord, "%s", solution);
    	printf("\nHere is the word!\n%s\n", solution);
    
    
    	partialWord(solution);
    
    
    
    
    	fclose(txtWord);
    	return;
    }
    
    
    void gameRules(){
    	printf("Welcome to the KSG Edition of Jumbled!!!\n");
    	printf("----------------------------------------------\n");
    	printf("To play, you will get a random, scrambled word. Then, it's up to the player \nto guess and figure out the word in three tries. If they succeed, they win! Are\nyou up for it?\n");
    }
    
    
    void partialWord(char current[]){
    	int n;
    	int i;
    	
    
    
    	n = strlen(current);
    
    
    	for (i = 0; i < n; i++){
    		current[i] = "*";
    	}
    
    
    	printf("%s\n", current);
    
    
    }
    Welcome to the KSG Edition of Jumbled!!!
    ----------------------------------------------
    To play, you will get a random, scrambled word. Then, it's up to the player
    to guess and figure out the word in three tries. If they succeed, they win! Are
    you up for it?


    Here is the word!
    spanhpsie
    ╨╨╨╨╨╨╨╨╨
    Press any key to continue . . .

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Perhaps you should pay attention to your error messages.
    Do NOT attempt to run code which generates any warnings or errors.
    Much less, go complaining on a forum that "it doesn't work" without also disclosing the existence of such warnings and errors.

    2. Perhaps you should read the post more carefully.
    Single '*' quotes are DIFFERENT to double "*" quotes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Im sorry for wasting your time. Thank you by the way. I'm an idiot, and I again apologize for wasting your time.

    Great, that means Im going written off as a loser then...
    Last edited by KSG XII; 04-05-2015 at 04:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-01-2014, 07:48 AM
  2. fgetc assigning input to \n character
    By Yendall in forum C Programming
    Replies: 6
    Last Post: 04-08-2014, 09:48 PM
  3. assigning 2D arrays to pointers
    By rakeshkool27 in forum C Programming
    Replies: 5
    Last Post: 01-19-2010, 12:34 AM
  4. trouble assigning arrays
    By shintaro in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2008, 08:31 AM
  5. Assigning values to arrays
    By napkin111 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2003, 08:52 PM

Tags for this Thread