Problem Description:
Write a C program to reverse a string using pointers. The program should take a string input from the user and display both the original and reversed string.
Technical Requirements:
– Programming Language: C
– Language Standard: C99 or later
– Compiler: GCC (GNU Compiler Collection)
– IDE: Code::Blocks / VS Code / Dev-C++ (any)
– Libraries Allowed: stdio.h only
– Do not use built-in string functions like strrev()
– Use pointer-based logic only (no array indexing)
Program Requirements:
– Accept a string (maximum 100 characters)
– Reverse the string using pointers
– Display both original and reversed string
– Code should be simple and well-commented
Sample Input:
hello
Sample Output:
Original string: hello
Reversed string: olleh
My Attempt:
#include <stdio.h>
int main() {
char str[100];
printf(“Enter string: “);
scanf(“%s”, str);
char *start = str;
char *end = str;
char temp;
// Move end pointer to last character
while (*end != ”) {
end++;
}
end–; // move back to last valid character
// Swap characters using pointers
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end–;
}
// Output
printf(“Original string: %sn”, str);
printf(“Reversed string: %sn”, str);
return 0;
}
Leave a Reply
You must be logged in to post a comment.