| by Arround The Web | No comments

How to Reverse a String In C

“We use the reverse string function whenever we are required to change or reverse the order of string in a program. For example, if we have initially declared a char string of memory size 5 as “cloth”, now if we want to apply the string reversal operation on this string, then we will get the output string as “htolc”. There exist several methods to perform the string reversal in the C, and they are strev func (), recursion func (), and string reversal using the pointers. We can also verify the palindrome string using the string reversal methods. A palindrome is a string whose order of the characters remains unchanged even after the implementation of the string reversal operation on it.”

Procedure

We will get to know about the basic requirements in terms of syntax for string reversal functions by implementing the various methods of string reversal. Furthermore, we will also discuss how we can verify the palindrome string.

Syntax

This article deals with the various methods for string reversal, so we may not be able to define a general syntax for string reversal, but we will explain the syntax for each method of string reversal in the examples.

Example # 01

This example of the guide is going to deal with the implementation of reversing a string using the “strrev ()” method. The implementation of this example would be done by creating a project in Visual Studio C, which is a compiler for executing the C programs. When we create a new project in the visual studio C, we are required to add this project to the path of the C source files, and we can do it by saving the project name with the extension of the C file, e.g., “project. c”. With the completion of this step, we have to import certain libraries that would allow us to use their built-in functions that we may use later while developing a program.

In our case, we will be importing two of the important libraries; one is “$ # include <stdio.h>” for the input reading and output display functions, and the second library will be “ $ # include <string.h>” since we will be dealing with the strings and the string operation in this example. Now we will move forward to the next step, where we will declare the main function that will have a return type as an integer, and in this main function, we will write a program for the string reversal operation.

We will now declare a string having the data type “char” and will allocate it a memory of size “50”. We would not be initializing this string in the program; rather, we will just take the input from the user for this string, and we will do this by calling the scanf () method and passing the function arguments, format specification of the string as “%s” with name with which we had declared the string. Once we get input from the user for the string, then we will call the method strrev () and pass it to the string for which we had taken the input from the user. Then we display the results with the function printf (), and we will exit the main function by returning the value equal to zero. The following is an example code, which is written in the C language. Copy this code into your compiler and execute it to check the output.

#include <stdio.h>

#include <string.h>

int main()

{

    char    string[50];
    printf(" \n string to be reversed: ");
    scanf("%s", string);
    printf(" \n  reverse of a string: %s ", strrev(string));
    return 0;

}

We have given the input string as “maple” to the reverse string program, and the program then returned in the output the reverse string as “elpam.”

Example # 02

We will now again implement the example for the reverse string, but this time, we will be using another method for string reversal, and that would be the “recursion function.” Now we will create a new project in the C compiler and will include the two libraries:

$ # include <stdio.h>

$ # include <string.h>

Then in the project, we will develop a recursion function which we will call later on in the code for reversing the strings. Declare a function with the return type “void” and name it “reverse.” The parameters of this function will be char type pointer string as “char*string.” In the function, we will declare the three variables as i, l, and temporary_var, all having the same data type, “int.” We will call the strlen() function to take the length of the string that we had declared as the parameter of the function “reverse” passing this string to the arguments of strlen() we will get the length of the string.

We will then use the “if condition” on the index ‘i’ that, if ‘i’ is less than the length of the string divided in half, then store the string[i] in temporary_var, then we will swap the value of “string[length-i-1]” with the “temporary_var” value, will make an increment in the index ‘i’ as “i++” and call the reverse () function which will take the string in its argument. This step will let the program recursively call the reverse() function that we have created.

Now we will declare the main function and declare the size of the string that we declared in the reverse function as “40” we will take the input string from the user using the “gets()” function and will again call the “reverse” func for reversing the string that will move in the length of the string and will reverse the order of the string. After this function call, we will save its result and will display the reverse string with the printf() method. The example can be executed following the code mentioned below in the figure:

#include <stdio.h>

#include <string.h>

void   reverse(char* string){

    static int i, l, temptemporary_var;
   l = strlen(string);
    if (i < l / 2) {
        temptemporary_var = string[i];
        string[i] = string[l - i - 1];
        string[l - i - 1] = temptemporary_var;
        i++;
        reverse(string);
    }
}
int main()
{
    char  string[40];
    printf(" Enter the string: ");
    gets(string);
    printf(" \n Before reversing the string: %s \n", string);
    revstr(string);
    printf(" After reversing the string: %s", string);

}

After executing the above-mentioned code, we will give an input string to the program as “lighter,” which will then be returned by the program in the output after reversing its order as “rethgil”.

Conclusion

This article explains the concept of reversing the strings in the syntax of the programming language C. We have shown the two methods as the “recursion method” and the “strrev ()” method and implemented the examples for both these methods in this article. We have also discussed the palindrome string in the article. We are hopeful that you will find this article helpful for your concerns regarding string reversal operations in C.

Share Button

Source: linuxhint.com

Leave a Reply