C program to reverse a string

devquora
devquora

Posted On: Nov 30, 2020

C program to reverse a string

 

Overview:

As we know that time is the most important concern in our life. To do any task easily, We have to use software to perform it in a simpler manner. Software is a set of programs with related documents. The Software Program or Program is a set of instructions to do a specific task. Here, our specific task is to reverse a string. First, I want to explain What is String? Then, I will explain What do you mean by Reverse? Moreover, I will explain What is a palindrome? Furthermore, I will introduce you to What is a Program? After all, I will write the logic of a C program to reverse a string and explain it with output.

Table of contents:

  1. What is a String?
  2. What do you mean by Reverse?
  3. What is a palindrome?
  4. What is a Program?
  5. Demonstration of Reverse a string.
  6. C program to reverse a string by using  C Library function strrev()
  7. C program to reverse a string by using  Array
  8. Explanation of the C program to reverse a string with output.

What is a String?

The String is a combination of characters that may be any digit from 0 to 9 and Alphabets from A to Z. The repetition of characters does not matter. For instance, a String Conax uses 5 characters i.e. C,o,n,e,and x.

What do you mean by Reverse?

Reverse means move backward and turned toward the direction opposite to that previously stated. For instance, If our string is Conax then its reverse string is Xanoc.

What is a palindrome?

A Palindrome is an approach in which the reversed string is the same as the previous string. For instance, the Reverse of a string 10101 is also 10101.

What is a Program?

A Program is a set of instructions to perform a specific task. A Program tells the Computer What to do? And a Logic tells the computer How to do it? To Write a Program We will construct our own logic related to the task.

Demonstration of Reverse a string.

  • Step 1: Take a String
  • Step 1: Move backward the last character to the previous string.
  • Step 2: Turned toward the direction opposite to it. That became the last character to the previous one as so on.
  • Step 3: At last Write the continuous process as a string until the last characters as a sequence.

Example: Demonstration of Reverse a string Conax.

Demo Reverse String

C program to reverse a string by using  C Library function strrev()

#include <stdio.h>
#include <string.h>
int main()
{
   char s[100];

   printf("Please! Enter a string that you want to reverse: ");
   gets(s);

   strrev(s);

   printf("The Reverse of the entered string is: %s", s);

   return 0;
}

Save this program with the filename and extension .c like filename.c. Then compile it on turbo C or another compiler.

The output of the Program with code:

Please! Enter a string that you want to reverse: Conax

The Reverse of the entered string is: xanoC

C program to reverse a string

 

C program to reverse a string by using  Array

#include <stdio.h>

int main()

{

   char s[1000], r[1000];

   int start, last, count = 0;

   printf("Please! Input a string: ");

   gets(s);

   // Calculating string length

   while (s[count] != '\0')

      count++;

   last = count - 1;

   for (start = 0; start < count; start++) {

      r[start] = s[last];

      last--;

   }

   r[start] = '\0';

   printf("The Reversed String is:%s\n", r);

   return 0;

}

The output of the Program with code:

Please! Enter a string that you want to reverse: Conax

The Reverse of the entered string is: xanoC

ReverseStringArray

Conclusion: Explanation of the C program to reverse a string with output.

The internal working of this program depends upon the previous demonstration of Reverse a string.

After the successful compilation of the program, a message is displayed on the screen as Please! Enter a string that you want to reverse: Suppose, User, entered Conax then again a message is displayed as Reverse of the entered string is: xanoC.

    Related Questions

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..

    C Program Examples

    C program to reverse any number by using function

    This Program reversed any number such as Integer, float, or decimal in their equivalent. After the successful compilation of the program, a message is displayed on the screen as Please! Enter a string..