1.11M Members

print words in reverse in a given sentence

 
0
 

i want to print words in reverse.
i could print characters in reverse but not words
for(int i=arr.length;i>0;i--)
printf(arr);

 
0
 

1. Please figure out how to use [code]
[/code] tags real soon.
Isn't the water mark at the back of the edit box, or the READ THIS FIRST links enough for you?

2. printf(arr);
The first parameter of printf() is a control string, not your data.

 
0
 

help me in aproper way

 
0
 

>help me in aproper way
Say please, jerk. The least you could do is show some respect to the people who are volunteering their time to help you for nothing in return.

>i could print characters in reverse but not words
Okay, you can print the characters in reverse now try to physically reverse the string and print it as a whole. That solution will give you the words in reverse, but also the characters in each word reversed. So you need to make another pass and reverse each word. The string reverse solution can be generalized to handle both cases, but since this is typical homework, I can't give you code before you make an attempt.

 
0
 

help me in aproper way

Hello pavani,

I just wanted a clarification for your question posted here.

Is that the question is reversing each individual word ?

example: If the input is

input = my name is kishore
Do you want the output like this ?

output = kishore is name my

or something else ?

Thanks
Kishore.:confused:

 
0
 

>Is that the question is reversing each individual word ?
Yes, that's what the OP wanted.

 
0
 

why son't you create an array of strings, and store in each string each word of your sentece, so you just have to print your array of strings backwards...

i'm sorry... but as narue, and our queen of daniweb, her majesty dani (in the anouncements posted in every forum) have already told you, WE DO NOT GIVE HOMEWORK TO THOSE THAT DO NOT SHOW ANY EFFORT

 
0
 

Just an idea:
Two words are usually separated by " "(SPACE). So, you have a string, containing some words. Now you start from the end of the string, and put the character into a new string's 0th position. Test if the next character is space. If not shift the new string, put the char to the 0th position. If you find a space, print your new string, and reinitialize it.

 
0
 

>Just an idea:
It sounds a great deal more complicated and less efficient than the usual double reverse trick.

 
0
 
void printReverse(char* string) {
	char* lastP = string;
	while (*string != NULL) {
	{
	     if (*string == ' ') {
		char* current = string -1;
		while (lastP != current) {
			printf("%c", *current);
			current--;
		}
		printf("%c", *(current -1));
		lastP = string;
	     }
	string++;
	}
}
 
0
 
void printReverse(char* string) {
	char* lastP = string;
	while (*string != NULL) {
	{
	     if (*string == ' ') {
		char* current = string -1;
		while (lastP != current) {
			printf("%c", *current);
			current--;
		}
		printf("%c", *(current -1));
		lastP = string
	     }
	string++;
	}
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article