473,581 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
14 2282
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.WriteLi ne("Start string: " + str);
Console.WriteLi ne("Result string: " + result);
Console.ReadLin e();
}
}
}
Hope that helps... Although there may be easier ways to do it...

John
"z_learning_tes ter" <so*****@micros oft.com> wrote in message
news:ej2Bc.7137 3$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
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_tes ter" <so*****@micros oft.com> wrote in message
news:ej2Bc.7137 3$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
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.Leng th - 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_tes ter" <so*****@micros oft.com> wrote in message
news:ej2Bc.7137 3$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
Try this

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

Shak.
"z_learning_tes ter" <so*****@micros oft.com> wrote in message
news:ej2Bc.7137 3$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
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**@fakedomai n.com> wrote in message
news:ud******** ******@tk2msftn gp13.phx.gbl...
Try this

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

Shak.
"z_learning_tes ter" <so*****@micros oft.com> wrote in message
news:ej2Bc.7137 3$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
clintonG <csgallagher@RE MOVETHISTEXT> 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
clintonG <csgallagher@RE MOVETHISTEXT> 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
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 ************@me tromilwaukee.co m> wrote in message
news:ug******** ******@tk2msftn gp13.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
clintonG <csgallagher@RE MOVETHISTEXT> 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #9
Thanks all for the replies.
This variety of feedback is invaluable to a beginner like me :-)
"z_learning_tes ter" <so*****@micros oft.com> wrote in message
news:ej2Bc.7137 3$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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1929
by: Vernon | last post by:
Hi anyone, This is my code. int num num = 0; for(int a=0; a<count; ++a){ if(current->transID==aNo) ++num; current = current->next;
4
2694
by: Chefry | last post by:
I'm trying to set up an off the shelf script and keep getting an error. My host set up the mysql on my site and I changed the variables I had to in the settings.php file but I keep getting the following errors Warning: mysql_pconnect(): Access denied for user: 'ODBC@localhost' (Using password: NO) in...
2
2019
by: iainw | last post by:
HI All, 1st post here, i wonder if you can help. We are about to upload CMS t a windows server and keep getting 2 errors below. We need to go LIVE an it's delaying us. An error occured when updating the database. The likely reason is that write permissions are not set on th 'database' folder. Please remember that write and delete...
1
1401
by: amerar | last post by:
Hi All, I posted a question about style sheets, and why certain email clients were ignoring them. Someone suggested placing them inline. I did this and get better results, but not what I wanted. The page still appears properly, and it shows in Netscape Messenger just fine, but on Hotmail and Yahoo, each <DIV> tag does not appear where...
1
15437
by: George W. | last post by:
Okay, I'm a C#/XML newbie, and I've been wrestling with this for a while now, checked dotnet sites, articles, MSDN Library, etc. and haven't been able to determine why this is happening. I have an xml file that I am loading into an XmlDocument and then trying to add a child element to the root element. I keep getting:...
2
2099
by: nfr | last post by:
I keep getting the following warning in my compile: Warning: The dependency 'WBWebServices, Version=1.0.1289.13943, Culture=neutral' in project 'WBWin' cannot be copied to the run directory because it would overwrite the reference 'WBWebServices, Version=1.0.1289.15088, Culture=neutral'. If I delete and readd the reference, it compiles fine...
2
3791
by: partybob99 | last post by:
I am trying to call SP_Password from some vb.net code. This should be very straight forward but no matter what I do, I keep getting errors. Here is the code strConnectString = "Data Source=" + strServer + ";Initial Catalog=master;user id=" + strID + ";password=" + strOldPass + ";" Conn.ConnectionString = strConnectString Conn.Open()
1
2028
by: onur karabulut | last post by:
Hi, I have a simple question for you guys. In my WinForms app, I'm using WndProc to trap certain Windows messages. I sometimes need to block the Main UI-Thread (waiting for a worker thread or a modal dialog box to close). WndProc stops working once that happens. Is there a way to keep getting Windows messages while the UI-thread is suspended? ...
0
7862
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7789
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8301
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3803
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3820
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1400
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1132
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.