-
Unexpected output
'm new to c can someone help me out i'm trying to write a program that reverses each letter in a sentence. eg - going home = gniog emoh. but i keep getting strange char's heres the code:
Code:
#include<stdio.h>
int main()
{
char info[100];
char temp;
int i = 0;
int n;
int j = 0;
int count;
printf("Enter info to encrypt: \n");
scanf("%[^\n]s", info);
while(info[n] != '\0')
{
n = 0;
while(info[j] != ' ')
{
j++;
}
count = j;
printf("%d", j);
while(i <= count)
{
temp = info[i];
info[i] = info[j];
printf("%c", info[i]);
info[i] = temp;
j--;
i++;
}
j = count;
j++;
if(info[j] == ' ')
{
n = info[j];
printf("%c", n);
j++;
}
n++;
}
return 0;
}
-
How do you expect loops like this to ever exit?
Code:
while(info[j] != ' ')
{
j=0;
j++;
}
Step through at least 3 iterations of your loop in your head or on paper. What is 'j' each time?
-
Hey
Hey i said i'm new to programming so just help me out don't dis me.
-
I'm not dis'ing you, I'm telling you how to figure out your problem. Instead of getting defensive for no reason, maybe you should just try the suggestion.
-
'j' is the number for the last word before each blank space.
-
i think i changed sumthin coz that part of the code worked prreviously
-
Oh okay but wat about the rest of the code?
-
Yeah i saw where i went wrong but my problem is with the rest of the code. :D
-
I have to say I'm with grumpy on this one. I think you coded all this before you knew exactly what it was you wanted to do. That is, you didn't plan well before you started coding. I probably spend more time reading and planning than I do actually coding, by a factor of 3 to 1. We know you're new, but honestly, learning to plan before you code is one of the first skills any programmer should learn.
You need to be able to solve this problem by hand, on paper, before you ever touch the keyboard. Work through several examples by hand. Try examples like sentences with one word, words with single characters, words with several spaces between them, punctuation, etc. Make sure you can handle all the bizarre scenarios too. Pay very careful attention to every little step you did. If you can explain each step in detail, in plain English (or your native language), then the code will become much, much easier to write.
Then, when it comes time to code, you need to start small, and code little bits at a time, compiling and testing often, and fixing all issues before moving on. For example, first I would simply make my program print out the full sentence the user entered. Then I would make it print out only the words, ignoring the punctuation, so you know you can properly isolate each word. Then I might have it print out the words in reverse, without spaces or punctuation, to make sure your reversal code works. Lastly I would have it print out the words in reverse with the spaces and punctuation back in.
It might help if you used better names than i and j, like start and end. start should always be set to the first letter of a word, and end to the last. Then, you print from end back to start, set start to the character after end, and look for the beginning of a word again. Always make sure you don't go off the end of the array, or you will end up in the land of undefined behavior, which will cause strange problems for your program and headaches for you.
-
WOW! Thanx alot i'll try that :D