c sharp

C# Reverse a String

In C# programming, reversing string means the original string is arranged so that the characters in the string are converted in reverse order, such as the original string “hello” is converted in reverse order and the reversed string is “olleh.” The reversed string starts from the last character of the original string. We can reverse the string by using different methods. In this article, we are going to show you how to reverse the string with and without built-in methods in C# programming.

Methods for Reversing a String

We have different methods to reverse a string in C# programming.

Example # 1: Using For Loop

We reverse the string by using the For Loop method in this example. We are going to perform these examples in Ubuntu 20.04. We use the text editor of Ubuntu 20.04 for writing the code. We must save our file with the file extension of “.cs.”

We start our C# program with the “using System” statement. This is the library in C# for accessing functions and classes used in our program. We must use this library in our C# program. Then we have a “namespace” named “Csharp.” The “namespace” is a keyword utilized to state a scope that includes a set of associated objects. Also, for organizing our code, we use “namespace.” We are starting a class with the title “Demo” and have a “main” function. We must invoke a “main” function in our C# program. We have to get the string from the user, so for this, we first print a line on the screen to tell the user that he/she has to enter the string. We print the line “Enter a String:” by using “Console.WriteLine”. It gets the input from the user and stores it in “orgStr,” which is the string data type.

We initialize this “string orgStr” with this “Console.WriteLine()” statement. After this, we declare another string named “revStr” with “string.Empty,” which means we declare a blank string. Now, we use “for loop” for reversing our original string. This “for loop” is used to iterate each character of the string in reverse order. Inside for loop, we take “i” of integer data type equal to “Length – 1,” where “Length” represents the length of the input string. We use “Length – 1” because the index starts from zero. Then we have a condition “i>=0,” which means “i” must be greater than or equal to “0”. After that, we want to come to one value less than the previous value, so for this, we use “i–.” It will decrease our value by “1”.

Now, in the next line, we are going to append every character to the “revStr.” After this, we have to print this reversed sting, so for this, we use the “Console.WriteLine()” method. Then close all the brackets and save this.

We can get the output of this code by the “mcs” and the “mono” commands. We run both commands on the terminal of Ubuntu 20.04. When we use the “mcs” command, we must put the “.cs” file extension, and with the “mono” command, we use the “.exe” file extension with the title of the file. The “mcs” command compiles our code and generates an executable file, and then the executable file is executed with the help of the “mono” command. The output of the overhead code is specified below.

When both commands run successfully on the terminal, it prints the line “Enter a String.” After this, the user enters “hello” and presses “Enter.” Then reverses this “hello” string and places the last character first. In other words, the input string is arranged in reverse order.

Example # 2: Using ArrayReverse() Method

We have another example in which we reverse our string by using the ArrayReverse() Method in C# programming. This is an additional way to reverse a string. We are going to enlighten this example in detail.

Starting our C# program with the library “using System,” which we have explained in the first example. The “namespace” here is titled “Program” and then declares a class with the title “Csharp.” We generate a method “Reverse” and provide a string named “text” here. It accepts this “string text” as an input. In the next line, we create a char array “char[ ]” with the name “charArray” and convert the string to a character array by using the “ToCharArray()” method. Then we invoked an “Array.reverse()” method, which is used to reverse the “charArray.”

After reversing this “charArray,” we use the “return” statement, which returns the reversed array in a new string. Then we are going to invoke our “main” function. In this “main” function, we declare a string with the name “Original” and initialize it with a string which is “this is our string.” First, we print this original string on the terminal by using the “Console.WriteLine()” method, and then we have to declare a new string with the name “Reversed” and store the reverse values in this “Reversed” string.

Then we call this Reverse() method. It will reverse our original string and store it in the “Reversed” string variable. Then we again use this “Console.WriteLine” for printing this reversed string.

In this output, you can see that, first, it prints the original string and then reverse this original string. It prints the last character first and then prints all the characters in reverse order.

Example # 3: Using the foreach loop Method

In this example, we use the third method to reverse a string. We use the “foreach” loop method for reversing our original string. Let’s have a look at this example and learn another method for reversing the string in C# programming.

The namespace named “Csharp” and declared a class with the name “Demo” here. Then invoked our “main” function. After this, we are going to get the input from the user, so we have to print one line on the screen, which is “Enter a String,” by using the “Console.WriteLine” statement so that the user will give the input and we get this input by using “Console.ReadLine()” and stores this user’s input in a string variable named “originalStr.”

We create another string variable with the name “reverseStr,” which is empty here. The code is the same as we deliberated in our first illustration. The “foreach” loop stores the characters of the “originalStr” in “c.” And we have “reverseStr,” which stores this “c + reverseStr.” Then we have to print this reverse string. So, we use “Console.WriteLine”. The reversed string will be rendered on the screen after using this “Console.WriteLine”. You can check the output of the overhead code in the image given underneath.

When the user types the string here, and presses enter, the reversed string is presented on the screen.

Conclusion

In this article, we have learned different methods for reversing a string in C# programming. We presented different examples. In each example, we have used a different method for reversing the string. We have explained all these examples in detail. We give you the output and code so that you can see how these methods work. There are many other methods for reversing the string. Here we have explained three methods in detail. So you can easily learn this concept after a thorough study of this article, which will help you a lot.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.