Reverse a string in C
In this post, you will learn how to reverse a string using C programming language. Such a type of question is generally asked in a programming interview. The interviewer may demand you to write various approaches to reverse a string, as they may ask you to use an inbuilt method or use a recursive method. Solving such problems can improve your logical skills and make us perfect for coding challenges.
There is predefined method strrev(), which can easily used to revere a string, but we should also know the logic. Logic in programming is a fundamental key to be a decent developer. Possibly, depending on your employment place you are going to use more algorithms or not. There are different ways to reverse a string in C programming. Here, we have mentioned most of them.
C program to reverse a string using strrev()
In the given C program, we have used the predefined strrev() method to reverse a string. This is a non-standard function that works only with older versions of Microsoft C.
#include <stdio.h>
#include <string.h>
int main()
{
// declaring the size of character string
char str[50], rev[50];
printf("\n Enter a string to be reversed: ");
scanf("%s", str);
// use strrev() function to reverse a string
rev = strrev(str);
printf("\n String After Reverse: %s ",rev);
return 0;
}
Output of the above code:
Enter a string to be reversed: Hello
String After Reverse: olleH
C program to reverse a string using user-defined function
In the given example, we have created an user defined function strrev() to reverse a string.
#include <stdio.h>
#include <string.h>
// Defining function strrev()
void strrev(char *str1)
{
// variable declaration
int i, len, temp;
// getting length of string
len = strlen(str1);
// using for loop to iterate the string
for (i = 0; i < len/2; i++)
{
temp = str1[i];
str1[i] = str1[len - i - 1];
str1[len - i - 1] = temp;
}
}
int main()
{
char str[50]; // size of char string
printf("\n Enter a string to be reversed: ");
scanf("%s", str);
// call strrev() function
strrev(str);
printf (" String After Reverse : %s", str);
}
Output of the above code:
Enter a string to be reversed: welcome
String After Reverse : emoclew
C program to reverse a string using recursion function
Recursion function is a function which is called by itself. In the given example, we call the recursion function to reverse a string. A recursion function continues until some condition is met to prevent it. That's why we use the if statement to break the infinite recursion.
#include<stdio.h>
// declaring recursive function
char* str_reverse(char* str);
void main() {
int i;
char str[50];
char *rev;
printf("\n Enter a string to be reversed: ");
scanf("%s", str);
printf("\n The original string :%s ",str);
rev = str_reverse(str);
printf("\n The reversed string : ");
puts(rev);
}
// defining recursive function
char* str_reverse(char *str) {
static int i = 0;
static char rev[100];
if(*str)
{
str_reverse(str+1);
rev[i++] = *str;
}
return rev;
}
Output of the above code:
Enter a string to be reversed: Peacock
The original string :Peacock
The reversed string : kcocaeP
C program to reverse a string using Pointer
In the given C program, we have used pointer to reverse a string. However, pointers are special variables that are used to store addresses rather than values, they cannot hold all the characters in a character array. This means that when we use a char * to keep track of a string.
#include<stdio.h>
#include<string.h>
void str_rev(char*);
int main() {
char str[50];
printf("Enter a string to be reversed: ");
scanf("%s", str);
str_rev(str);
printf("\n Reverse of the string : \"%s\".", str);
return 0;
}
void str_rev(char *s) {
int length, x;
char *begin, *end, temp;
length = strlen(s);
begin = s;
end = s;
for (x = 0; x < length - 1; x++)
end++;
for (x = 0; x < length/2; x++) {
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
Output of the above code:
Enter a string to be reversed: laptop
Reverse of the string : "potpal".
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative