#### Archived

This topic is now archived and is closed to further replies.

# I have this problem

## Recommended Posts

I have this code:
char phrase[100];
int s;
scanf("%s", phrase);
for (s = 0; phrase[s] != ''\0''; s++)
phrase[s] = ''_'';
printf(phrase);
printf("\n");

What I am trying to do is add a " " between each "_" but no matter what I have tried it gives a funny reponse. Also is each space represent by a "value" like the first dash is phrase[1], second is phrase[2], and so on and so on. So does the computer remember what the dash really are?? Thanks for the help
(friend) WHAT!!!!..... Are you crazy??? (me) Yes I am.

##### Share on other sites
Why not simply use strcat after you take input from the user?

##### Share on other sites
What you''re currently doing is replacing every character of phrase with a ''_''. By every iteration you print the actual value of phrase (some underscores, then the rest of the original string). Using an if would be a good starting point.

##### Share on other sites
quote:
Original post by librab103

I have this code:

*snip*

What I am trying to do is add a " " between each "_" but no matter what I have tried it gives a funny reponse. Also is each space represent by a "value" like the first dash is phrase[1], second is phrase[2], and so on and so on. So does the computer remember what the dash really are?? Thanks for the help

Well, your code simply replaces all the letters in the string with underscores. How could the original string possibly remember the previous values? You're completely overwriting them.

In terms of making it print spaces in between the underscores, it's probably easier to have whatever prints the word on the screen take care of that, rather than edit the string to have all these meaningless spaces in it. Something like this:

//after the above codeint c;for(c = 0; phrase[s] != '\0'; c++) //c++. get it? get it? ah, I kill me...{  printf("%c ", phrase[s]);}printf("\n");

   char phrase[100]printf("Please enter a word");scanf("%s", phrase);//and then when you want to spit out the string as a sequence of underscores:int c;for(c = 0; c < strlen(phrase); c++){  printf("_ ");}printf("\n");

God, these forums are frustrating...[/edit]

[edited by - SoulSkorpion on August 10, 2003 9:30:00 AM]

• ### Forum Statistics

• Total Topics
628362
• Total Posts
2982266

• 10
• 9
• 13
• 24
• 11