w3resource

C#: Print a string in reverse order

C# Sharp For Loop: Exercise-57 with Solution

Write a program in C# Sharp to print a string in reverse order.

C# Sharp: Print a string in reverse order

Sample Solution:-

C# Sharp Code:

using System;  // Importing necessary namespace

public class Exercise57  // Declaration of the Exercise57 class
{  
    public static void Main()  // Main method, entry point of the program
    {
        string str, str1 = "";  // Declaration of string variables
        int i, l;  // Declaration of integer variables
		
        Console.Write("\n\n");
        Console.Write("Print a string in reverse order:\n");
        Console.Write("----------------------------------");
        Console.Write("\n\n");				

        Console.Write("Input a String: ");
        str = Console.ReadLine();  // Taking user input for a string

        l = str.Length - 1;  // Finding the length of the string

        // Loop to reverse the given string
        for (i = l; i >= 0; i--)
        {
            str1 = str1 + str[i];  // Building the reversed string
        }

        // Displaying the reversed string
        Console.WriteLine("The string in Reverse Order Is: {0}", str1);
        Console.Write("\n");	
    }
}

Sample Output:

Print a string in reverse order:                                                                            
----------------------------------                                                                          
Input  A String : W3RESOURCE                                                                                
The string in Reverse  Order Is : ECRUOSER3W 

Flowchart:

Flowchart: Print a string in reverse order

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to Check Whether a Number can be Express as Sum of Two Prime Numbers.
Next: Write a C#Sharp program to display alphabet pattern like 'A' with an asterisk.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/for-loop/csharp-for-loop-exercise-57.php