Click here to Skip to main content
13,445,309 members (37,505 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 2 Jan 2013

How to reverse a string sentence (only words, not characters) without using the C# "string built-in" functions

, 5 Jan 2013
Rate this:
Please Sign up or sign in to vote.
Reverse of a string sentence


This article represents one of few ways of reversal of string sentence (only words, without using any inbuilt c# functions except String.Length property as author feels lazy to write code for it.)


Knowledge of control structures (for, foreach, while) and data types (char, string).

Using the code

There are mainly three functions created :

  1. ReverseString( String ): returns reversed string, e.g., ReverseString("Hello How are you").
  2. Split( String ): returns a string array containing words and spaces at consecutive places.
  3. ReturnSpace( int ): accepts an integer representing number of spaces, creates and return a string containing specified number of spaces.
/**Reverse a Sentence without using C# inbuilt functions
 (except String.Length property, m not going to write code for this small functionality )*/
const char EMPTYCHAR = ' ';
const string EMPTYSTRING = " ";

/// <summary>
/// Reverse a string Sentence
/// </summary>
/// <param name="pStr"></param>
/// <returns></returns>
public string ReverseString(string pStr)
  if (pStr.Length > 1) //can be checked/restricted via validation
    string strReversed = String.Empty;
    string[] strSplitted = new String[pStr.Length];
    int i;

    strSplitted = Split(pStr); // Complexity till here O(n)

    for (i = strSplitted.Length - 1; i >= 0; i--)
    // this for loop add O(length of string) in O(n) which is similar to O(n)
        strReversed += strSplitted[i];

    return strReversed;
  return pStr;

/// <summary>
/// Split the string into words & empty spaces
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string[] Split(string str)
    string strTemp = string.Empty;
    string[] strArryWithValues = new String[str.Length];
    int j = 0;
    int countSpace = 0;

    //Complexity of for conditions result to O(n)
    foreach (char ch in str)
        if (!ch.Equals(EMPTYCHAR))
            strTemp += ch; //append characters to strTemp

            if (countSpace > 0)
                strArryWithValues[j] = ReturnSpace(countSpace); // Insert String with Spaces
                countSpace = 0;

            if (countSpace == 1)
                strArryWithValues[j] = strTemp; // Insert String with Words
                strTemp = String.Empty;

    strArryWithValues[j] = strTemp;
    return (strArryWithValues);

/// <summary>
/// Return a string with number of spaces(passed as argument)
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public string ReturnSpace(int count)
    string strSpaces = String.Empty;

    while (count > 0)
        strSpaces += EMPTYSTRING;

    return strSpaces;


/************Reverse Sentence Ends***************/

Similar post

Points of Interest

Maintaining complexity to O(n) where n is length of string.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

GeneralReverse the individual words in a sentence Pin
Member 1213583012-Nov-15 16:47
memberMember 1213583012-Nov-15 16:47 
SuggestionAnother suggestion Pin
Malte Klena9-Jan-13 7:08
memberMalte Klena9-Jan-13 7:08 
SuggestionHow about this... Pin
Andreas Gieriet5-Jan-13 12:11
memberAndreas Gieriet5-Jan-13 12:11 
QuestionRepeat Pin
jfriedman4-Jan-13 4:15
memberjfriedman4-Jan-13 4:15 
AnswerRe: Repeat Pin
PIEBALDconsult4-Jan-13 4:35
memberPIEBALDconsult4-Jan-13 4:35 
GeneralRe: Repeat Pin
jfriedman4-Jan-13 5:21
memberjfriedman4-Jan-13 5:21 
GeneralRe: Repeat Pin
PIEBALDconsult4-Jan-13 10:51
memberPIEBALDconsult4-Jan-13 10:51 
GeneralRe: Repeat Pin
VeeQuest5-Jan-13 9:24
memberVeeQuest5-Jan-13 9:24 
SuggestionUse StringBuilders Pin
John Brett2-Jan-13 22:06
memberJohn Brett2-Jan-13 22:06 
GeneralRe: Use StringBuilders Pin
adriancs3-Jan-13 7:46
memberadriancs3-Jan-13 7:46 
GeneralThoughts Pin
PIEBALDconsult2-Jan-13 16:38
memberPIEBALDconsult2-Jan-13 16:38 
QuestionGood one Pin
maq_rohit2-Jan-13 14:53
membermaq_rohit2-Jan-13 14:53 
AnswerRe: Good one Pin
VeeQuest5-Jan-13 9:26
memberVeeQuest5-Jan-13 9:26 
GeneralRe: Good one Pin
maq_rohit9-Jan-13 19:28
membermaq_rohit9-Jan-13 19:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180314.2 | Last Updated 5 Jan 2013
Article Copyright 2013 by VeeQuest
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid