Q:

Program to Reverse a String in C | Using Loop, Recursion, and Strrev()

belongs to collection: String in C Programs

0

C Program to Reverse a String in C programming language. We are going to solve reverse words in a string using while loop, using strrev predefine function, reverse a string using pointers, and using recursion in C language.

Reverse a String- Table of Content

 

-Reverse a String Logic Explanation

-Words Reverse in a String Using While Loop C Program

-C Program to Reverse a String Using Strrev Function

-Reverse a String Using Recursion in C

-Output of Code

 

Reverse a String Logic Explanation

 
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 the full string, this is a similar problem to reverse 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 C programming language.

Special Case: There is an 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 spaces between the words so our output should be according to spaces are given in a String.

Input Format

Programming   With  Basics Website

Output Format

etisbeW scisaB  htiW   gnimmargorP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

void main()
{

   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 Words in a String Is: %s\n\n", str);
   
   return (0);
}

 

 

Output:

Enter The String : nerdutella examples in c programming

Reverse String Is:gnimmargorp c ni selpmaxe alletudren

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to Compare Two Strings Using strcmp... >>
<< C Program For Reverse A String Using Library Funct...