Skip to content

Instantly share code, notes, and snippets.

@dlupaescu
Last active August 29, 2015 14:28
Show Gist options
  • Save dlupaescu/35f13c51681087d50257 to your computer and use it in GitHub Desktop.
Save dlupaescu/35f13c51681087d50257 to your computer and use it in GitHub Desktop.
Level: 1 | first_word.c | repeat_alpha.c
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* first_word.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: exam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/08/21 17:07:55 by exam #+# #+# */
/* Updated: 2015/08/21 17:29:38 by exam ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
int ft_check_word(char **argv, int i)
{
while (argv[1][i] != ' ' && argv[1][i])
{
ft_putchar(argv[1][i]);
i++;
}
return (1);
}
int main(int argc, char **argv)
{
char c;
int ok;
int i;
c = '\n';
if (argc != 2)
{
ft_putchar(c);
return (0);
}
else if (argc == 2)
{
i = 0;
ok = 0;
while (argv[1][i] && ok == 0)
{
if (argv[1][i] != ' ')
ok = ft_check_word(argv, i);
i++;
}
}
ft_putchar(c);
return (0);
}
/*
Exercitiu : first_word
Fisiere de iesire : first_word.c
Functii autorizate : write
--------------------------------------------------------------------------------
Scrieti un program care ia ca parametru un sir de caractere si afiseaza primul
cuvant din acest sir, urmat de un '\n'.
Numim "cuvant" o portiune a sirului de caractere delimitat de spatii sau/si
tabulatori, atat la inceputul cat si la sfarsitul sirului.
Daca numarul de parametri transmis este diferit de 1, sau daca nu este niciun
cuvant de afisat, programul va afisa \texttt'\n'.
Exemplu:
$> ./first_word "FOR PONY" | cat -e
FOR$
$> ./first_word "this ... is sparta, then again, maybe not" | cat -e
this$
$> ./first_word " " | cat -e
$
$> ./first_word "a" "b" | cat -e
$
$> ./first_word " lorem,ipsum " | cat -e
lorem,ipsum$
$>
*/
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* repeat_alpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: exam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/08/21 17:23:13 by exam #+# #+# */
/* Updated: 2015/08/21 18:01:35 by exam ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int ft_is_alpha(char c)
{
if ((c >= 'a') && (c <= 'z'))
return (0);
else
return (1);
}
int ft_count(char c)
{
int k;
k = (c - 'a');
return (k);
}
void ft_print(char **argv)
{
int i;
int j;
i = 0;
j = 0;
while (argv[1][i])
{
if (ft_is_alpha(argv[1][i]))
{
write(1, &argv[1][i], 1);
}
else
{
while (j <= ft_count(argv[1][i]))
{
write(1, &argv[1][i], 1);
j++;
}
j = 0;
}
i++;
}
}
int main(int argc, char **argv)
{
if (argc != 2)
{
write(1, "\n", 1);
return (0);
}
ft_print(argv);
write(1, "\n", 1);
return (0);
}
/*
Exercitiu : repeat_alpha
Fisiere de iesire : repeat_alpha.c
Functii autorizate : write
--------------------------------------------------------------------------------
Scrieti un program numit repeat_alpha care primeste un sir de caractere si il
afiseaza repetand fiecare caracter alfabetic de atatea ori cat indexul sau
alfabetic, urmat de un '\n'.
'a' becomes 'a', 'b' becomes 'bb', 'e' becomes 'eeeee', etc...
Majusculele raman neschimbate.
Daca numarul de argumente nu e 1, programul afiseaza '\n'.
Exemple:
$>./repeat_alpha "abc"
abbccc
$>./repeat_alpha "Alex." | cat -e
Alllllllllllleeeeexxxxxxxxxxxxxxxxxxxxxxxx.$
$>./repeat_alpha 'abacadaba 42!' | cat -e
abbacccaddddabba 42!$
$>./repeat_alpha | cat -e
$
$>
$>./repeat_alpha "" | cat -e
$
$>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment