How to Reverse a String In C

In string coding, string reversal is a crucial topic. There are also several methods for reversing a string, each of which has a different logic. This article will show you how to properly reverse a string to use several distinct approaches and reasoning with and without requiring any preset functions. Whenever the reversal of a string technique is applied in a particular unique input string, the text that a consumer would supply in a specific order will be completely reversed. In the instance beneath, different alternatives of reversing a string with C have been employed.

When implementing this guide, we have been working on Ubuntu 20.04 Linux system configured on Virtual Box. While working on a Linux system, we used to do most of our installations and queries on the terminal shell. Hence, open the terminal shell by a shortcut “Ctrl+Alt+T” or search it via an application search bar under the Activity area of the Ubuntu desktop. It is necessary to update your system first via the apt update query. It will ask you for the current user password to run the update. Hence, add a password and press Enter button.

$ sudo apt update

As we have been using the C programming language to elaborate the concept of reversing strings in the Ubuntu 20.04 system, one should have a C compiler installed on their Ubuntu system. Hence make sure to install the GCC compiler on your system via the below query.

$ sudo apt install gcc

Example 01: Using For Loop

After the installation and configuration of the GCC compiler and updating the “apt” package, it turns to create a new file. This file should be of C type; therefore, use the touch command to create a file “new.c” as below. This file will be used in our code from now on during the implementation of reverse string programs.

$ touch new.c

Now you can open this file for editing or coding by a Nano editor that has been built-in in your Ubuntu 20.04 Linux system. Therefore, try the instruction below in your shell to do so.

$ nano new.c

The newly created C-type file “new” has been opened in the GNU Nano Editor of the Ubuntu 20.04 system. You have to write a C script in it, as presented in the screenshot image beneath. Let us elaborate on this code for your better understanding. At the start of a code, we have included two libraries. The library “stdio.h” has been used to take inputs and display the outputs, and the other library “string.h” has been used to use all sorts of strings in our code. All the work of reversing a string will be done in the “main()” method of a C code. We have declared a character type string “str” having size 50. This means a string with more than 50 characters can not be entertained in this code. After that, we have declared two integer-type variables. The variable “l” has been used for collecting the length of a string “str” and, the variable “I” will be used as an initializer in for loop. Then we have used a printf statement to print a text on the shell “Enter string,” telling a user to add a value to a string variable.  The scanf() method has been used to input the user at run time and save that value into a string “str.”  The function “strlen()” has been used to check the length of a string “str” that has been added by a user at run time and save it into a variable “l’. Then we initialized a “for” loop to reverse the string “str.” You can see that the initializer “I’ is taking the value from a variable “l” to reverse the direction of a string. Then it prints the characters of a string “str’ one by one reversal. In the last, the main method has been ended. Save this file using “Ctrl+S” and quit it via “Ctrl+X.”

Now it’s time to compile the code and check for its output. Hence, the compilation has been done by the below-stated gcc query with the name of a file “new.c”.

$ gcc new.c

As the compilation gets successful, it means there is no error in our code. Let’s execute our file by “a.out” query in the shell as follows:

$ ./a.out

The execution process will ask the user to add a string. We have added “I-Am-Aqsa-Yasin” and pressed Enter. You can see it returns the reverse of a string at the following line.

Example 02: Using Swapping

In this example, we will be using the swapping method to reverse the order of a string. Hence open the file “new.c” once again using the nano editor as follows:

$ nano new.c

Now the file has been opened in the GNU nano editor; we need to update the code file with the below-shown script in the screenshot. We have included the standard input and output header file in our code first using a keyword #include. We have defined a method “Reverse()” as per the swapping technique’s usage. We have initialized three integer type variables “new,” “I,” and “swap.” The first “for” loop is used to check whether the string “s” is empty or not. The next for loop is used for iteration, and in its body, we have swapped the values using “new” and “swap” variables. It’s worth noting that reversing an n-length string just takes n/2 rounds. Once strings have been swapped, you’ll have to loop again to show the reversed string, which we do within our application using a third “for” loop. The function “Reverse()” must be called from the inside main program. You’ve stated what your software performs with printf inside the context of main(). After that, you used scanf() to get user input and invoked the Reverse() method. Now save the file again using Ctrl+S and leave the nano editor by Ctrl+X shortcut and return to the terminal shell.

Compile the code first by a gcc query as below.

$ gcc new.c

Now execute the code file usng the same “./a.out” instruction.

$ ./a.out

It will ask you to enter the string value. We have added “AqsaYasin” and got its reverse.

Example 03: Using Recursion

In this example, we will be using recursion to reverse a string added by a user. Hence open the file again.

$ nano new.c

This program prints “Enter a sentence:” A Reverse() method is then used. The initial letter inputted by a user is saved in c through this method. Reverse() is executed again if the argument is something other than \n (newline). This procedure continues till the user presses the Enter key. Whenever the user presses enter, the Reverse() method prints the text in reverse order. Save and close the file.

Compile the code first and then run it by using the previous commands as follows:

$ gcc new.c

$ ./a.out

You can see it takes input from a user as a whole string sentence and then reverse the order of that sentence.

Conclusion:

Finally, we have done three examples to elaborate on reversing a string type input with different methods, e.g., using for loop, using recursion, and using swap.



from Linux Hint https://ift.tt/3hzODOu

Post a Comment

0 Comments