Reverse a Given String in C

 

Introduction

Reversing a string is one of the fundamental problems in computer programming. It involves reversing the order of characters in a given string. In C programming, strings are handled as arrays of characters, and reversing them involves manipulating these characters. This problem is a great way to understand basic operations on arrays, loops, and string handling in C.

Objective

The objective of this program is to write a C program that accepts a string from the user and reverses it. The program will make use of basic loops, array manipulation, and string handling techniques to achieve this task.

Code

#include 
#include 

// Function to reverse a string
void reverseString(char str[]) {
    int start = 0;
    int end = strlen(str) - 1;
    char temp;

    // Loop to swap characters from the start and end
    while (start < end) {
        // Swap characters at start and end indices
        temp = str[start];
        str[start] = str[end];
        str[end] = temp;

        // Move towards the middle
        start++;
        end--;
    }
}

int main() {
    char str[100];

    // Ask the user for a string input
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin); // Read the string with spaces
    str[strcspn(str, "\n")] = 0; // Remove the newline character if present

    // Call the function to reverse the string
    reverseString(str);

    // Display the reversed string
    printf("Reversed string: %s\n", str);

    return 0;
}

Explanation of the Program

Let’s break down the structure of the program:

  • Header Files: The program includes two header files:
    • stdio.h: Contains functions for input and output, such as printf() and fgets().
    • string.h: Provides string manipulation functions, like strlen() and strcspn().
  • reverseString Function: This function takes a string as an argument and reverses it in-place using a two-pointer technique.
    • start points to the beginning of the string, and end points to the last character.
    • It then enters a loop where the characters at start and end are swapped. After each swap, start is incremented, and end is decremented until both pointers meet in the middle.
  • Main Function:
    • The main() function first prompts the user to enter a string and reads it using fgets() to handle spaces in the input.
    • The newline character, if present, is removed using strcspn() to clean the input string.
    • Then, the reverseString() function is called to reverse the string, and the reversed string is printed using printf().

How to Run the Program

To run this C program, follow these steps:

  1. Save the Code: Save the C code in a file with a .c extension, such as reverse_string.c.
  2. Compile the Code: Open your terminal or command prompt and use the C compiler (like gcc) to compile the code:
    gcc reverse_string.c -o reverse_string
  3. Run the Program: Once compiled, you can run the program by typing the following command:
    ./reverse_string
  4. Input and Output: The program will prompt you to enter a string. After entering the string, it will print the reversed version of the string.

 

5 Replies to “Reverse a Given String in C”

  1. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is required to get setup?
    I’m assuming having a blog like yours would cost
    a pretty penny? I’m not very internet smart so I’m not 100% certain. Any recommendations or advice would be greatly appreciated.

    Many thanks

  2. This is very fascinating, You’re an overly skilled blogger.
    I’ve joined your rss feed and stay up for in search of extra of your wonhderful post.Also, I’ve shared
    your site in my social networks

  3. Hmm it seems like your website ate my first comment
    (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly
    enjoying your blog. I too am an aspiring blog writer but I’m still new to
    the whole thing. Do you have any tips and hints for novice
    blog writers? I’d genuinely appreciate it.

  4. What you posted made a ton of sense. But, what about this?
    suppose you were to write a awesome title? I ain’t saying your content is not solid,
    but what if you added a headline that makes people desire more?

    I mean Reverse a Given String in C – Learn Programming is kinda boring.
    You might look at Yahoo’s home page and see how
    they create article titles to get viewers to open the links.
    You might add a video or a related picture or two to grab people excited about everything’ve got to say.
    Just my opinion, it might make your posts a little bit more interesting.

Leave a Reply

Your email address will not be published. Required fields are marked *