Computing.Net > Forums > Programming > Reverse Words

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Reverse Words

Reply to Message Icon

Name: khanati
Date: November 1, 2005 at 14:00:25 Pacific
OS: Windows XP
CPU/Ram: 512
Comment:

How can we reverse words in a string using C. For example we have "box is red". it hsould give "red is box". How can we use pointers to solve this problem.

Atif



Sponsored Link
Ads by Google

Response Number 1
Name: wizard-fred
Date: November 1, 2005 at 23:08:06 Pacific
Reply:

I don't know if you need pointers, but the first thing is to break the string into words.


0

Response Number 2
Name: jambon_vn
Date: November 6, 2005 at 19:29:23 Pacific
Reply:

//Try this code

string s = "box is red"; //Input string
string t = ""; //Output string
string w = "";

for (int i = 0; i < s.Length; i++)
{
if (s[i] == ' ')
{
t += w + ' ';
w = "";
}
else w += s[i];
}

t+= w; //Get the last word of string if there is not a ' ' at the end of input string



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Reverse Words

Please help me in C! www.computing.net/answers/programming/please-help-me-in-c/8537.html

Hexadecimal to WORD conversion? www.computing.net/answers/programming/hexadecimal-to-word-conversion/14507.html

error when loading word documents www.computing.net/answers/programming/error-when-loading-word-documents/5126.html