#include <stdio.h>
#include <string.h>
// Function to reverse a portion of the string
void reverseString(char str[], int start, int end) {
while (start < end) {
// Swap characters at start and end
char temp = str[start];
str[start] = str[end];
str[end] = temp;
// Move towards the center
start++;
end--;
}
}
// Function to reverse the words in a sentence
void reverseWords(char str[]) {
int length = strlen(str);
// Reverse the entire string
reverseString(str, 0, length - 1);
// Reverse each word individually
int start = 0;
for (int end = 0; end < length; end++) {
// Check for space to identify the end of a word
if (str[end] == ' ') {
reverseString(str, start, end - 1);
start = end + 1; // Move to the next word
}
}
// Reverse the last word (or the only word if there is only one)
reverseString(str, start, length - 1);
}
int main() {
char sentence[100];
// Input the sentence
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
// Remove the newline character from the input
sentence[strcspn(sentence, "\n")] = '\0';
// Reverse the words in the sentence
reverseWords(sentence);
// Display the result
printf("Reversed Sentence: %s\n", sentence);
return 0;
}