473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to reverse the words in a string

20
I am trying to reverse the location of words in a string which i have working but not sure if it's the best way. I can't use Array.Reverse or String.Split in the code. The idea is to return a string with the words reversed. eg: “The quick brown fox” should return “fox brown quick The”

Expand|Select|Wrap|Line Numbers
  1. public string ReverseWordsInString (string input)
  2.     {
  3.  
  4.         StringBuilder result = new StringBuilder();
  5.  
  6.         //remove all potentially multiple whitespace from beginning
  7.         //and end of string so indexof test does not match empty whitespace
  8.         input = input.Trim();
  9.  
  10.         //next insert a single blank space at the end of the string 
  11.         //to satisfy the last indexOf test.
  12.         input = input.Insert((input.Length), " ");
  13.  
  14.         while(input != "")
  15.         {
  16.             //find location of whitespace to identify end of word
  17.             int t = input.IndexOf(" ");
  18.  
  19.             //input the word into result string (use t + 1 to include the space)
  20.             result.Insert( 0, (input.Substring(0 , (t + 1))));
  21.  
  22.             //removed the word just retrieved for the next loop
  23.             input = input.Remove(0, (t + 1));
  24.             t = 0;
  25.         }
  26.         return result.ToString();
  27.     }

Is it better to use stringbuilder for both operations for efficiency. Shall I cast the input string to a stringbuilder?

Not sure if using a "while string not null" test is a good idea in the while loop. Also I would like to keep this to just 1 function answer.
May 6 '10 #1
5 3485
hype261
207 100+
@pinman
Because of the memory concerns you might want to change the input string to a StringBuilder class. From looking at the documentation the StringBuilder doesn't have a Trim function so you would have to do.

StringBuilder sbInput = new StringBuilder(input.Trim());

This would make your removal of the previous words more efficient.

I don't believe the while string not null test would work though in your looping mechanism to remove words because presumably you a string or stringBuilder that has been allocated on the heap.

I am not sure if the String Trim member function will throw an exception if it is given a null string, but you should be checking that yourself before your start your parsing.
May 6 '10 #2
Plater
7,872 Expert 4TB
Have you considered using the .LastIndexOf function?
May 6 '10 #3
pinman
20
The code works fine using the while loop as I've tested it with lots of input strings but I was just wondering if it was the best way of doing things by testing for a null string to end the loop and was thinking of a forloop maybe.

I've used a stringbuilder for the "result" string and was thinking of doing the same thing for input string so thanks for letting me know about the trim and also for the test for a null input string.

@Plater the lastindex of would give me the last position of the whitespace would it? So would I then take out the word beginning at this whitespace forward and then repeat?
May 6 '10 #4
Plater
7,872 Expert 4TB
That was the idea i was going for. I hadn't really thought it through, just know that starting with the last word in the original is easier for what you are doing i think
May 6 '10 #5
tlhintoq
3,525 Expert 2GB
This is very obviously a school homework project.

Bytes has a policy regarding assisting students with their homework.

The short version is that the volunteers here can't help you with schoolwork.
A) We don't know what material you have and have not learned in class.
B) We don't know the guidelines you must follow.
C) In the long run giving you the answers actually short changes your education.

Hint 1: Try hitting Google with terms of your programming language and primary terms of what you want to do. For example "C# custom events" or "VB datagrid Excel". I've found this to be a very effective tool.
Hint 2: Your text book
Hint 3: Your instructor
Hint 4: Posting guidelines regarding homework assignments.
May 6 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Filip Dreger | last post by:
Each function has a func_code property that is suposed to contain the pure bytecode of the function. All the context (including reference to relevant namespaces) is stored in different fields of...
7
by: john | last post by:
On my form i have a message box called txtItemDesc that displays the french phrase qualité Père Noël. Now then when i run this code on that text box: Dim chrArr() As Char chrArr =...
2
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but...
6
by: Bala Nagarajan | last post by:
Hello, I am using Oracle 9i in my application and facing a problem with the connection string. In the datasource attribute of the connection string i had to specify an entry in "tnsnames.ora"...
2
by: wk | last post by:
Hi, I am reading a mp3 file's last 128 bytes and putting the text into string variables. What is happening is that the string is being stored with, probably some wrong encoding or character, and...
8
by: kujahleague | last post by:
Dear all I'm new here and also new to C language. I have been stuck with string problem in C for some time and I decide to post here to get some help, please kindly explain what I get wrong with the...
2
by: Fred | last post by:
Hello, I am having a problem with a multiline string that I create from a byte array (xml file). I am using StringBuilder and Ecoding the string to ASCII (code below). / Upload the...
5
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi, I posted last week about a problem I've got and didn't get any responses, but I've managed to come up with a workaround and a possible cause (guessing). I was wondering if someone could...
3
by: emlinux | last post by:
hai friends iam new this field of software engineering , i have a problem in string operations let me explain generally strings are terminated by Null but when iam receving a command from a buffer...
0
by: kunaltilak | last post by:
hello sir, please help me out for a asp.net(c#) problem. actually i want to make an online test series. for that i want to paste each question with options into rich textbox. each question has 4...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.