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

  • Thread starter Thread starter z_learning_tester
  • Start date Start date
Z

z_learning_tester

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
 
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
 
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
 
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;
}

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

HTH,
Tom Dacon
Dacon Software Consulting
 
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.
 
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 said:
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 said:
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
 
clintonG said:
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.
 
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/
 
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 said:
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/
 
Thanks all for the replies.
This variety of feedback is invaluable to a beginner like me :-)
 
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
 
Yep, that works nicely :-)
Thanks!

Jeff

John Young said:
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 said:
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
 
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 said:
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
 
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
 
Back
Top