65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Jan 2, 2013

CPOL
viewsIcon

74161

Reverse of a string sentence

Introduction

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.)

Background

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
                j++;
                countSpace = 0;
            }
        }
        else
        {
            countSpace++;

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

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

    return strSpaces;

}

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

Similar post

Points of Interest

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