954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

print words in reverse in a given sentence

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[i]);

pavani2006
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 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[i]);
The first parameter of printf() is a control string, not your data.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

help me in aproper way

pavani2006
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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:

kishore22
Newbie Poster
2 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

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.

Chaster
Junior Poster in Training
68 posts since Jun 2007
Reputation Points: 12
Solved Threads: 3
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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++;
	}
}
devaru2003
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 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++;
	}
}
devaru2003
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You