Alright quick question I can get this to work but after the "char s[] = "The quick brown fox ";" I need a space after fox whats wrong in my code thats making it that I need a space. Im stumped how to make it terminate with NULL
#include <stdio.h>
void main()
{
int begin = 0;
int end = 0;
int space = 0;
char s[] = "The quick brown fox ";
int i = 0;
do
{
if(s[i] != 32)
{
i++;
}
else
{
char temp;
begin = space;
end = i;
space = end + 1;
while(end >= begin)
{
temp = s[begin];
s[begin] = s[end];
s[end] = temp;
begin++;
end--;
}
}
}while(s[i] != NULL);
for(int index = 0; index < i; ++index)
{
printf("%c", s[index]);
}
printf("\n\n");
}
1. nul and NULL are two entirely different things.
2. All string literals end with '\0'.
Mythios: You're comparing a char to a NULL pointer. Change the NULL in your comparison to 0 or '\0' (they both mean the same, so it doesn't make a difference). 0 is the nul character.
OK so say I do it how your saying (my bad with the nul there, should of picked that one up myself) I'm still getting no errors but the output of the program will be this:
The quick brown fox
ehT kciuq nworbfox
So it's stumped me why it's not printing it out how I need. Yet if I make the input have a space at the end it works fine.
s itself will never be false, so those conditions on 19 and 38 are useless.
You just need to reverse the letters in each word?
Why not just write two functions - the first one, given a string and a starting index, finds the index of the first space occuring after the start
index, and the second, given a string and two indices reverses all of the characters in the range?
Same as the other program I'm doing. They are basic but they have to be done a cert ant way. Not exactly the way I'd do them but I have to follow the guideline.
Its the very beginning of this course we technically haven't learned them yet lol. So it's not that we can't we are meant to use what we know. I have a lot more knowledge otherwise. It's just a little harder to attack these problems in a way like this if you get what I mean :)
Ah well, I agree but nothing I can do about it until a couple of weeks lol. His just running through the basics as some people don't know a lot about it. So I have to sit back and enjoy the ride of using no advancedness lol.
Really though. Do as helios suggested. Or at least ask your professor.
In my intermediate programming class we used Turbo Pascal. Most of the class was teaching things I already knew and had been using for years. In fact, I had developed a text-based UI similiar to Turbo Vision before even starting the class, so to make the homework assignments the least bit challenging I wrote all of my homework assignments to use my UI instead of just writeln.
My UI library was OO with some assembler. Turbo Pascal's OO extensions were taught the last few weeks of the semester; assembler wasn't touched upon until the following semester (systems programming).
If your skills are well beyond the class, why don't you see if you can take the final exam early and place out of the class if it is a prerequisite?