Originally Posted by
camel-man
Why do you have the while loop? Why cant you just use the strcpy?
Yeah i realized that the while loop involving fgets is completely out there, and isn't correct.
Solution calls for:
char temp[80], reverse[80], *ptr;
Use strtok to get the first part of the string and save it to the ptr pointer. Then using sprintf, copy it to the reverse array.
Use strpy to copy reverse into another array, temp. Repeat. Now the key to printing it backwards is to keep using sprintf, with ptr in front of
temp.
reverse = sprintf("%s %s", ptr, temp);
That's why im using a while loop.
Hopefully i implement this correctly
Code:
void reverse(char name[],char reverse_name[],char temp[]);
int main()
{
char name[80];
char reverse_name[80];
char temp[80];
printf("Input: ");
fgets(name,80,stdin);
reverse(name,reverse_name,temp);
return 0;
}
void reverse(char name[],char reverse_name[],char temp[])
{
char *ptr=strtok(name, " \n"); //Use strtok to get the first part of the string and save it to the ptr pointer. Then using sprintf, copy it to the reverse array.
reverse_name=sprintf("%s,ptr);
strcpy(temp,reverse); // Use strcpy to copy reverse into another array, temp. Repeat. Now the key to printing it backwards is to keep using sprintf, with ptr in front of
temp.
printf("%s\n",temp);
}
I wish i can see this in action.