By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,649 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,649 IT Pros & Developers. It's quick & easy.

I keep getting this question on beginner study sheets...

P: n/a
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?

The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"

This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)

Again, any help here much appreciated.
Thanks!

Jeff

Nov 16 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Hi,

Is this the kind of thing you are after?

using System;

namespace Reverse
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string str = "1,2,3,4";
string result = "";
for (int i = str.Length - 1; i>=0; i--)
{
result += str.Substring(i, 1);
}
Console.WriteLine("Start string: " + str);
Console.WriteLine("Result string: " + result);
Console.ReadLine();
}
}
}
Hope that helps... Although there may be easier ways to do it...

John
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02...
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?

The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"

This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)

Again, any help here much appreciated.
Thanks!

Jeff

Nov 16 '05 #2

P: n/a
One approach:

Use String.Split() with a comma as the delimiter, to break up the numeric
substrings into an array. Then iterate over the array from the end to the
beginning, to reassemble the substrings into the order you want. For
example:
HTH,
Tom Dacon
Dacon Software Consulting
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02...
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?

The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"

This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)

Again, any help here much appreciated.
Thanks!

Jeff

Nov 16 '05 #3

P: n/a
One approach:

Use String.Split() with a comma as the delimiter, to break up the numeric
substrings into an array. Then iterate over the array from the end to the
beginning, to reassemble the substrings into the order you want. For
example:

string input = "1,2,3,4";
string output = "";
string[] substrings = input.Split(new char[] { ',' });
for ( int i = substrings.Length - 1; i >= 0; i-- )
{
if ( output != "" )
output += ",";
output += substrings[i];
}

Extra points if you use a StringBuilder instance to assemble the output.

HTH,
Tom Dacon
Dacon Software Consulting
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02...
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?

The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"

This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)

Again, any help here much appreciated.
Thanks!

Jeff

Nov 16 '05 #4

P: n/a
Try this

//convert to character array
char [] myArray = myString.ToCharArray()
//reverse the arrary
Array.Reverse(myArray);
//store in new string
string newString = new string(myArray);

Shak.
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02...
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?

The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"

This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)

Again, any help here much appreciated.
Thanks!

Jeff

Nov 16 '05 #5

P: n/a
Isn't the simple code always the most elegant? :-)

I mean who would have ever thought of checking the
documentation to determine which members of the
Array class are supported?

What will they think of next.

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:ud**************@tk2msftngp13.phx.gbl...
Try this

//convert to character array
char [] myArray = myString.ToCharArray()
//reverse the arrary
Array.Reverse(myArray);
//store in new string
string newString = new string(myArray);

Shak.
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02...
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?

The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"

This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)

Again, any help here much appreciated.
Thanks!

Jeff


Nov 16 '05 #6

P: n/a
clintonG <csgallagher@REMOVETHISTEXT> wrote:
Isn't the simple code always the most elegant? :-)

I mean who would have ever thought of checking the
documentation to determine which members of the
Array class are supported?

What will they think of next.


On the other hand, that will reverse 12,34 to 43,21 which I suspect
isn't what's wanted.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
The Reverse method would provide acceptable results on the string
of characters that was given but I would concur, in most any other
form of delineated characters a split would be required.

But that doesn't change the circumstances for we neophytes as I have
found that a quick trip to my local copy of the MSDN Library or
Google before posting to newsgroups has proven to be insightful and
other neophytes should be encouraged to do the same even if it means
beating them over the head with a mouse. My own lumps and bruises
are almost gone :-)

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
clintonG <csgallagher@REMOVETHISTEXT> wrote:
Isn't the simple code always the most elegant? :-)

I mean who would have ever thought of checking the
documentation to determine which members of the
Array class are supported?

What will they think of next.


On the other hand, that will reverse 12,34 to 43,21 which I suspect
isn't what's wanted.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8

P: n/a
Thanks for the help.
Honestly I would rather go here than MSDN which one can muddle through for
weeks looking for a simple answer. Sure if you need an extremely specific
answer and have 10 keywords it will find an example(great!) but for basic
questions its just lame and gives *too much* info IMO.

Actually I can honestly say that so far I have never gotten the answer I've
needed on anything whatsoever from MSDN. I always get lost in the ocean of
data. But then I'm a beginner. I'm sure there's tricks :-)
Thanks again,

Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:ug**************@tk2msftngp13.phx.gbl...
The Reverse method would provide acceptable results on the string
of characters that was given but I would concur, in most any other
form of delineated characters a split would be required.

But that doesn't change the circumstances for we neophytes as I have
found that a quick trip to my local copy of the MSDN Library or
Google before posting to newsgroups has proven to be insightful and
other neophytes should be encouraged to do the same even if it means
beating them over the head with a mouse. My own lumps and bruises
are almost gone :-)

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
clintonG <csgallagher@REMOVETHISTEXT> wrote:
Isn't the simple code always the most elegant? :-)

I mean who would have ever thought of checking the
documentation to determine which members of the
Array class are supported?

What will they think of next.


On the other hand, that will reverse 12,34 to 43,21 which I suspect
isn't what's wanted.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #9

P: n/a
Thanks all for the replies.
This variety of feedback is invaluable to a beginner like me :-)
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02...
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?

The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"

This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)

Again, any help here much appreciated.
Thanks!

Jeff

Nov 16 '05 #10

P: n/a
Oh, I finally got your point...
Sorry, a little slow getting this stuff. C# makes Oracle look like a
cakewalk :-)
Anyhow, yeah this solution would work on my string but not on ints over 10.
Thanks for pointing that out!

Jeff
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
clintonG <csgallagher@REMOVETHISTEXT> wrote:
Isn't the simple code always the most elegant? :-)

I mean who would have ever thought of checking the
documentation to determine which members of the
Array class are supported?

What will they think of next.


On the other hand, that will reverse 12,34 to 43,21 which I suspect
isn't what's wanted.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #11

P: n/a
Yep, that works nicely :-)
Thanks!

Jeff

"John Young" <polomint77ATAThotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

Is this the kind of thing you are after?

using System;

namespace Reverse
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string str = "1,2,3,4";
string result = "";
for (int i = str.Length - 1; i>=0; i--)
{
result += str.Substring(i, 1);
}
Console.WriteLine("Start string: " + str);
Console.WriteLine("Result string: " + result);
Console.ReadLine();
}
}
}
Hope that helps... Although there may be easier ways to do it...

John
"z_learning_tester" <so*****@microsoft.com> wrote in message
news:ej2Bc.71373$eu.3683@attbi_s02...
But I can't seem to find the answer.
The question is how do you reverse the words in a string?
Or how do you reverse the numbers listed in a string?

The example is usually something like:
Turn this string "1,2,3,4,..."
Into "...4,3,2,1"

This one seems hard enough let alone trying to turn a string of
space-seperated words around(is that even possible? a trick question
perhaps?)

Again, any help here much appreciated.
Thanks!

Jeff


Nov 16 '05 #12

P: n/a
I understand and agree that the MSDN website is humongous
but you should try to get a copy of the MSDN Library which
can be installed locally and is much more user freindly as it
does not include everything Microsoft has ever developed as the
MSDN website seems to.

A copy of the MSDN Library is distributed with MSDN subscriptions
and you may know somebody that can give you a copy of just the
library or dig into Microsoft to learn of you can obtain just a copy of
the MSDN Library itself. If you have Visual Studio.NET or have seen
its help system you were looking at a near equivalent to the
MSDN Library as both are deployed using Microsoft's HTML
Help 2.0 which is not being distributed unless embedded into one
of their products or distributed as the help for one of their SDKs.

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/


"z_learning_tester" <so*****@microsoft.com> wrote in message
news:IFRBc.88317$HG.66021@attbi_s53...
Thanks for the help.
Honestly I would rather go here than MSDN which one can muddle through for
weeks looking for a simple answer. Sure if you need an extremely specific
answer and have 10 keywords it will find an example(great!) but for basic
questions its just lame and gives *too much* info IMO.

Actually I can honestly say that so far I have never gotten the answer I've
needed on anything whatsoever from MSDN. I always get lost in the ocean of
data. But then I'm a beginner. I'm sure there's tricks :-)
Thanks again,

Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:ug**************@tk2msftngp13.phx.gbl...
The Reverse method would provide acceptable results on the string
of characters that was given but I would concur, in most any other
form of delineated characters a split would be required.

But that doesn't change the circumstances for we neophytes as I have
found that a quick trip to my local copy of the MSDN Library or
Google before posting to newsgroups has proven to be insightful and
other neophytes should be encouraged to do the same even if it means
beating them over the head with a mouse. My own lumps and bruises
are almost gone :-)

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
clintonG <csgallagher@REMOVETHISTEXT> wrote:
> Isn't the simple code always the most elegant? :-)
>
> I mean who would have ever thought of checking the
> documentation to determine which members of the
> Array class are supported?
>
> What will they think of next.

On the other hand, that will reverse 12,34 to 43,21 which I suspect
isn't what's wanted.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 16 '05 #13

P: n/a
Actually I have VS but didn't install the MSDN help due to my experiences
getting lost in the web site.
I just installed it and you're right, the distributed version is far more
condensed and useful.
Thanks for the tip!

Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
I understand and agree that the MSDN website is humongous
but you should try to get a copy of the MSDN Library which
can be installed locally and is much more user freindly as it
does not include everything Microsoft has ever developed as the
MSDN website seems to.

A copy of the MSDN Library is distributed with MSDN subscriptions
and you may know somebody that can give you a copy of just the
library or dig into Microsoft to learn of you can obtain just a copy of
the MSDN Library itself. If you have Visual Studio.NET or have seen
its help system you were looking at a near equivalent to the
MSDN Library as both are deployed using Microsoft's HTML
Help 2.0 which is not being distributed unless embedded into one
of their products or distributed as the help for one of their SDKs.

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/


"z_learning_tester" <so*****@microsoft.com> wrote in message
news:IFRBc.88317$HG.66021@attbi_s53...
Thanks for the help.
Honestly I would rather go here than MSDN which one can muddle through for weeks looking for a simple answer. Sure if you need an extremely specific answer and have 10 keywords it will find an example(great!) but for basic questions its just lame and gives *too much* info IMO.

Actually I can honestly say that so far I have never gotten the answer I've needed on anything whatsoever from MSDN. I always get lost in the ocean of data. But then I'm a beginner. I'm sure there's tricks :-)
Thanks again,

Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message news:ug**************@tk2msftngp13.phx.gbl...
The Reverse method would provide acceptable results on the string
of characters that was given but I would concur, in most any other
form of delineated characters a split would be required.

But that doesn't change the circumstances for we neophytes as I have
found that a quick trip to my local copy of the MSDN Library or
Google before posting to newsgroups has proven to be insightful and
other neophytes should be encouraged to do the same even if it means
beating them over the head with a mouse. My own lumps and bruises
are almost gone :-)

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
> clintonG <csgallagher@REMOVETHISTEXT> wrote:
> > Isn't the simple code always the most elegant? :-)
> >
> > I mean who would have ever thought of checking the
> > documentation to determine which members of the
> > Array class are supported?
> >
> > What will they think of next.
>
> On the other hand, that will reverse 12,34 to 43,21 which I suspect
> isn't what's wanted.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Nov 16 '05 #14

P: n/a
So there you go now :-)

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"z_learning_tester" <so*****@microsoft.com> wrote in message
news:JI0Cc.76665$Hg2.55932@attbi_s04...
Actually I have VS but didn't install the MSDN help due to my experiences
getting lost in the web site.
I just installed it and you're right, the distributed version is far more
condensed and useful.
Thanks for the tip!

Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
I understand and agree that the MSDN website is humongous
but you should try to get a copy of the MSDN Library which
can be installed locally and is much more user freindly as it
does not include everything Microsoft has ever developed as the
MSDN website seems to.

A copy of the MSDN Library is distributed with MSDN subscriptions
and you may know somebody that can give you a copy of just the
library or dig into Microsoft to learn of you can obtain just a copy of
the MSDN Library itself. If you have Visual Studio.NET or have seen
its help system you were looking at a near equivalent to the
MSDN Library as both are deployed using Microsoft's HTML
Help 2.0 which is not being distributed unless embedded into one
of their products or distributed as the help for one of their SDKs.

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/


"z_learning_tester" <so*****@microsoft.com> wrote in message
news:IFRBc.88317$HG.66021@attbi_s53...
Thanks for the help.
Honestly I would rather go here than MSDN which one can muddle through for weeks looking for a simple answer. Sure if you need an extremely specific answer and have 10 keywords it will find an example(great!) but for basic questions its just lame and gives *too much* info IMO.

Actually I can honestly say that so far I have never gotten the answer I've needed on anything whatsoever from MSDN. I always get lost in the ocean of data. But then I'm a beginner. I'm sure there's tricks :-)
Thanks again,

Jeff
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message news:ug**************@tk2msftngp13.phx.gbl...
> The Reverse method would provide acceptable results on the string
> of characters that was given but I would concur, in most any other
> form of delineated characters a split would be required.
>
> But that doesn't change the circumstances for we neophytes as I have
> found that a quick trip to my local copy of the MSDN Library or
> Google before posting to newsgroups has proven to be insightful and
> other neophytes should be encouraged to do the same even if it means
> beating them over the head with a mouse. My own lumps and bruises
> are almost gone :-)
>
> --
> <%= Clinton Gallagher
> A/E/C Consulting, Web Design, e-Commerce Software Development
> Wauwatosa, Milwaukee County, Wisconsin USA
> NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
> URL http://www.metromilwaukee.com/clintongallagher/
>
>
>
> "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
> news:MP************************@msnews.microsoft.c om...
> > clintonG <csgallagher@REMOVETHISTEXT> wrote:
> > > Isn't the simple code always the most elegant? :-)
> > >
> > > I mean who would have ever thought of checking the
> > > documentation to determine which members of the
> > > Array class are supported?
> > >
> > > What will they think of next.
> >
> > On the other hand, that will reverse 12,34 to 43,21 which I suspect
> > isn't what's wanted.
> >
> > --
> > Jon Skeet - <sk***@pobox.com>
> > http://www.pobox.com/~skeet
> > If replying to the group, please do not mail me too
>
>



Nov 16 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.