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++
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.
The question is not clear, "atleast" to me. Can you try to explain using an example?
Anoop.
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
like this
Code:char p[20]; strcopy(p, "hello"); while(p != '\0') { *p = '_'; ++p; }
What header does strcopy belong too... but yea I think i see how it works
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
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
--------------------------------------------------------------------------------
>>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.
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] = '_'; }
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
Well help out a newbie.... show me what you mean...
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.
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; }
Yea hehe I realize that hehe... this is what I got:
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 "_".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"); }
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.
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.