I have this problem

Started by
2 comments, last by librab103 20 years, 8 months ago
I have this code:

char phrase[100];
	int s;
	printf("Please enter a word: ");
		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.
(friend) WHAT!!!!..... Are you crazy??? (me) Yes I am.
Advertisement
Why not simply use strcat after you take input from the user?
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.
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");



Or instead of that, you could replace your code with:

   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");


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

[edited by - SoulSkorpion on August 10, 2003 9:30:00 AM]
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)

This topic is closed to new replies.

Advertisement