-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reverse_Order_of_Words.c
49 lines (39 loc) · 936 Bytes
/
Reverse_Order_of_Words.c
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
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size
int main(){
char str[100], reverse[100];
int len, i, index, wordStart, wordEnd;
printf("Enter a String : ");
fgets(str, 100, stdin);
len = strlen(str);
index = 0;
// Start checking of words from the end of string
wordStart = len - 1;
wordEnd = len - 1;
while(wordStart > 0){
// If a word is found
if(str[wordStart] == ' '){
// Add the word to the reverse string
i = wordStart + 1;
while(i <= wordEnd){
reverse[index] = str[i];
i++;
index++;
}
reverse[index++] = ' ';
wordEnd = wordStart - 1;
}
wordStart--;
}
// Finally add the last word
for(i = 0; i <= wordEnd; i++){
reverse[index] = str[i];
index++;
}
// Add NULL character at the end of reverse string
reverse[index] = '\0';
printf("Original string \n%s\n\n", str);
printf("Reversed Order of words \n%s", reverse);
return 0;
}