Introduction
Reversing a string involves changing the order of characters in the string so that the last character becomes the first, the second-to-last becomes the second, and so on. This guide will show you how to write a C program to reverse a string provided by the user.
Problem Statement
Create a C program that:
- Takes a string as input from the user.
- Reverses the string.
- Displays the reversed string.
Example:
- Input: String = "Hello"
- Output: Reversed String = "olleH"
Solution Steps
- Include the Standard Input-Output and String Libraries: Use
#include <stdio.h>
for standard input-output functions and#include <string.h>
for string functions. - Write the Main Function: Define the
main
function, which is the entry point of every C program. - Declare Variables: Declare variables to store the string, its length, and a temporary variable for swapping characters.
- Input the String: Use
scanf
orgets
to take input from the user for the string. - Reverse the String: Use a loop to reverse the string by swapping characters from the beginning and the end.
- Display the Reversed String: Use
printf
to display the reversed string.
C Program
#include <stdio.h> #include <string.h> int main() { // Step 1: Declare variables to hold the string, its length, and a temporary variable for swapping char str[100]; int length, i; char temp; // Step 2: Prompt the user to enter a string printf("Enter a string: "); gets(str); // Using gets to read the string including spaces // Step 3: Find the length of the string length = strlen(str); // Step 4: Reverse the string by swapping characters for (i = 0; i < length / 2; i++) { temp = str[i]; str[i] = str[length - 1 - i]; str[length - 1 - i] = temp; } // Step 5: Display the reversed string printf("Reversed string: %s\n", str); return 0; // Step 6: Return 0 to indicate successful execution }
Explanation
Step 1: Declare Variables
- The variable
str
is declared to store the input string. The variablelength
is used to store the length of the string, andi
is used as a loop counter.temp
is used as a temporary variable for swapping characters.
Step 2: Input the String
- The program prompts the user to enter a string using
printf
. Thegets
function is used to read the string, allowing it to include spaces.
Step 3: Find the Length of the String
- The
strlen
function from thestring.h
library is used to calculate the length of the string.
Step 4: Reverse the String
- The program uses a
for
loop to iterate over the string:- Characters from the beginning of the string are swapped with characters from the end using the
temp
variable. - The loop continues until it reaches the middle of the string.
- Characters from the beginning of the string are swapped with characters from the end using the
Step 5: Display the Reversed String
- After the string is reversed, the program displays the reversed string using the
printf
function.
Step 6: Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example 1:
Enter a string: Hello Reversed string: olleH
Example 2:
Enter a string: C Programming Reversed string: gnimmargorP C
Conclusion
This C program demonstrates how to reverse a string by swapping characters from the beginning and end of the string. It covers basic concepts such as string manipulation, loops, and conditional statements, making it a useful example for beginners learning C programming.