-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1_search_and_replace.c
More file actions
58 lines (52 loc) · 1.64 KB
/
1_search_and_replace.c
File metadata and controls
58 lines (52 loc) · 1.64 KB
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
/* ***************************************************************************
* Author : Kura Peng (kpeng) <https://github.com/sayakura>
* Created : 2018/10/05
* Updated : 2018/10/05
* ***************************************************************************/
#include <unistd.h>
void pc(char c)
{
write(1, &c, 1);
}
int main(int ac, char **av)
{
if (ac != 4 || av[2][1] || av[3][1])
{
pc('\n');
return (0);
}
int i = 0;
while (av[1][i])
{
if (av[1][i] == av[2][0])
pc(av[3][0]);
else
pc(av[1][i]);
i++;
}
pc('\n');
return (0);
}
/*------------------------------------------------------------------------------
Assignment name : search_and_replace
Expected files : search_and_replace.c
Allowed functions: write, exit
--------------------------------------------------------------------------------
Write a program called search_and_replace that takes 3 arguments, the first
arguments is a string in which to replace a letter (2nd argument) by
another one (3rd argument).
If the number of arguments is not 3, just display a newline.
If the second argument is not contained in the first one (the string)
then the program simply rewrites the string followed by a newline.
Examples:
$>./search_and_replace "My life for Aizr" "z" "u"
My life for Aiur
$>./search_and_replace "gaetan" "art" "zul" | cat -e
$
$>./search_and_replace "zaz" "r" "u" | cat -e
zaz$
$>./search_and_replace "jacob" "a" "b" "c" "e" | cat -e
$
$>./search_and_replace "In foct, stoff crew ore very nice. They like metol." "o" "a" | cat -e
In fact, staff crew are very nice. They like metal.$
------------------------------------------------------------------------------*/