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.

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

2 Comments

While this code snippet may solve the question, including an explanation 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.
This code does not work.
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

Comments

0
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();
    }
}

Comments

0
string outString= string.Join(" ", inString.Split(' ').Select(x=> new string(x.Reverse().ToArray())));

1 Comment

Welcome to SO. Can you add a little explanation as to what each statement is doing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.