0

Ques: I have a string "This is paragraph."

Desired Output: sihT si hpargarap.

Sample Code:

using System.Data;
using System.Text;
class ReverseString
{
  public static void Main()
  {
    string inputString="This is paragraph.";// input can be dynamic
    char[] x=inputString.ToCharArray();
    StringBuilder sb = new StringBuilder();
    for(int i=inputString.Length-1;i>=0;--i)
    {
      sb.Append(x[i]);
    }
    Console.Write(sb.ToString());
    Console.ReadKey();
  }
}

Please correct me.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445

4 Answers4

1
using System;
using System.Linq;
class Program
{
    static void Main(string[] args)
    {
        Console.Write("Enter the string: ");
        string inputString = Console.ReadLine();
        string outputString = string.Join(" ", inputString.Split(' ').Select(x=> new string(x.Reverse().ToArray())));
        Console.WriteLine("Output: "+ outputString);
        Console.ReadKey();
    }
}
1
string result = "";
string inputString = "This is paragraph.";
for (int i = inputString.Length - 1; i >= 0; i--)
{
    result += inputString[i];
}
Console.WriteLine(result);
Console.ReadLine();
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Isma Jan 05 '18 at 14:56
  • This code [does not work](https://stackoverflow.com/questions/22114707/). – Dour High Arch Jan 05 '18 at 18:51
1

To reverse the given string without using inbuilt function using C#

    string str="Welcome";
    char[] array = new char[str.Length];
    int j = 0;
    for(int i=str.Length-1;i>=0;i--)
    {
      array[j++] = str[i];
    }
    string reverseString = new string(array);

Output:

emocleW

0
string outString= string.Join(" ", inString.Split(' ').Select(x=> new string(x.Reverse().ToArray())));
Jason Armstrong
  • 1,058
  • 9
  • 17