Introduction
Reversing a string involves changing the order of characters in the string so that the first character becomes the last, the second becomes the second-to-last, and so on. This guide will show you how to write a C program to reverse a string using pointers.
Example:
- Input:
"Hello" - Output:
"olleH"
Problem Statement
Create a C program that:
- Takes a string as input from the user.
- Uses pointers to reverse the string in place.
- Displays the reversed string.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Declare a String and Pointer Variables: Declare a character array to store the string and pointers to help reverse the string.
- Input the String: Use
getsorfgetsto take input from the user. - Reverse the String Using Pointers: Use pointers to swap characters from the beginning and end of the string, moving toward the center.
- Display the Reversed String: Use
printfto display the reversed string.
C Program to Reverse a String Using Pointers
#include <stdio.h>
#include <string.h>
int main() {
// Step 2: Declare a string and pointer variables
char str[100];
char *start_ptr, *end_ptr, temp;
// Step 3: Prompt the user to enter the string
printf("Enter a string: ");
gets(str); // Use fgets(str, 100, stdin) if gets is deprecated
// Initialize pointers to the start and end of the string
start_ptr = str;
end_ptr = str + strlen(str) - 1;
// Step 4: Reverse the string using pointers
while (start_ptr < end_ptr) {
// Swap the characters at start_ptr and end_ptr
temp = *start_ptr;
*start_ptr = *end_ptr;
*end_ptr = temp;
// Move the pointers toward the center
start_ptr++;
end_ptr--;
}
// Step 5: Display the reversed string
printf("Reversed string: %s\n", str);
return 0; // Return 0 to indicate successful execution
}
Explanation
Step 2: Declare a String and Pointer Variables
- The character array
stris declared to store the string entered by the user. - The pointers
start_ptrandend_ptrare declared to point to the beginning and the end of the string, respectively. - The temporary variable
tempis used to help swap the characters.
Step 3: Input the String
- The program prompts the user to enter a string using
gets. Note thatgetsmay be unsafe; for safer input, you can usefgets(str, 100, stdin).
Step 4: Reverse the String Using Pointers
start_ptris initialized to point to the first character of the string.end_ptris initialized to point to the last character of the string (str + strlen(str) - 1).- A
whileloop is used to reverse the string:- The loop continues until
start_ptris no longer less thanend_ptr. - Inside the loop, the characters pointed to by
start_ptrandend_ptrare swapped using the temporary variabletemp. - The pointers
start_ptrandend_ptrare then moved towards the center by incrementing and decrementing them, respectively.
- The loop continues until
Step 5: Display the Reversed String
- The program uses
printfto display the reversed string.
Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example Output:
Enter a string: Hello
Reversed string: olleH
Conclusion
This C program demonstrates how to reverse a string using pointers. It covers basic concepts such as pointer manipulation, swapping values, and string handling. This example is useful for beginners learning how to work with pointers and strings in C programming.