Program to Reverse string in C Language

program-to-reverse-string-in-c-language

Program Description:

Write a Program to Reverse String in C programming language. The program should accept a string from the user and reverse its characters.

📢 This Program is part of the C String Practice Programs series

Here is the example input and output of the program.

Example Input and Output:

Input:

Enter a string : learn programming

Output:

Reversed String: gnimmargorp nrael

As you can see from the above input and output, The output string is the exact reverse of the input string.

Prerequisites:

It is recommended to have knowledge of the C-Strings and Arrays. Please go through the following articles to learn more about it.

Algorithm to Reverse string in C:

We can Reverse a String by using Two pointer approach.

  1. Create two variables called start and end. Which stores the indices.
  2. The start variable stores the first element in the string(Zeroth Index). The end variable stores the last element in the string.
  3. Create a Loop to iterate over the string.
    • Swap the start index and end index elements. Now The first character and last character are swapped. We are using a temporary variable to swap the elements, But you can use other methods to Swap as well.
    • Then increment the start element, so that it pointed to the next element in the string. Similarly, Decrement the end, So that it points to the second last element.
  4. Repeat the above step until the start index becomes equal to or greater than the end index. (start < end)
  5. Once the above loop is completed, The input strings will be Reversed.

Reverse string in C Using Iterative Method:

In the following program, We used the above string reverse algorithm. Note, That We used the variables i and j in place of the start and end in the above example.

We declared a string called inputStr and took the user input and updated it. Then we used the above String Reverse Two Pointer approach to Reverse the string.

Finally, We displayed the Reversed string on the console.

Program Output:

Let’s compile and Run the Program

reverse-string-in-c-program-output

As we can see from the above output When the user entered the input string as Welcome, The program reversed the string and provided the output as emocleW.

Program to Reverse string in C using User-defined functions:

In this method, We are going to move the string reverse logic to a user-defined function called stringReverse.

The stringReverse function takes a string(i.e character pointer) as a formal argument and reverses the given string and returns it back to the caller.

Prototype of the stringReverse function:

Here is the program to reverse a string in the C language using a custom function called stringReverse.

The stringReverse function uses the above two-pointer approach to reverse the string. It uses two variables called i and j to maintain the start and end indices and Swaps the characters pointed by the i and j indices, Then it increments the i and decrements j. This process continues until the i<j condition becomes false. Finally, stringReverse function returns the resultant string back to the main() function.

📢 We used the strlen string library function in the above program. But if you don’t want to use any library functions, Then you can implement and use custom strlen function also.

Here is the article, Where we implemented a custom strlen function( string length function )

Program Output:

Let’s compile and Run the program using GCC.

string-reverse-in-c-using-user-defined-functions

Program is generating the desired results.

Reverse String using strrev function in C Language:

We can also use the strrev function to reverse a string in C language. But the strrev() function is a non-standard C function. It is a non-standard function from Microsoft’s C-Lang library. So it might not work on all implementations (compilers/environments)

The strrev(str) function takes a string as input and reverses its characters.

You can use the following program on compatible C Implementations (Compilers) to Reverse the string using the strrev function.

Related String Practice Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Reverse a String […]

Leave a Reply