12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /* 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 $ */
- #include <unistd.h>
- int main(int argc, char **argv)
- {
- (void) *argv;
- int i;
- int no_ch;
- int j;
-
- no_ch = 0;
- i = 0;
- j = 0;
- if (argc == 4 && argv[2][1] == '\0' && argv[3][1] == '\0')
- {
- while (argv[1][i])
- {
- if (argv[1][i] != argv[2][0])
- {
- i++;
- no_ch++;
- }
- else
- {
- argv[1][i] = argv[3][0];
- i++;
- }
- }
- if (no_ch == i)
- write(1, "\n", 1);
- else
- while(i)
- {
- write(1, &argv[1][j], 1);
- j++;
- i--;
- }
- }
- else
- write(1, "\n", 1);
- return (0);
- }
|