주의점
av[2]와av[3]는 letter(= 문자여야 함)- 이외에는 찾고 바꾸기만 하면 됨.
코드
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 "Papache est un sabre" "a" "o"
Popoche est un sobre
$>./search_and_replace "zaz" "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 "ZoZ eT Dovid oiME le METol." "o" "a" | cat -e
ZaZ eT David aiME le METal.$
$>./search_and_replace "wNcOre Un ExEmPle Pas Facilw a Ecrirw " "w" "e" | cat -e
eNcOre Un ExEmPle Pas Facile a Ecrire $
해결책
c
#include <unistd.h>
void search_and_replace(char *str, char c1, char c2)
{
int i = 0;
while (str[i])
{
if (str[i] == c1)
write(1, &c2, 1);
else
write(1, &str[i], 1);
i++;
}
}
int main(int ac, char **av)
{
if (ac == 4 && av[2][1] == '\0' && av[3][1] == '\0')
search_and_replace(av[1], av[2][0], av[3][0]);
write(1, "\n", 1);
}