02/03/2023

Write a Program to Reverse a String in C | Top 3 Methods

Write a Program to Reverse a String in C Language. We are going to solve reverse words in a string using a while loop, using the strrev predefine function, reverse a string using pointers, and use recursion in C language. We are going to use 3 methods to solve this problem. The First method is a Program to Reverse a String Using Loops in C or Reverse a String in C  Using strrev and c program to reverse a string Recursion.

Write a Program to Reverse a String in C

Program to Reverse a String in C Using While Loop


#include <stdio.h>
#include <string.h>

void main()
{
	/*Write a Program to Reverse a String in C Using Loop */

	char str[100], temp;
	int i, j = 0;

	printf("Enter The String: ");
	gets(str);

	i = 0;
	j = strlen(str) - 1;

	while (i < j)
	{
		temp = str[i];
		str[i] = str[j];
		str[j] = temp;
		i++;
		j--;
	}

	printf("\nReverse a String Is: %s\n\n", str);

	return (0);
}

Reverse a String in C Using strrev


#include <stdio.h>
#include <string.h>

void main()
{
	/*Write a Program to Reverse a String in C */

	char str[100], temp;
	int i, j = 0;

	printf("Enter The String: ");
	gets(str);

	strrev(str);

	printf("\nReverse a String in C Is: %s\n\n", str);

	return (0);
}

Program to Reverse a String Using Recursion


#include <stdio.h>
#include <string.h>

void revstr(char *, int, int);

/*How to reverse a string in C Using Recursion */

int main()
{
	char str[100];

	printf("Enter The String: ");
	gets(str);

	revstr(str, 0, strlen(str) - 1);

	printf("%s\n", str);

	return 0;
}

void revstr(char *z, int start, int end)
{
	char ch;

	if (start >= end)
		return;

	ch = *(z + start);
	*(z + start) = *(z + end);
	*(z + end) = ch;

	revstr(z, ++start, --end);
}

The Output of Reverse a String in C


The Output of Reverse a String in C

How to Reverse a String in C


We can reverse a string without using the library function and strrev(). In this program, I am going to reverse the string with and without using strrev(). Firstly, we need to find the length of the string. After finding the length of the string run a loop from the last index to string the first index, and in each iteration decrease the last index by 1. 

So we can Reverse a String to the full string, this is a similar problem to reversing the sentence, and we can solve this problem by using a strrev() library function. There are many ways to solve this problem, we can solve this problem using Using Pointers, strrev(), and Recursion in the C. For IT Professionals A complete list of Amazon AWS certification exam practice test questions is available to Pass the Exam on the First Attempt Easily.

Special Case: There is a special case for this problem we need to know about it. Let's take an example of it. As we can see that the below program has more than 1 space between the words so our output should be according to the spaces given in a String.

Input Format


Programming With Basics Website

Output Format


Reverse a String is: etisbeW scisaB htiW gnimmargorP

Instead, for using the library function always solve the problem with your own logic, in this way your logic building will be implemented

Similar Reverse a String


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: