-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion4.c
47 lines (43 loc) · 1.14 KB
/
question4.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
/*
Reverse the words in a given sentence
METHOD1:
First reverse the given letters in the sentence fully, then in the output received, reverse only the letters
in each word. A single function can be made to reverse the string which takes the starting and the ending
index. A loop can be used to identify the white spaces in the string and start and end index can be passed
to the function each time
Time complexity:O(n) //for full string of n characters it will take O(n) time and since words are part of
the sentence itself, overall time complexity will stay as O(n)
Space complexity:O(1)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverse(char *arr,int start, int end){
int j=end;
int i=start;
while(i <=j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
void reverseWords(char *arr, int size){
reverse(arr,0,size-1);
int start = 0, end;
for(int i=0; i<size; i++){
if(arr[i]== ' '){
end = i-1;
reverse(arr,start,end);
start = i+1;
}
}
}
int main(){
char arr[] = "i am rahul";
int size = strlen(arr);
reverseWords(arr,size);
printf("sentence after reversing words %s\n", arr);
return 0;
}