Help with something.. I don't know what to call it???

This is a discussion on Help with something.. I don't know what to call it??? within the C Programming forums, part of the General Programming Boards category; I would like to know how could I make a word or words, no matter how many characters it might ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    34

    Question Help with something.. I don't know what to call it???

    I would like to know how could I make a word or words, no matter how many characters it might have and make each character be replaced with this: "_"

    I am using C and mvc++
    Last edited by librab103; 08-09-2003 at 10:46 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    The question is not clear, "atleast" to me. Can you try to explain using an example?

    Anoop.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    alright... lets say I input the word "Happy" the computer prints to the screen "_ _ _ _ _" each "_" representing one letter of the word. I hope this is a little clearer

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    like this
    Code:
    char p[20];
    strcopy(p, "hello");
    while(p != '\0') {
         *p = '_';
          ++p;
    }

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    What header does strcopy belong too... but yea I think i see how it works

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Actually, its strcpy and string.h.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    With what you gave me chrismiceli and what XSquared told me which header to use... it gave me this error: error C2105: '++' needs l-value

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    --------------------------------------------------------------------------------
    >>char p[20];
    >>strcopy(p, "hello");
    >>while(p != '\0') {
    >> *p = '_';
    >> ++p;
    >>}

    -----------------------------------------------------------------

    yes it would complain. change
    while (p!= '\0')

    to while (*p != '\0').



    you could ofcourse simplify things in the following way.

    Code:
    for (i = 0; p[i] != '\0'; i++)
    {
        p[i] = '_';
    }

    Anoop.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    like this right:

    Code:
    char p[100];
    	char phrase[100];
    	int i;
    	printf("Please enter a word:");
    		scanf("%s", phrase);
    	strcpy(p, phrase);
    	for (i = 0; p[i] != '\0'; i++)
    		{
    			p[i] = '_';
    		}

  10. #10
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Yeah, that would work, but it wouldn't output your new string, and you can just overwrite the old string if you want to, you don't need two strings. and sorry for the typos, and I just love to use pointers, makes you look smart

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Well help out a newbie.... show me what you mean...

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    All you have to do now is use a "printf()" statement.
    Ideally you should have a check on the length of the word being entered. It should not be more than 99 characters long(definitely would not be a proper english word) as per your code.

    Anoop.

  13. #13
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    final code

    Code:
    #include <stdio.h>
    int main(void) {
    char p[100];
    	char phrase[100];
    	int i;
    	printf("Please enter a word:");
    		scanf("%s", phrase);
    	strcpy(p, phrase);
    	for (i = 0; p[i] != '\0'; i++)
    		{
    			p[i] = '_';
    		}
            printf("%s\n", p);
            return 0;
    }

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Yea hehe I realize that hehe... this is what I got:

    Code:
    void thePhrase()
    {
    	char p[100];
    	char phrase[100];
    	int i;
    	printf("Please enter a word: ");
    		scanf("%s", phrase);
    	strcpy(p, phrase);
    	for (i = 0; p[i] != '\0'; i++)
    		{
    			printf ("_ ");
    		}
    	printf("\n");
    }
    Ultimately what I want to do is have the computer pick from a file a category, display what category it picked, and then have it pick a phrase out of that category and display the phrase as "_".

    For right now I am making it a 2 player game until I get it the way I want it then i will implement the computer into it.

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Code:
    #include <stdio.h>
    
    int main(void) {
        char string[100];
        int i;
        
        puts("Please enter a word:");
        scanf("%s", string);
        
        for (i = 0; string[i] != '\0'; string[i++] = '_')
            ;
        puts(string);
        return 0;
    }
    Beware the fury of a patient man.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 04:44 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

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