Let's begin by creating a function that takes a string as input and reverses its characters. We'll do this by swapping characters from the beginning and end of the string, moving towards the center.
#include <stdio.h>
#include <string.h>
void reverseString(char *str) {
int len = strlen(str);
int i;
char temp;
for (i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline character if present
reverseString(str);
printf("Reversed string: %s", str);
return 0;
}
This function utilizes a for loop to swap characters from opposite ends of the string, efficiently reversing the string in-place.