write a program for reversing of given string in c

20 Sep 2022 Balmiki Mandal 0 C Programming

C program for reversing the given string

Introduction

This program demonstrates how to reverse a given string in C. Reversing a string is the process of rearranging the characters of a string in reverse order. This is a common operation in C programming, and it is used in many different applications, such as text processing, data manipulation, and web development.

Example

How to reverse a string using a for loop:

C Programming
#include <stdio.h>

int main() {
  char str[100];
  int len, i;

  printf("Enter a string to reverse: ");
  scanf("%s", str);

  len = strlen(str);

  // Reverse the string using a for loop
  for (i = 0; i < len / 2; i++) {
    char temp = str[i];
    str[i] = str[len - 1 - i];
    str[len - 1 - i] = temp;
  }

  // Print the reversed string
  printf("The reversed string is: %s\n", str);

  return 0;
}

Output

The following is the output of the above program:

Enter a string to reverse: hello
The reversed string is: olleh

Conclusion

This program demonstrates how to reverse a given string in C. The for loop is a simple and efficient way to reverse a string.

 

Alternative approach

Another way to reverse a string in C is to use the strrev() function from the standard library. This function reverses a string in place, meaning that it does not create a new string.

How to use the strrev() function to reverse a string:

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

int main() {
  char str[100];

  printf("Enter a string to reverse: ");
  scanf("%s", str);

  // Reverse the string using the strrev() function
  strrev(str);

  // Print the reversed string
  printf("The reversed string is: %s\n", str);

  return 0;
}

Output:

 

Enter a string to reverse: hello
The reversed string is: olleh

Reversing a given string in C means writing a C program that takes a string as input and outputs the exact string printing its characters in reverse order.

 

Another Alternative approach 

C Program for reversing the given string

#include<stdio.h>
void main()
{
    char s[10],ch;
    int i,j,ele;
    ele=sizeof(s)/sizeof(s[0]);
    printf("enter the string:");
    scanf("%s",&s);
    printf("before reversing the sting:%s\n",s);
    for(i=0;s[i];i++);//finding the length of string
        for(j=0,i=i-1;j<i;j++,i--)
        {
        ch=s[j];
        s[j]=s[i];
        s[i]=ch;
        }
    printf("After reversing the sting:%s\n",s);
}

output:

enter the string: ELECTRO4U
before reversing the sting: ELECTRO4U
After reversing the sting: U4ORTCELE

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

Design a function to find string length in c

Design a Function to print the string character by character

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.