EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Reverse String in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Reverse String in C

Reverse-String-in-C

Introduction to Reverse String in C

Reverse String can be defined as a operation in which the original string which is given by the user is modified in such a way that the characters in it are arranged in a reverse manner starting from the last character to the first character thus by forming a new string which will be the exact reverse of the original string while in case of a C language the reverse string algorithm will take input as string and apply the algorithm to reverse it character by character and then it will return the newly formed reversed string to the user.

We can apply the same logic as mentioned in the definition, to reverse a string we can traverse characters in a string from end to start and append one after one. This way we will be having a new string which is formed by reverse traversal and this string will be the reversed string. In C language as we don’t have support for a string data type, we need to use character array instead. It is easy here to traverse the character array character by character and form a new character array.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Examples of Reverse String in C

Following are the different examples of reverse string in c using various methods.

Example #1 – Using For Loop

Code:

#include <stdio.h>
#include <string.h>
int main ()
{
// char array to take input
char inputString[100];
// char array to build output
char outputString[100];
int length;
int i;
// Take input from the user : input in character array
printf( "Please Enter a string to be reversed \n" );
scanf( "%s", inputString );
// Find the number of characters or length of a string using in built function strlen() from string.h library
length = strlen( inputString );
int j = 0;
// Traverse character by character from end to start and form a new string
for( i = length - 1; i >= 0; i--) {
outputString[ j ] = inputString[ i ];
j++;
}
printf( "The reversed string is: ");
printf( "%s", outputString );
printf( "\n" );
return 0;
}

Output:

Reverse String in C 1-1

Here, we have used strlen() from the <string.h> library to find out the count of characters present in the input string and passed it in for loop. Using for loop we have parsed array from end to start and appended characters in reverse order in an output array

Example #2 – Using While Loop

Code:

#include <stdio.h>
#include <string.h>
int main ()
{
// char array to take input
char inputString[100];
// char array to build output
char outputString[100];
int length;
int i;
// Take input from the user : input in character array
printf( "Please Enter a string to be reversed \n" );
scanf( "%s", inputString );
// Find the number of characters or length of a string using in built function strlen() from string.h library
length = strlen( inputString );
int j = 0;
// Traverse character by character from end to start and form a new string
i = length - 1;
while( i >= 0) {
outputString[ j ] = inputString[ i ];
i--;
j++;
}
printf( "The reversed string is: ");
printf( "%s", outputString );
printf( "\n" );
return 0;
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

Reverse String in C 1-2

Example #3 – Using do While Loop

Let’s modify the same code with a do-while loop.

Code:

#include <stdio.h>
#include <string.h>
int main ()
{
// char array to take input
char inputString[100];
// char array to build output
char outputString[100];
int length;
int i;
// Take input from the user : input in character array
printf( "Please Enter a string to be reversed \n" );
scanf( "%s", inputString );
// Find the number of characters or length of a string using in built function strlen() from string.h library
length = strlen( inputString );
int j = 0;
// Traverse character by character from end to start and form a new string
i = length - 1;
do {
outputString[ j ] = inputString[ i ];
i--;
j++;
}while( i >= 0);
printf( "The reversed string is: ");
printf( "%s", outputString );
printf( "\n" );
return 0;
}

Output:

Reverse String in C 1-3

We can’t enter an empty string in input because C language will not allow it.

Example #4 – Using Swapping

Code:

#include <stdio.h>
#include <string.h>
int main ()
{
// char array to take input
char inputString[100];
int length;
int i;
// Take input from the user : input in character array
printf( "Please Enter a string to be reversed \n" );
scanf( "%s", inputString );
// Find the number of characters or length of a string using in built function strlen() from string.h library
length = strlen( inputString );
// swap characters from start with characters from end
int j = length -1;
char temp;
for( i = 0; i <= (length-1) /2; i++) {
temp = inputString[i];
inputString[i] = inputString[j];
inputString[j] = temp;
j--;
}
printf( "The reversed string is: ");
printf( "%s", inputString );
printf( "\n" );
return 0;
}

Output:

Reverse String in C 1-4

Here, we are not using any extra character array for storage. We are modifying the existing input character array by swapping the characters from the start with the characters from the end. We need to use only one extra memory space for storage in this case.

Conclusion

String reverse is an operation in which the sequence of characters in a string is reversed. C language provides efficient ways to implement and perform this operation. In this article, we have seen various methods by which we can perform the reverse operation.

Recommended Articles

This is a guide to Reverse String in C. Here we discuss the Introduction along with different examples and code implementation. You may also look at the following articles to learn more –

  1. Reverse String in Java
  2. Reverse String in PHP
  3. Reverse String in JavaScript
  4. Reverse String in C++

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More