-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path41.c
More file actions
58 lines (47 loc) · 1.4 KB
/
41.c
File metadata and controls
58 lines (47 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#define MAX_LENGTH 1000 // Maximum length of the input string
// Function to reverse a word in place
void reverseWord(char *word, int start, int end) {
while (start < end) {
// Swap characters
char temp = word[start];
word[start] = word[end];
word[end] = temp;
start++;
end--;
}
}
// Function to reverse the odd-indexed words in a string
void reverseOddWords(char *str) {
int i = 0;
int start = 0;
int wordCount = 0;
while (str[i] != '\0') {
// Check for spaces or end of string to identify words
if (str[i] == ' ' || str[i + 1] == '\0') {
if (str[i + 1] == '\0') {
// If it's the end of the string, include the last character as part of the last word
i++;
}
// Reverse the word if it's an odd-indexed word
if (wordCount % 2 == 1) {
reverseWord(str, start, i - 1);
}
// Update the starting index for the next word
start = i + 1;
wordCount++;
}
i++;
}
}
int main() {
char str[MAX_LENGTH];
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Reverse the odd-indexed words in the string
reverseOddWords(str);
// Output the modified string
printf("Modified string: %s", str);
return 0;
}