1,105,225 Community Members

print words in reverse in a given sentence

Member Avatar
pavani2006
Newbie Poster
20 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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);

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
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.

Member Avatar
pavani2006
Newbie Poster
20 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

help me in aproper way

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
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.

Member Avatar
kishore22
Newbie Poster
2 posts since Jun 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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:

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

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

Member Avatar
Nichito
Posting Virtuoso
1,724 posts since Mar 2007
Reputation Points: 393 [?]
Q&As Helped to Solve: 81 [?]
Skill Endorsements: 4 [?]
Featured
 
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

Member Avatar
Chaster
Junior Poster in Training
68 posts since Jun 2007
Reputation Points: 2 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

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

Member Avatar
devaru2003
Newbie Poster
2 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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++;
	}
}
Member Avatar
devaru2003
Newbie Poster
2 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article