-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReversearray.c
66 lines (61 loc) · 1.45 KB
/
Reversearray.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 3 operation Xor Swap
void swap ( char* a, char* b){
/*
char c =*a;
*a = *b;
*b = c;
*/
// fails if a and b point to same memory
if(a!=b)
*a ^= *b ^=(*a ^= *b);
}
// Reverse C String with offset in place.
void reverse_chars(char *arr, size_t start, size_t end)
{
size_t n = end- start;
//End contains blankspace, '\0'. Excluded
if(n>0){
swap(&arr[start],&arr[end-1]);
if(arr[start] >='a' && arr[start]<='z')
arr[start] -= 'a' -'A';
}
for (size_t i=1; i < n/2; ++i) {
swap(&arr[start+i],&arr[end-i-1]);
}
}
// Reverse words without trimming inplace
void reverse_words(char * arr) {
size_t length = strlen(arr);
size_t src_start = 0, src_end = 0;
size_t dest_start = 0, dest_end = 0;
for (size_t i = 0; i <length; i++){
if(arr[i] != ' ')
src_end+=1;
else if(arr[i] == ' ')
{
if(src_start!=src_end)
reverse_chars(arr, src_start, src_end);
src_end+=1;
src_start = src_end;
}
}
if(src_start!=src_end)
reverse_chars(arr, src_start, src_end);
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int T;
scanf("%d", &T);
char arr[200];
for(int i = 0; i <T; i++) {
scanf (" %[^\n]%*c", arr);
//scanf (" %[^\n]", arr);
//fgets(arr, 200, stdin);
reverse_words(arr);
printf("%s\n", arr);
}
return 0;
}