-
Notifications
You must be signed in to change notification settings - Fork 3
/
4_rev_wstr.c
108 lines (96 loc) · 2.24 KB
/
4_rev_wstr.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* ***************************************************************************
* Author : Kura Peng (kpeng) <https://github.com/sayakura>
* Created : 2018/10/05
* Updated : 2018/10/05
* ***************************************************************************/
#include <unistd.h>
#include <stdlib.h>
void pc(char c)
{
write(1, &c, 1);
}
void ps(char *s)
{
while (*s)
pc(*s++);
}
void reverse(char *s, int start, int end)
{
char temp;
while (start < end)
{
temp = s[start];
s[start++] = s[end];
s[end--] = temp;
}
}
int sl(char *str)
{
int i = 0;
while (str[i])
i++;
return (i);
}
void rev_wstr(char *str)
{
int len = sl(str);
int i = 0;
char *arr = (char *)malloc(sizeof(char) * len + 1);
arr[len] = '\0';
while ((arr[i] = str[i]) != '\0')
i++;
/////////////
int start = 0;
int temp = 0;
while (arr[temp])
{
temp++;
if (arr[temp] == ' ' || arr[temp] == '\0')
{
reverse(arr, start, temp - 1);
start = temp + 1;
}
}
reverse(arr, 0, i - 1);
ps(arr);
}
int main(int ac, char **av)
{
if (ac != 2)
{
pc('\n');
return (0);
}
else
{
if (av[1][0])
rev_wstr(av[1]);
pc('\n');
}
return (0);
}
/* ***************************************************************************
Assignment name : rev_wstr
Expected files : rev_wstr.c
Allowed functions: write, malloc, free
--------------------------------------------------------------------------------
Write a program that takes a string as a parameter, and prints its words in
reverse order.
A "word" is a part of the string bounded by spaces and/or tabs, or the
begin/end of the string.
If the number of parameters is different from 1, the program will display
'\n'.
In the parameters that are going to be tested, there won't be any "additional"
spaces (meaning that there won't be additionnal spaces at the beginning or at
the end of the string, and words will always be separated by exactly one space).
Examples:
$> ./rev_wstr "le temps du mepris precede celui de l'indifference" | cat -e
l'indifference de celui precede mepris du temps le$
$> ./rev_wstr "abcdefghijklm"
abcdefghijklm
$> ./rev_wstr "il contempla le mont" | cat -e
mont le contempla il$
$> ./rev_wstr | cat -e
$
$>
* ***************************************************************************/