-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordsReverser.c
More file actions
50 lines (42 loc) · 994 Bytes
/
WordsReverser.c
File metadata and controls
50 lines (42 loc) · 994 Bytes
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
#include <stdio.h>
#include <string.h>
int main()
{
int s = 1;
char str[255];
printf("Input a sentence: ");
fgets(str, 255, stdin);
//Counting the number of words
for(int j = 0; j < strlen(str); j++){
if(str[j] == ' '){
s++;
}
}
char words[s][255];
int v = 0, z = 0;
//Splitting the sentence into words
for(int k = 0; k < strlen(str); k++){
if(str[k] == ' ' || str[k] == '\n'){
words[v][z] = '\0';
z = 0;
v++;
}
else{
words[v][z] = str[k];
z++;
}
}
//Printing the words reversed
int n = (sizeof(words)/sizeof(words[0]))-1;
char temp[255];
for(int k = 0; k < s/2; k++){
strcpy(temp, words[k]);
strcpy(words[k], words[n]);
strcpy(words[n], temp);
n--;
}
for(int j = 0; j < s; j++){
printf("%s ", words[j]);
}
return 0;
}