String Manipulation Pointers
C
Easy
2 views
Problem Description
Reverse the string using two pointers (start and end). In-place modification without extra array. Logic of character swapping.
Official Solution
#include <stdio.h>
#include <string.h>
void reverseString(char str[]) {
int start = 0;
int end = strlen(str) - 1;
char temp;
while (start < end) {
/* Swap characters */
temp = str[start];
str[start] = str[end];
str[end] = temp;
/* Move pointers */
start++;
end--;
}
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("Original string: %sn", str);
reverseString(str);
printf("Reversed string: %sn", str);
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!