#include <stdio.h>
#include <string.h>
// Function to reverse a 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 words in a sentence
void reverseWords(char sentence[]) {
int length = strlen(sentence);
// Reverse the entire sentence
reverseString(sentence, 0, length - 1);
// Initialize variables to track word boundaries
int start = 0;
int end = 0;
// Traverse the reversed sentence
while (end < length) {
// Find the end of the current word
while (end < length && sentence[end] != ' ') {
end++;
}
// Reverse the current word
reverseString(sentence, start, end - 1);
// Move to the next word
start = end + 1;
end++;
}
}
// Function to reverse digits in a number
int reverseNumber(int num) {
int reversed = 0;
// Reverse the digits
while (num > 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return reversed;
}
int main() {
// Example with a sentence
char sentence[] = "Hello, World!";
printf("Original sentence: %s\n", sentence);
// Reverse words in the sentence
reverseWords(sentence);
printf("Flipped sentence: %s\n", sentence);
// Example with a number
int number = 12345;
printf("Original number: %d\n", number);
// Reverse digits in the number
int reversedNumber = reverseNumber(number);
printf("Flipped number: %d\n", reversedNumber);
return 0;
}