Simple C program that reverses a string using function:

Reversing a String in C

In this program, we will define a function that takes a string as input and returns the reversed string.

Example Program

Here’s a simple C program to reverse a given string using a function that returns the reversed string:

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

// Function to reverse the string and return it
void reverseString(char str[]) {
    int n = strlen(str); // Get the length of the string
    int start = 0; // Starting index
    int end = n - 1; // Ending index
    char temp;

    // Swap characters from start to end until middle is reached
    while (start < end) {
        temp = str[start]; // Store the current start character in temp
        str[start] = str[end]; // Replace start character with end character
        str[end] = temp; // Replace end character with temp
        start++; // Move start index to the right
        end--; // Move end index to the left
    }
}

int main() {
    char str[100]; // Declare a string of size 100

    // Input the string
    printf("Enter a string: ");
    scanf("%s", str);

    // Print the original string
    printf("Original String: %s\n", str);

    // Reverse the string
    reverseString(str);

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

    return 0;
}

Detailed Explanation

  • Include Headers: We include the necessary header files. stdio.h for standard input/output functions and string.h for string functions.
#include <stdio.h>
#include <string.h>
  • Reverse Function: We define a function reverseString that takes a string as input and reverses it.
void reverseString(char str[]) {
    int n = strlen(str); // Get the length of the string
    int start = 0; // Starting index
    int end = n - 1; // Ending index
    char temp;

    // Swap characters from start to end until middle is reached
    while (start < end) {
        temp = str[start]; // Store the current start character in temp
        str[start] = str[end]; // Replace start character with end character
        str[end] = temp; // Replace end character with temp
        start++; // Move start index to the right
        end--; // Move end index to the left
    }
}
  • We get the length of the string using strlen.
  • We initialize two indices, start and end, pointing to the beginning and end of the string.
  • We use a loop to swap the characters from the start and end until the middle of the string is reached.

  • Main Function: In the main function, we handle user input and call the reverseString function.
int main() {
    char str[100]; // Declare a string of size 100

    // Input the string
    printf("Enter a string: ");
    scanf("%s", str);

    // Print the original string
    printf("Original String: %s\n", str);

    // Reverse the string
    reverseString(str);

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

    return 0;
}
  • We declare a character array str to store the input string.
  • We use scanf to read the input string. Note that scanf will read a single word (i.e., it stops reading at a space).
  • We print the original string.
  • We call the reverseString function to reverse the string.
  • We print the reversed string.
  • We initialize two indices, start and end, pointing to the beginning and end of the string.
  • We use a loop to swap the characters from the start and end until the middle of the string is reached.

Testing the Program

When you run the program, it will prompt you to enter a string. For example:

Enter a string: Hello
Original String: Hello
Reversed String: olleH

Conclusion

Reversing a string is a fundamental exercise for beginners to understand string manipulation and loops in C programming. This program provides a simple and clear way to reverse a string by swapping characters from the start and end. Using scanf ensures straightforward input handling for single words. Feel free to leave your questions or comments below if you need further assistance or clarification. Happy coding!

Leave a Comment

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

Scroll to Top