C program to reverse the order of the words entered.
-
for ex, input: My name is Bay Max. output: Max Bay is name My. below is my code, but its too long and messy. How do i make it more concise and readable? Any alternative would be appreciated.
#include
#include#define EOL '\n'
#define MAX_SIZE 100int main(){
char text\[MAX\_SIZE\], back\[MAX\_SIZE\], temp\[MAX\_SIZE\]; int count, i, mark\[50\]; //reading the text for(count=0; (text\[count\] = getc(stdin))!= EOL; ++count){ mark\[count\]=0; } text\[count\] = '\\0'; //reversing the text for(i = count-1; i>=0; --i){ back\[(count-1)-i\] = text\[i\]; } back\[count\] = '\\0'; //noting the frequencies of the occurences of space for(i=count-1; i>=0; --i){ if(back\[i\]== ' '){ mark\[i\] = i; } } //irr-reversing each words at their respective place int j, k=0, l=0; for(i=0; i\=k; j--){ temp\[l\] = back\[j\]; //it reverse the words btw two space entries l++; } temp\[mark\[i\]\] = ' '; l= l+1; k = mark\[i\]+1; } } for(i=count-1; i>=k; i--){ temp\[l\] = back\[i\]; //it reverse the word after the last space entry. l++; } //printing the string. printf("\\n%s", temp); return 0;
}
Thank you.
-
for ex, input: My name is Bay Max. output: Max Bay is name My. below is my code, but its too long and messy. How do i make it more concise and readable? Any alternative would be appreciated.
#include
#include#define EOL '\n'
#define MAX_SIZE 100int main(){
char text\[MAX\_SIZE\], back\[MAX\_SIZE\], temp\[MAX\_SIZE\]; int count, i, mark\[50\]; //reading the text for(count=0; (text\[count\] = getc(stdin))!= EOL; ++count){ mark\[count\]=0; } text\[count\] = '\\0'; //reversing the text for(i = count-1; i>=0; --i){ back\[(count-1)-i\] = text\[i\]; } back\[count\] = '\\0'; //noting the frequencies of the occurences of space for(i=count-1; i>=0; --i){ if(back\[i\]== ' '){ mark\[i\] = i; } } //irr-reversing each words at their respective place int j, k=0, l=0; for(i=0; i\=k; j--){ temp\[l\] = back\[j\]; //it reverse the words btw two space entries l++; } temp\[mark\[i\]\] = ' '; l= l+1; k = mark\[i\]+1; } } for(i=count-1; i>=k; i--){ temp\[l\] = back\[i\]; //it reverse the word after the last space entry. l++; } //printing the string. printf("\\n%s", temp); return 0;
}
Thank you.
I'd make a map of the words (array of pointers to char), then iterate it backward.
-
for ex, input: My name is Bay Max. output: Max Bay is name My. below is my code, but its too long and messy. How do i make it more concise and readable? Any alternative would be appreciated.
#include
#include#define EOL '\n'
#define MAX_SIZE 100int main(){
char text\[MAX\_SIZE\], back\[MAX\_SIZE\], temp\[MAX\_SIZE\]; int count, i, mark\[50\]; //reading the text for(count=0; (text\[count\] = getc(stdin))!= EOL; ++count){ mark\[count\]=0; } text\[count\] = '\\0'; //reversing the text for(i = count-1; i>=0; --i){ back\[(count-1)-i\] = text\[i\]; } back\[count\] = '\\0'; //noting the frequencies of the occurences of space for(i=count-1; i>=0; --i){ if(back\[i\]== ' '){ mark\[i\] = i; } } //irr-reversing each words at their respective place int j, k=0, l=0; for(i=0; i\=k; j--){ temp\[l\] = back\[j\]; //it reverse the words btw two space entries l++; } temp\[mark\[i\]\] = ' '; l= l+1; k = mark\[i\]+1; } } for(i=count-1; i>=k; i--){ temp\[l\] = back\[i\]; //it reverse the word after the last space entry. l++; } //printing the string. printf("\\n%s", temp); return 0;
}
Thank you.
I would be tempted to use
strtok()
. Then just read the returned value in reverse order."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles