Skip to content

Instantly share code, notes, and snippets.

@sametaybaz
Last active June 25, 2023 17:38
Show Gist options
  • Save sametaybaz/301f130d9363b66c2685d65218649054 to your computer and use it in GitHub Desktop.
Save sametaybaz/301f130d9363b66c2685d65218649054 to your computer and use it in GitHub Desktop.
42_Exam_02_prep
42_Exam_02 questions and answers
***********************************************
Level_1
***********************************************
// ******************** first_word ********************
/* QUESTION
Assignment name : first_word
Expected files : first_word.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string and displays its first word, followed by a
newline.
A word is a section of string delimited by spaces/tabs or by the start/end of
the string.
If the number of parameters is not 1, or if there are no words, simply display
a newline.
Examples:
$> ./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$
$>
*/
/* ANSWER
#include <unistd.h>
int main(int ac,char **av)
{
int i = 0;
if (ac == 2)
{
while (av[1][i] == ' ' || av[1][i] == '\t') // ' ' = 32 ; '\t' == 9
i++;
while ( av[1][i] != 32 && av[1][i] != '\t' && av[1][i] != '\0')
{
write(1,&av[1][i],1);
i++;
}
}
write (1,"\n",1);
}
*/
// ********************************************************************
// ******************** fizzbuzz **********************
/* QUESTION
Assignment name : fizzbuzz
Expected files : fizzbuzz.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that prints the numbers from 1 to 100, each separated by a
newline.
If the number is a multiple of 3, it prints 'fizz' instead.
If the number is a multiple of 5, it prints 'buzz' instead.
If the number is both a multiple of 3 and a multiple of 5, it prints 'fizzbuzz' instead.
Example:
$>./fizzbuzz
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
[...]
97
98
fizz
buzz
$>
*/
/* ANSWER
#include <unistd.h>
void ft_write_number(int number)
{
char decimal[10] = "0123456789";
if (number > 9)
ft_write_number(number / 10);
write(1,&decimal[number % 10],1);
}
int main()
{
int i;
while (i <= 100)
{
if (i % 15 == 0)
write(1,"fizzbuzz",8);
else if (i % 3 == 0)
write(1,"fizz",4);
else if (i % 5 == 0)
write(1,"buzz",4);
else
ft_write_number(i);
i++;
write(1,"\n",1);
}
}
*/
// ********************************************************************
// ******************** ft_putstr **********************
/* QUESTION
Assignment name : ft_putstr
Expected files : ft_putstr.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a function that displays a string on the standard output.
The pointer passed to the function contains the address of the string's first
character.
Your function must be declared as follows:
void ft_putstr(char *str);
*/
/* ANSWER
#include <unistd.h>
void ft_putstr(char *str)
{
int i = 0;
while (str[i] != '\0')
{
write(1,&str[i],1);
i++;
}
}
*/
// ********************************************************************
// ******************** ft_strcpy **********************
/* QUESTION
Assignment name : ft_strcpy
Expected files : ft_strcpy.c
Allowed functions:
--------------------------------------------------------------------------------
Reproduce the behavior of the function strcpy (man strcpy).
Your function must be declared as follows:
char *ft_strcpy(char *s1, char *s2);
*/
// !!! s2 yi s1 e kopyalıyoruz !!!
/* ANSWER
char *ft_strcpy(char *s1, char *s2)
{
int i = 0;
while (s2[i] != '\0')
{
s1[i] = s2[i]
i++;
}
s1[i] = '\0';
return (s1);
}
*/
// *********************************************************************
// ******************** ft_strlen **********************
/* QUESTION
Assignment name : ft_strlen
Expected files : ft_strlen.c
Allowed functions:
--------------------------------------------------------------------------------
Write a function that returns the length of a string.
Your function must be declared as follows:
int ft_strlen(char *str);
*/
/* ANSWER
int ft_strlen(char *str) {
int i = 0;
while (str[i] != '\0')
i++;
return (i);
}
*/
// *********************************************************************
// ******************** ft_swap **********************
/* QUESTION
Assignment name : ft_swap
Expected files : ft_swap.c
Allowed functions:
--------------------------------------------------------------------------------
Write a function that swaps the contents of two integers the adresses of which
are passed as parameters.
Your function must be declared as follows:
void ft_swap(int *a, int *b);
*/
/* ANSWER
void ft_swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
*/
// **********************************************************************
// ******************** repeat_alpha **********************
/* QUESTION
Assignment name : repeat_alpha
Expected files : repeat_alpha.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program called repeat_alpha that takes a string and display it
repeating each alphabetical character as many times as its alphabetical index,
followed by a newline.
'a' becomes 'a', 'b' becomes 'bb', 'e' becomes 'eeeee', etc...
Case remains unchanged.
If the number of arguments is not 1, just display a newline.
Examples:
$>./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
$
$>
*/
/* ANSWER
#include <unistd.h>
int main(int ac,char **av)
{
int i = 0;
int repeater;
if (ac == 2)
{
while (av[1][i] != '\0')
{
if ( av[1][i] >= 'a' && av[1][i] <= 'z') {
repeater = av[1][i] - ('a' - 1); // ('a'- 1 == 96)
}
else if (av[1][i] >= 'A' && av[1][i] <= 'Z') {
repeater = av[1][i] - ('A' - 1); // ('A' - 1 == 64)
}
while (repeater > 0)
{
write(1,&av[1][i],1);
repeater--;
}
write(1,&av[1][i],1); // !!! for non-alpha numeric chars
i++;
}
}
write (1,"\n",1);
}
*/
// ***********************************************************************
// ******************** rev_print **********************
/* QUESTION
Assignment name : rev_print
Expected files : rev_print.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string, and displays the string in reverse
followed by a newline.
If the number of parameters is not 1, the program displays a newline.
Examples:
$> ./rev_print "zaz" | cat -e
zaz$
$> ./rev_print "dub0 a POIL" | cat -e
LIOP a 0bud$
$> ./rev_print | cat -e
$
*/
/* ANSWER
#include <unistd.h>
int main(int ac, char **av)
{
int i = 0;
if (ac == 2)
{
while (av[1][i] != '\0')
i++;
i--; // for pass null terminate
while (i >= 0)
{
write(1, &av[1][i], 1);
i--;
}
}
write (1,"\n",1);
}
*/
// ************************************************************************
// ******************** rot_13 **********************
/* QUESTION
Assignment name : rot_13
Expected files : rot_13.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string and displays it, replacing each of its
letters by the letter 13 spaces ahead in alphabetical order.
'z' becomes 'm' and 'Z' becomes 'M'. Case remains unaffected.
The output will be followed by a newline.
If the number of arguments is not 1, the program displays a newline.
Example:
$>./rot_13 "abc"
nop
$>./rot_13 "My horse is Amazing." | cat -e
Zl ubefr vf Nznmvat.$
$>./rot_13 "AkjhZ zLKIJz , 23y " | cat -e
NxwuM mYXVWm , 23l $
$>./rot_13 | cat -e
$
$>
$>./rot_13 "" | cat -e
$
$>
*/
/* ANSWER
#include <unistd.h>
int main(int ac,char **av)
{
int i = 0;
if (ac == 2)
{
while (av[1][i])
{
if ((av[i][i] >= 'a' && av[1][i] <= 'm') || (av[i][i] >= 'A' && av[1][i] <= 'm'))
av[1][i] += 13;
else if((av[i][i] >= 'n' && av[1][i] <= 'z') || (av[i][i] >= 'N' && av[1][i] <= 'Z'))
av[1][i] -= 13;
write (1, &av[1][i] ,1);
i++;
}
}
write (1,"\n",1);
return (0);
}
*/
// *************************************************************************
// ******************** rot_one **********************
/* QUESTION
Assignment name : rotone
Expected files : rotone.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string and displays it, replacing each of its
letters by the next one in alphabetical order.
'z' becomes 'a' and 'Z' becomes 'A'. Case remains unaffected.
The output will be followed by a \n.
If the number of arguments is not 1, the program displays \n.
Example:
$>./rotone "abc"
bcd
$>./rotone "Les stagiaires du staff ne sentent pas toujours tres bon." | cat -e
Mft tubhjbjsft ev tubgg of tfoufou qbt upvkpvst usft cpo.$
$>./rotone "AkjhZ zLKIJz , 23y " | cat -e
BlkiA aMLJKa , 23z $
$>./rotone | cat -e
$
$>
$>./rotone "" | cat -e
$
$>
*/
/* ANSWER
int main(int ac, char **av)
{
int i = 0;
if (ac == 2)
{
while (av[1][i] != '\0')
if ((av[1][i] >= 'a' && av[1][i] <= 'y') || (av[1][i] >= 'A' && av[1][i] <= 'Y'))
av[1][i] += 1;
else if (av[1][i] == 'z' || av[1][i] == 'Z')
av[1][i] -= 25;
write(1, &av[1][i], 1);
i++;
}
write(1, "\n", 1);
return (0);
}
*/
// ************************************************************************
// ******************** search_and_replace **********************
/* QUESTION
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 $
*/
/* ANSWER
#include <unistd.h>
int main(int ac, char **av)
{
int i = 0;
if (ac == 4 && !av[2][1] && !av[3][1]) // !!! 2. VE 3. argumanlar tek char olmalı
{
while (av[1][i] != '\0')
{
if (av[1][i] == av[2][0])
av[1][i] = av[3][0];
write(1, &av[1][i], 1);
i++;
}
}
write(1, "\n", 1);
}
*/
// ************************************************************************
// ******************** ulstr **********************
/* QUESTION
Assignment name : ulstr
Expected files : ulstr.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string and reverses the case of all its letters.
Other characters remain unchanged.
You must display the result followed by a '\n'.
If the number of arguments is not 1, the program displays '\n'.
Examples :
$>./ulstr "L'eSPrit nE peUt plUs pRogResSer s'Il staGne et sI peRsIsTent VAnIte et auto-justification." | cat -e
l'EspRIT Ne PEuT PLuS PrOGrESsER S'iL STAgNE ET Si PErSiStENT vaNiTE ET AUTO-JUSTIFICATION.$
$>./ulstr "S'enTOuRer dE sECreT eSt uN sIGnE De mAnQuE De coNNaiSSanCe. " | cat -e
s'ENtoUrER De SecREt EsT Un SigNe dE MaNqUe dE COnnAIssANcE. $
$>./ulstr "3:21 Ba tOut moUn ki Ka di KE m'en Ka fe fot" | cat -e
3:21 bA ToUT MOuN KI kA DI ke M'EN kA FE FOT$
$>./ulstr | cat -e
$
*/
/* ANSWER
#include <unistd.h>
int main(int ac,char **av)
{
int i = 0;
if (ac == 2)
{
while (av[1][i] != '\0')
{
if (av[1][i] <= 'z' && av[1][i] >= 'a')
av[1][i] -= 32;
else if (av[1][i] <= 'Z' && av[1][i] >= 'A')
av[1][i] += 32;
write (1,&av[1][i],1);
i++;
}
}
write(1,"\n",1);
}
*/
// ************************************************************************
// ------------------------ BİTTİ ------------------------ //
******************************************************************************************************************************
***********************************************
Level_2
***********************************************
// ******************** alpha_mirror ********************
/* QUESTION
Assignment name : alpha_mirror
Expected files : alpha_mirror.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program called alpha_mirror that takes a string and displays this string
after replacing each alphabetical character by the opposite alphabetical
character, followed by a newline.
'a' becomes 'z', 'Z' becomes 'A'
'd' becomes 'w', 'M' becomes 'N'
and so on.
Case is not changed.
If the number of arguments is not 1, display only a newline.
Examples:
$>./alpha_mirror "abc"
zyx
$>./alpha_mirror "My horse is Amazing." | cat -e
Nb slihv rh Znzarmt.$
$>./alpha_mirror | cat -e
$
$>
*/
/* ANSWER
#include <unistd.h>
int main(int ac,char **av)
{
int i = 0;
if (ac == 2)
{
while (av[1][i] != '\0')
{
if (av[1][i] >= 'A' && av[1][i] <= 'Z')
av[1][i] = 'A' - av[1][i] + 'Z';
else if(av[1][i] >= 'a' && av[1][i] <= 'z')
av[1][i] = 'a' - av[1][i] + 'z';
write(1, &av[1][i],1);
i++;
}
}
write (1,"\n",1);
}
*/
// ********************************************************************
// ******************** camel_to_snake ********************
/* QUESTION
Assignment name : camel_to_snake
Expected files : camel_to_snake.c
Allowed functions: malloc, realloc, write
--------------------------------------------------------------------------------
Write a program that takes a single string in lowerCamelCase format
and converts it into a string in snake_case format.
A lowerCamelCase string is a string where each word begins with a capital letter
except for the first one.
A snake_case string is a string where each word is in lower case, separated by
an underscore "_".
Examples:
$>./camel_to_snake "hereIsACamelCaseWord"
here_is_a_camel_case_word
$>./camel_to_snake "helloWorld" | cat -e
hello_world$
$>./camel_to_snake | cat -e
$
*/
/* ANSWER
#include <unistd.h>
int main(int ac, char **av)
{
int i = 0;
if (ac = 2)
{
while (av[1][i] != '\0')
{
if (av[1][i] <= 'Z' && av[1][i] >= 'A')
{
av[1][i] += 32;
write(1, "_", 1);
}
write(1, &av[1][i], 1);
i++;
}
}
write(1,"\n",1);
}
*/
// ********************************************************************
// ******************** do_op ********************
/* QUESTION
Assignment name : do_op
Expected files : *.c, *.h
Allowed functions: atoi, printf, write
--------------------------------------------------------------------------------
Write a program that takes three strings:
- The first and the third one are representations of base-10 signed integers
that fit in an int.
- The second one is an arithmetic operator chosen from: + - * / %
The program must display the result of the requested arithmetic operation,
followed by a newline. If the number of parameters is not 3, the program
just displays a newline.
You can assume the string have no mistakes or extraneous characters. Negative
numbers, in input or output, will have one and only one leading '-'. The
result of the operation fits in an int.
Examples:
$> ./do_op "123" "*" 456 | cat -e
56088$
$> ./do_op "9828" "/" 234 | cat -e
42$
$> ./do_op "1" "+" "-43" | cat -e
-42$
$> ./do_op | cat -e
$
*/
/* ANSWER
#include <stdlib.h>
#include <stdio.h>
int main(int ac,char **av)
{
if (ac == 4 && !av[2][1]) // !av[2][1] ekstra önlem için yazdım.
{
int av_1 = atoi(av[1]);
int av_3 = atoi(av[3]);
if (av[2][0] == '+')
printf("%d",av_1+av_3);
else if (av[2][0] == '-')
printf("%d",av_1-av_3);
else if (av[2][0] == '*')
printf("%d",av_1*av_3);
else if (av[2][0] == '/')
printf("%d",av_1/av_3);
else if(av[2][0] == '%')
printf("%d",av_1%av_3);
}
printf("\n");
}
*/
// ********************************************************************
// ******************** atoi ********************
/* QUESTION
Assignment name : ft_atoi
Expected files : ft_atoi.c
Allowed functions: None
--------------------------------------------------------------------------------
Write a function that converts the string argument str to an integer (type int)
and returns it.
It works much like the standard atoi(const char *str) function, see the man.
Your function must be declared as follows:
int ft_atoi(const char *str);
*/
/* ANSWER
int ft_atoi(const char *str)
{
int i = 0;
int result = 0;
int sign = 1;
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '-')
sign = -1;
if (str[i] == '-' || str[i] == '+')
i++;
while ((str[i] >= '0' && str[i] <= '9') && str[i] != '\0')
{
result = (result * 10) + (str[i] - '0');
i++;
}
return (result * sign);
}
*/
// ********************************************************************
// ******************** strcmp ********************
/* QUESTION
Assignment name : ft_strcmp
Expected files : ft_strcmp.c
Allowed functions:
--------------------------------------------------------------------------------
Reproduce the behavior of the function strcmp (man strcmp).
Your function must be declared as follows:
int ft_strcmp(char *s1, char *s2);
*/
// !!! UNUTMA S1 - S2
/* ANSWER
int ft_strcmp(char *s1, char *s2) {
int i;
i = 0;
while ( s1[i] == s2[i] && s1[i] == '\0' && s2[i] == '\0')
i++;
return (s1[i] - s2[i]);
}
*/
// ********************************************************************
// ******************** strcspn ********************
/* QUESTION
Assignment name : ft_strcspn
Expected files : ft_strcspn.c
Allowed functions: None
---------------------------------------------------------------
Reproduce exactly the behavior of the function strcspn
(man strcspn).
The function should be prototyped as follows:
size_t ft_strcspn(const char *s, const char *reject);
strcspn nedir= Program strcspn() fonksiyonu ile bir karakter dizisi
içindeki karakterlerin herhangi birinin diğer karakter dizisi içinde
bulunduğu ilk yerin indeksini hesaplayarak elde ettiği sonucu ekrana
yazar.
*/
/* ANSWER
size_t ft_strcspn(const char *s, const char *reject)
{
int i = 0;
int j = 0;
while (s[i] != '\0')
{
j = 0;
while (reject[j] != '\0')
{
if (s[i] == reject[j])
return (i); // !!! j yi değil i yi return etcez.
j++;
}
i++;
}
return (i);
}
*/
// ********************************************************************
// ******************** strdup ********************
/* QUESTION
Assignment name : ft_strdup
Expected files : ft_strdup.c
Allowed functions: malloc
--------------------------------------------------------------------------------
Reproduce the behavior of the function strdup (man strdup).
Your function must be declared as follows:
char *ft_strdup(char *src);
*/
/* ANSWER
#include <stdlib.h>
int ft_strlen(char *str) {
int i = 0;
while (str[i] != '\0')
i++;
return i;
}
char *ft_strdup(char *src) {
int len = ft_strlen(src);
char *src_copy = malloc(sizeof(char)*len + 1);
int i = 0;
if (src_copy == NULL)
return (NULL);
while (src[i] != '\0')
{
src_copy[i] = src[i];
i++;
}
src_copy[i] = '\0';
return (src_copy);
}
*/
// ********************************************************************
// ******************** strpbrk ********************
/* QUESTION
Assignment name : ft_strpbrk
Expected files : ft_strpbrk.c
Allowed functions: None
---------------------------------------------------------------
Reproduce exactly the behavior of the function strpbrk
(man strpbrk).
The function should be prototyped as follows:
char *ft_strpbrk(const char *s1, const char *s2);
strpbrk() fonksiyonu
Str2 parametresi ile gösterilen karakter dizisinde yer alan karakterlerden
herhangi birinin str1 parametresi ile gösterilen karakter dizisi içinde
bulunduğu yerin bellek adresini geri döndürür. Eğer aranan karakterlerden
hiç biri bulunamazsa NULL bir işaretçi geri döndürür.
*/
/* ANSWER
char *ft_strpbrk(const char *s1, const char *s2)
{
int i;
int j;
if (!s1 || !s2)
return (0);
i = 0;
while (s1[i] != '\0')
{
j = 0;
while (s2[j] != '\0')
{
if (s1[i] == s2[j])
return ((char *)&s1[j]); // tek fark bu strcspn den
j++;
}
i++;
}
return (0);
}
*/
// ********************************************************************
// ******************** strrev ********************
/* QUESTION
Assignment name : ft_strrev
Expected files : ft_strrev.c
Allowed functions:
--------------------------------------------------------------------------------
Write a function that reverses (in-place) a string.
It must return its parameter.
Your function must be declared as follows:
char *ft_strrev(char *str);
*/
/* ANSWER
char *ft_strrev(char *str)
{
int i = 0;
int length = 0;
char temporary;
while (str[length] != '\0')
length++;
while (i < length / 2) // 0 , 1 , 2(not)
{
temporary = str[i];
str[i] = str[length - 1 - i]; // 0 = 3 // 1 = 2
str[length - 1 - i] = temporary; // 3 = 0 // 2 = 1
i++;
}
return (str);
}
// samet // t...s // te.as // m olduğu yerde duruyor (5 elemanlı)
// abla // a..a // al.a// alba
*/
// ********************************************************************
// ******************** strspn ********************
/* QUESTION
strspn nedir;
size_t strspn(const char *str1, const char *str2);
Str1 parametresi ile gösterilen karakter dizisinde sadece str2 parametresi
ile gösterilen karakter dizisinde yer alan karakterlerin bulunduğu ilk
kısmın uzunluğunu geri döndürür. Arama işlemine sonlandırıcı boş
karakterler ('\0') dahil edilmez.
Str1 veya str2 parametresi boş bir karakterle sonlandırılmış bir karakter
dizisini gösteren bir işaretçi değilse, sonuç belirsiz olabilir.
---------------------------------------------------------------
Assignment name : ft_strspn
Expected files : ft_strspn.c
Allowed functions: None
---------------------------------------------------------------
Reproduce exactly the behavior of the strspn function
(man strspn).
The function should be prototyped as follows:
size_t ft_strspn(const char *s, const char *accept);
*/
/* ANSWER
int ft_strchr(const char *str, char c)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (1);
i++;
}
return (0);
}
size_t ft_strspn(const char *str1, const char *str2)
{
size_t i;
if (!str1 || !str2)
return (0);
i = 0;
while (str1[i])
{
if (ft_strchr(str2, str1[i]))
i++;
else
return (i);
}
return (i);
}
*/
// ********************************************************************
// ******************** inter ********************
/* QUESTION
Assignment name : inter
Expected files : inter.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes two strings and displays, without doubles, the
characters that appear in both strings, in the order they appear in the first
one.
The display will be followed by a \n.
If the number of arguments is not 2, the program displays \n.
Examples:
$>./inter "padinton" "paqefwtdjetyiytjneytjoeyjnejeyj" | cat -e
padinto$
$>./inter ddf6vewg64f gtwthgdwthdwfteewhrtag6h4ffdhsd | cat -e
df6ewg4$
$>./inter "rien" "cette phrase ne cache rien" | cat -e
rien$
$>./inter | cat -e
$
*/
/* ANSWER
// ÖZET: av_1 de öncesinde matchi olmayan bir karakter varsa ve bu
// karekter in av_2 de matchi varsa yazılır .
#include <unistd.h>
// tekrar eden karekter varmı av[1] de
int check_one(char* str, char c, int current_index)
{
int i;
i = 0;
while (str[i] && (i < current_index))
{
if (str[i] == c)
return (0);
i++;
}
return (1);
}
// av[2] de av[1] deki char var mı
int check_two(char* str, char c)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (1);
i++;
}
return (0);
}
int main(int argc, char* argv[])
{
int i;
if (argc == 3)
{
i = 0;
while (argv[1][i])
{
if (check_one(argv[1], argv[1][i], i))
{
if(check_two(argv[2], argv[1][i]))
write(1, &argv[1][i], 1);
}
i += 1;
}
}
write(1, "\n", 1);
return (0);
}
*/
// ********************************************************************
// ******************** is_power_of_2 ********************
/* QUESTION
Assignment name : is_power_of_2
Expected files : is_power_of_2.c
Allowed functions: None
--------------------------------------------------------------------------------
Write a function that determines if a given number is a power of 2.
This function returns 1 if the given number is a power of 2, otherwise it returns 0.
Your function must be declared as follows:
int is_power_of_2(unsigned int n);
*/
/* ANSWER
int is_power_of_2(unsigned int n)
{
if (n == 0)
return 0;
while (n % 2 == 0)
{
n /= 2;
}
if (n == 1)
return 1;
else
return 0;
}
//**************************
// açıklama;
int main()
{
printf("%d\n", is_power_of_2(20));
// 20 = 2 * 2 * 5
// ( 5 bozuyor bunada /2 ile 20 / 2 = 10 , 10/2 = 5 diyerek ulaşır .)
printf("%d", is_power_of_2(2));
// 2 ye böle böle n = 1 çarpanına ulaşırız 1%2 0 olmaz
// ve çıkarız return 1 yazdırırız.
}
// alternatife;
int is_power_of_2(unsigned int n) {
int number = 1;
while (number <= n)
{
if (number == n)
return 1;
number = number * 2 ;
}
return 0;
}
*/
// ********************************************************************
// ******************** last_word ******************** ! ALTERNATİF ÇÖZÜMÜ TEK MAIN DE YAZ
/* QUESTION
Assignment name : last_word
Expected files : last_word.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string and displays its last word followed by a \n.
A word is a section of string delimited by spaces/tabs or by the start/end of
the string.
If the number of parameters is not 1, or there are no words, display a newline.
Example:
$> ./last_word "FOR PONY" | cat -e
PONY$
$> ./last_word "this ... is sparta, then again, maybe not" | cat -e
not$
$> ./last_word " " | cat -e
$
$> ./last_word "a" "b" | cat -e
$
$> ./last_word " lorem,ipsum " | cat -e
lorem,ipsum$
$>
*/
/* ANSWER
// ********************************************
// yazdırılabilir arağı baz alarak algoritma;
#include <unistd.h>
int main(int argc, char *argv[])
{
int i;
if (argc == 2)
{
i = 0;
while (argv[1][i])
i++;
i--;
while (argv[1][i] <= 32)
i--;
while (argv[1][i] > 32)
i--;
i++;
while (argv[1][i] > 32)
write(1, &argv[1][i++], 1);
}
write(1, "\n", 1);
return (0);
}
// ********************************************
// tab ve space i baz alan çözüm ;
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_last_word(char *s)
{
int i;
i = 0;
while (s[i])
i++;
i -= 1;
while (s[i] == ' ' || s[i] == '\t')
i--;
while ((s[i] != ' ' && s[i] != '\t') && i >= 0)
i--;
i += 1;
while (s[i] && s[i] != ' ' && s[i] != '\t')
{
ft_putchar(s[i]);
i++;
}
}
int main(int ac, char **av)
{
if (ac == 2)
ft_last_word(av[1]);
ft_putchar('\n');
return (0);
}
*/
// ********************************************************************
// ******************** max ********************
/* QUESTION
Assignment name : max
Expected files : max.c
Allowed functions:
--------------------------------------------------------------------------------
Write the following function:
int max(int* tab, unsigned int len);
The first parameter is an array of int, the second is the number of elements in
the array.
The function returns the largest number found in the array.
If the array is empty, the function returns 0.
*/
/* ANSWER
int max(int* tab, unsigned int len)
{
int i = 0;
int result = tab[i];
if (len == 0)
return 0;
while (i < len)
{
if (result < tab[i])
{
result = tab[i];
}
i++;
}
}
*/
// ********************************************************************
// ******************** print_bits ********************
/* QUESTION
Assignment name : print_bits
Expected files : print_bits.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a function that takes a byte, and prints it in binary WITHOUT A NEWLINE
AT THE END.
Your function must be declared as follows:
void print_bits(unsigned char octet);
Example, if you pass 2 to print_bits, it will print "00000010"
*/
/* ANSWER
#include <unistd.h>
void print_bits(unsigned char octet)
{
int i = 7;
unsigned char bit;
while (i >= 0)
{
bit = (octet >> i & 1) + '0';
write(1, &bit, 1);
i--;
}
}
int main() {
print_bits('A');
}
*/
// ********************************************************************
// ******************** reverse_bits ********************
/* QUESTION
Assignment name : reverse_bits
Expected files : reverse_bits.c
Allowed functions:
--------------------------------------------------------------------------------
Write a function that takes a byte, reverses it, bit by bit (like the
example) and returns the result.
Your function must be declared as follows:
unsigned char reverse_bits(unsigned char octet);
Example:
1 byte
_____________
0010 0110
||
\/
0110 0100
*/
/* ANSWER
unsigned char reverse_bits(unsigned char octet)
{
int i = 8;
unsigned char res = 0;
while (i > 0)
{
res = res * 2 + (octet % 2);
octet = octet / 2;
i--;
}
return (res);
}
*/
// ******************** swap_bits ********************
/* QUESTION
Assignment name : swap_bits
Expected files : swap_bits.c
Allowed functions:
--------------------------------------------------------------------------------
Write a function that takes a byte, swaps its halves (like the example) and
returns the result.
Your function must be declared as follows:
unsigned char swap_bits(unsigned char octet);
Example:
1 byte
_____________
0100 | 0001
\ /
/ \
0001 | 0100
*/
/* ANSWER
unsigned char swap_bits(unsigned char octet)
{
return ((octet >> 4) || (octet << 4));
}
*/
// ********************************************************************
// ******************** snake_to_camel ********************
/* QUESTION
Assignment name : snake_to_camel
Expected files : snake_to_camel.c
Allowed functions: malloc, free, realloc, write
--------------------------------------------------------------------------------
Write a program that takes a single string in snake_case format
and converts it into a string in lowerCamelCase format.
A snake_case string is a string where each word is in lower case, separated by
an underscore "_".
A lowerCamelCase string is a string where each word begins with a capital letter
except for the first one.
Examples:
$>./snake_to_camel "here_is_a_snake_case_word"
hereIsASnakeCaseWord
$>./snake_to_camel "hello_world" | cat -e
helloWorld$
$>./snake_to_camel | cat -e
$
*/
/* ANSWER
#include <unistd.h>
int main(int ac,char **av)
{
int i = 0;
if (ac == 2)
{
while(av[1][i] != '\0')
{
if (av[1][i] == '_') {
i++;
av[1][i] -= 32;
}
write(1,&av[1][i],1);
i++;
}
}
write(1,"\n",1);
}
// dipnot: soruda newline ve ac ile ilgili bir anlatım yok ama
// ek olarak eklendi.
*/
// ********************************************************************
// ******************** union ********************
/* QUESTION
Assignment name : union
Expected files : union.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes two strings and displays, without doubles, the
characters that appear in either one of the strings.
The display will be in the order characters appear in the command line, and
will be followed by a \n.
If the number of arguments is not 2, the program displays \n.
Example:
$>./union zpadinton "paqefwtdjetyiytjneytjoeyjnejeyj" | cat -e
zpadintoqefwjy$
$>./union ddf6vewg64f gtwthgdwthdwfteewhrtag6h4ffdhsd | cat -e
df6vewg4thras$
$>./union "rien" "cette phrase ne cache rien" | cat -e
rienct phas$
$>./union | cat -e
$
$>
$>./union "rien" | cat -e
$
$>
*/
/* ANSWER
// ÖZET : iki stringi(av_1 & av_2) birleştir matchi(öncesinde) olmayanları yaz
#include <unistd.h>
int check(char c, char *str, int index)
{
int i = 0;
while (i < index)
{
if (str[i] == c)
return 0;
i++;
}
return 1;
}
int main(int ac, char **av)
{
int i = 0;
int j = 0;
int k = 0;
if (ac == 3)
{
while (av[1][i])
i++;
// av_1 ve av_2 uc uca eklendi (yeni string)
while (av[2][j])
{
av[1][i] = av[2][j];
i++;
j++;
}
i--; // passed null terminate
// yeni stringde dolaşma
while (k <= i)
{
if (check(av[1][k],av[1],k) == 1)
write (1 , &av[1][k],1);
k++;
}
}
write(1,"\n",1);
}
*/
// ********************************************************************
// ******************** wdmatch ********************
/* QUESTION
Assignment name : wdmatch
Expected files : wdmatch.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes two strings and checks whether it's possible to
write the first string with characters from the second string, while respecting
the order in which these characters appear in the second string.
If it's possible, the program displays the string, followed by a \n, otherwise
it simply displays a \n.
If the number of arguments is not 2, the program displays a \n.
Examples:
$>./wdmatch "faya" "fgvvfdxcacpolhyghbreda" | cat -e
faya$
$>./wdmatch "faya" "fgvvfdxcacpolhyghbred" | cat -e
$
$>./wdmatch "quarante deux" "qfqfsudf arzgsayns tsregfdgs sjytdekuoixq " | cat -e
quarante deux$
$>./wdmatch "error" rrerrrfiiljdfxjyuifrrvcoojh | cat -e
$
$>./wdmatch | cat -e
$
*/
/* ANSWER
// özet : av_1 in her elemanının av_2 de matchi varsa check edip yazıyoruz.
// dipnot : av_1 elemanları av_2 de ordered şekilde bulunmalı.
#include <unistd.h>
int main(int argc, char **argv)
{
int i = 0;
int j = 0;
int k = 0;
if (argc == 3)
{
while(argv[2][j] != '\0' && argv[1][j] != '\0')
{
if (argv[1][i] == argv[2][j])
i++;
j++;
}
// av_1 i bitiremediysek yani daha hala av_2 de match olamayan
// char lar varsa (tüm av yi döndüğümüzde bile) bu if e girmiyecek.
if (argv[1][i] == '\0') // !argv[1][i] şeklinde de yazılabilir.
{
while(argv[1][k])
{
write(1, &argv[1][k], 1);
k++;
}
}
}
write(1, "\n", 1);
return (0);
}
// dipnot: av[1][i] != '\0' => 1.arguman da tüm matchleri bulduğunda av2 de ,
// , av2 içindde boş yere dönememesi için.
*/
// ********************************************************************
// ------------------------ BİTTİ ------------------------ //
***********************************************
Level_3
***********************************************
// ******************** ft_list_size ********************
/* QUESTION
Assignment name : ft_list_size
Expected files : ft_list_size.c
Allowed functions:
--------------------------------------------------------------------------------
Write a function that returns the number of elements in the linked list that's
passed to it.
It must be declared as follows:
int ft_list_size(t_list *begin_list);
You must use the following structure in your program ft_list_size.c :
typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;
*/
/* ANSWER
// ayrı dosyada ft_list.h olarak tanımlancak;
typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;
// #include "ft_list.h"
int ft_list_size(t_list *begin_list) {
if (begin_list == 0)
return 0;
else
return (1 + ft_list_size(begin_list -> next));
}
*/
// ********************************************************************
// ******************** ft_range ********************
/* QUESTION
Assignment name : ft_range
Expected files : ft_range.c
Allowed functions: malloc
--------------------------------------------------------------------------------
Write the following function:
int *ft_range(int start, int end);
It must allocate (with malloc()) an array of integers, fill it with consecutive
values that begin at start and end at end (Including start and end !), then
return a pointer to the first value of the array.
Examples:
- With (1, 3) you will return an array containing 1, 2 and 3.
- With (-1, 2) you will return an array containing -1, 0, 1 and 2.
- With (0, 0) you will return an array containing 0.
- With (0, -3) you will return an array containing 0, -1, -2 and -3.
*/
/* ANSWER
#include <stdlib.h>
int *ft_range (int start , int end)
{
int i = 0;
int size = abs((end - start)) + 1;
int *res;
res = malloc(sizeof(int)*size);
if (!res)
return (NULL);
if (start < end)
{
while (start <= end)
{
res[i] = start;
start++;
i++;
}
}
else {
while (start >= end)
{
res[i] = start;
start--;
i++;
}
}
return (res);
}
*/
// ********************************************************************
// ******************** paramsum ********************
/* QUESTION
Assignment name : paramsum
Expected files : paramsum.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that displays the number of arguments passed to it, followed by
a newline.
If there are no arguments, just display a 0 followed by a newline.
Example:
$>./paramsum 1 2 3 5 7 24
6
$>./paramsum 6 12 24 | cat -e
3$
$>./paramsum | cat -e
0$
$>
*/
/* ANSWER
#include <unistd.h>
void ft_putnbr(int n)
{
if (n >= 10)
{
ft_putnbr(n / 10);
}
char c = (n%10) + '0';
write(1,&c,1);
}
int main(int ac , char **av)
{
(void) av; // av yi kullanmadığımız için
ft_putnbr(ac - 1);
write(1,"\n",1);
}
*/
// ********************************************************************
// ******************** lcm ********************
/* QUESTION
Assignment name : lcm
Expected files : lcm.c
Allowed functions:
--------------------------------------------------------------------------------
Write a function who takes two unsigned int as parameters and returns the
computed LCM of those parameters.
LCM (Lowest Common Multiple) of two non-zero integers is the smallest postive
integer divisible by the both integers.
A LCM can be calculated in two ways:
- You can calculate every multiples of each integers until you have a common
multiple other than 0
- You can use the HCF (Highest Common Factor) of these two integers and
calculate as follows:
LCM(x, y) = | x * y | / HCF(x, y)
| x * y | means "Absolute value of the product of x by y"
If at least one integer is null, LCM is equal to 0.
Your function must be prototyped as follows:
unsigned int lcm(unsigned int a, unsigned int b);
*/
/* ANSWER
// 1.yol
unsigned int lcm(unsigned int a, unsigned int b)
{
unsigned int n;
if (a == 0 || b == 0)
return (0);
if (a > b)
n = a;
else
n = b;
while (1)
{
if (n % a == 0 && n % b == 0)
return (n);
n++;
}
}
// 2.yol (daha mantıklı ve verimli)
unsigned int lcm(unsigned int a, unsigned int b)
{
unsigned int lcm;
lcm = (a < b) ? b : a;
// büyük olanı seçip daha az adımda en küçük ortak kata erişmek için.
while (a > 0 && b > 0)
{
if (lcm % a == 0 && lcm % b == 0)
return (lcm);
lcm += (a < b) ? b : a;
}
return (0);
}
*/
// ********************************************************************
// ******************** pgcd ********************
/* QUESTION
Assignment name : pgcd
Expected files : pgcd.c
Allowed functions: printf, atoi, malloc, free
--------------------------------------------------------------------------------
Write a program that takes two strings representing two strictly positive
integers that fit in an int.
Display their highest common denominator followed by a newline (It's always a
strictly positive integer).
If the number of parameters is not 2, display a newline.
Examples:
$> ./pgcd 42 10 | cat -e
2$
$> ./pgcd 42 12 | cat -e
6$
$> ./pgcd 14 77 | cat -e
7$
$> ./pgcd 17 3 | cat -e
1$
$> ./pgcd | cat -e
$
*/
/* ANSWER
#include <stdio.h>
#include <stdlib.h>
void pgcd(int a, int b)
{
int n = a;
// n = a < b ? a : b denilebilir ;
// en büyük ortak böleni bulmak için küçük olandan başlayarak
// azaltmak daha az adımda buldurur çünkü.
while (n > 0)
{
if (a % n == 0 && b % n == 0)
{
printf("%d", n);
return;
}
n--;
}
}
int main(int argc, char **argv)
{
if (argc == 3)
pgcd(atoi(argv[1]), atoi(argv[2]));
printf("\n");
return (0);
}
*/
// ********************************************************************
// ******************** tab_mult ********************
/* QUESTION
Write a program that displays a number's multiplication table.
The parameter will always be a strictly positive number that fits in an int,
and said number times 9 will also fit in an int.
If there are no parameters, the program displays \n.
Examples:
$>./tab_mult 9
1 x 9 = 9
2 x 9 = 18
3 x 9 = 27
4 x 9 = 36
5 x 9 = 45
6 x 9 = 54
7 x 9 = 63
8 x 9 = 72
9 x 9 = 81
$>./tab_mult 19
1 x 19 = 19
2 x 19 = 38
3 x 19 = 57
4 x 19 = 76
5 x 19 = 95
6 x 19 = 114
7 x 19 = 133
8 x 19 = 152
9 x 19 = 171
$>
$>./tab_mult | cat -e
$
$>
*/
/* ANSWER
// grademe ile benzer
#include <unistd.h>
int atoi(char *av_1)
{
int i = 0;
int result = 0;
while (av_1[i] != '\0')
{
if (av_1[i] <= '9' && av_1[i] >= '0')
result = result * 10 + (av_1[i] - '0');
i++;
}
return result;
}
void ft_putnbr(int n)
{
if (n >= 10)
ft_putnbr(n / 10);
char c = (n % 10) + '0';
write(1, &c, 1);
}
int main(int ac, char **av)
{
int i = 1;
int multiplier = 1;
if (ac != 2)
write(1, "\n", 1);
if (ac == 2)
{
int av_1 = atoi(av[1]);
while (i <= 9)
{
ft_putnbr(i);
write(1, " x ", 3);
ft_putnbr(av_1);
write(1, " = ", 3);
ft_putnbr(av_1 * i);
write(1, "\n", 1);
i++;
}
}
// write(1, "\n", 1); // sakın bu şekilde yazma son satır bozuluyor.
}
*/
// ********************************************************************
// ******************** print_hex ********************
/* QUESTION
Assignment name : print_hex
Expected files : print_hex.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a positive (or zero) number expressed in base 10,
and displays it in base 16 (lowercase letters) followed by a newline.
If the number of parameters is not 1, the program displays a newline.
Examples:
$> ./print_hex "10" | cat -e
a$
$> ./print_hex "255" | cat -e
ff$
$> ./print_hex "5156454" | cat -e
4eae66$
$> ./print_hex | cat -e
$
*/
/* ANSWER
// benim çözüm (grademe ile benzer)
#include <unistd.h>
void convert_hex(int num)
{
char *base_16 = "0123456789abcdef";
if (num > 16)
convert_hex(num / 16);
char c = base_16[num % 16];
write (1, &c,1);
}
int ft_atoi(char *av)
{
int i = 0;
int result = 0;
while (av[i] != '\0')
{
if (av[i] <= '9' && av[i] >= '0')
result = result * 10 + (av[i] - '0');
i++;
}
return (result);
}
int main(int ac,char **av)
{
int i = 0;
if (ac == 2) {
int av_1 = ft_atoi(av[1]);
convert_hex(av_1);
}
write (1, "\n",1);
}
*/
// ******************** hidenp ********************
// ********************************************************************
// ******************** epur_str ********************
/* QUESTION
Assignment name : epur_str
Expected files : epur_str.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string, and displays this string with exactly one
space between words, with no spaces or tabs either at the beginning or the end,
followed by a \n.
A "word" is defined as a part of a string delimited either by spaces/tabs, or
by the start/end of the string.
If the number of arguments is not 1, or if there are no words to display, the
program displays \n.
Example:
$> ./epur_str "See? It's easy to print the same thing" | cat -e
See? It's easy to print the same thing$
$> ./epur_str " this time it will be more complex . " | cat -e
this time it will be more complex .$
$> ./epur_str "No S*** Sherlock..." "nAw S*** ShErLaWQ..." | cat -e
$
$> ./epur_str "" | cat -e
$
$>
*/
/* ANSWER
// özet : verile n argümanı tek boşluk atarak yazma .
#include <unistd.h>
int main(int argc, char const *argv[])
{
int i = 0;
int flg = 0; // başlangıçda değerler ver yada if den önce!!!
if (argc == 2)
{
while (argv[1][i] == ' ' || argv[1][i] == '\t')
i++;
while (argv[1][i])
{
if (argv[1][i] == ' ' || argv[1][i] == '\t')
flg = 1;
if (!(argv[1][i] == ' ' || argv[1][i] == '\t'))
{
if (flg)
write(1, " ", 1);
flg = 0;
write(1, &argv[1][i], 1);
}
i++;
}
}
write(1, "\n", 1);
return (0);
}
*/
// ********************************************************************
// ******************** epur_str ********************
/* QUESTION
Assignment name : expand_str
Expected files : expand_str.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string and displays it with exactly three spaces
between each word, with no spaces or tabs either at the beginning or the end,
followed by a newline.
A word is a section of string delimited either by spaces/tabs, or by the
start/end of the string.
If the number of parameters is not 1, or if there are no words, simply display
a newline.
Examples:
$> ./expand_str "See? It's easy to print the same thing" | cat -e
See? It's easy to print the same thing$
$> ./expand_str " this time it will be more complex " | cat -e
this time it will be more complex$
$> ./expand_str "No S*** Sherlock..." "nAw S*** ShErLaWQ..." | cat -e
$
$> ./expand_str "" | cat -e
$
$>
*/
/* ANSWER
// özet : verilen argumanı 3 boşluklu şekilde yazma
#include <unistd.h>
int main (int ac, char **av)
{
int i = 0;
int flag = 0;
if (ac == 2) {
while (av[1][i] == ' ' || av[1][i] == '\t')
i++;
while (av[1][i]) {
if (av[1][i] == '\t' || av[1][i] == ' ')
flag = 1;
if (!(av[1][i] == '\t' || av[1][i] == ' '))
{
if (flag)
write(1," ",3);
flag = 0;
write(1, &av[1][i], 1);
}
i++;
}
}
write (1, "\n", 1);
}
*/
// ********************************************************************
// ******************** ft_strcapitalizer ******************** KENDİN YAZ
/* QUESTION
Assignment name : str_capitalizer
Expected files : str_capitalizer.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes one or several strings and, for each argument,
capitalizes the first character of each word (If it's a letter, obviously),
puts the rest in lowercase, and displays the result on the standard output,
followed by a \n.
A "word" is defined as a part of a string delimited either by spaces/tabs, or
by the start/end of the string. If a word only has one letter, it must be
capitalized.
If there are no arguments, the progam must display \n.
Example:
$> ./str_capitalizer | cat -e
$
$> ./str_capitalizer "a FiRSt LiTTlE TESt" | cat -e
A First Little Test$
$> ./str_capitalizer "__SecONd teST A LITtle BiT Moar comPLEX" " But... This iS not THAT COMPLEX" " Okay, this is the last 1239809147801 but not the least t" | cat -e
__second Test A Little Bit Moar Complex$
But... This Is Not That Complex$
Okay, This Is The Last 1239809147801 But Not The Least T$
$>
*/
/* ANSWER
#include <unistd.h>
int main(int ac, char **av)
{
int i = 0;
if (ac == 2)
{
// ilk gelen kelimenin ilk harfi ve tek harfli kelime için;
if (av[1][i] >= 'a' && av[1][i] <= 'z')
av[1][i] -= 32;
write (1, &av[1][i],1);
while (av[1][i])
{
// İlk harfden sonra gelenleri küçültmek içn
if (av[1][i] >= 'A' && av[1][i] <= 'Z')
av[1][i] += 32;
// boşluk sonrası başlayan kelimenin ilk harfi için
if ((av[1][i] >= 'a' && av[1][i] <= 'z') &&
(av[1][i-1]) == ' ' || av[1][i - 1] == '\t')
av[1][i] -= 32;
write (1, &av[1][i],1);
i++;
}
}
write (1,"\n",1);
return (0);
}
*/
// ********************************************************************
// ******************** rstr_capitalizer ********************
/* QUESTION
Assignment name : rstr_capitalizer
Expected files : rstr_capitalizer.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes one or more strings and, for each argument, puts
the last character that is a letter of each word in uppercase and the rest
in lowercase, then displays the result followed by a \n.
A word is a section of string delimited by spaces/tabs or the start/end of the
string. If a word has a single letter, it must be capitalized.
A letter is a character in the set [a-zA-Z]
If there are no parameters, display \n.
Examples:
$> ./rstr_capitalizer | cat -e
$
$> ./rstr_capitalizer "a FiRSt LiTTlE TESt" | cat -e
A firsT littlE tesT$
$> ./rstr_capitalizer "SecONd teST A LITtle BiT Moar comPLEX" " But... This iS not THAT COMPLEX" " Okay, this is the last 1239809147801 but not the least t" | cat -e
seconD tesT A littlE biT moaR compleX$
but... thiS iS noT thaT compleX$
okay, thiS iS thE lasT 1239809147801 buT noT thE leasT T$
$>
*/
/* ANSWER
// özet: farkı son karekteri büyütmek diğerlerini küçültmek
// aynı şekilde ilk len kelime tek harfli ise onu büyütmek.
#include <unistd.h>
int main(int ac, char **av)
{
int i = 0;
if (ac == 2)
{
while (av[1][i] != '\0')
{
if (av[1][i] >= 'A' && av[1][i] <= 'Z')
av[1][i] += 32;
if ((av[1][i] >= 'a' && av[1][i] <= 'z') &&
(av[1][i + 1] == ' ' || av[1][i + 1] == '\t' || av[1][i + 1] == '\0'))
av[1][i] -= 32;
write(1, &av[1][i], 1);
i++;
}
}
write(1, "\n", 1);
}
*/
// ******************** hidenp ********************
/* QUESTION
Assignment name : hidenp
Expected files : hidenp.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program named hidenp that takes two strings and displays 1
followed by a newline if the first string is hidden in the second one,
otherwise displays 0 followed by a newline.
Let s1 and s2 be strings. We say that s1 is hidden in s2 if it's possible to
find each character from s1 in s2, in the same order as they appear in s1.
Also, the empty string is hidden in any string.
If the number of parameters is not 2, the program displays a newline.
Examples :
$>./hidenp "fgex.;" "tyf34gdgf;'ektufjhgdgex.;.;rtjynur6" | cat -e
1$
$>./hidenp "abc" "2altrb53c.sse" | cat -e
1$
$>./hidenp "abc" "btarc" | cat -e
0$
$>./hidenp | cat -e
$
$>
*/
/* ANSWER
// özet: wd_matchin 1, 0 versiyonu
#include <unistd.h>
int main(int argc, char **argv)
{
int i = 0;
int j = 0;
if (argc == 3)
{
while(argv[2][j] != '\0' && argv[1][i] != '\0')
{
if (argv[1][i] == argv[2][j])
i++;
j++;
}
if (argv[1][i] == '\0')
write(1,"1",1);
else
write(1,"0",1);
}
write(1, "\n", 1);
return (0);
}
*/
// ********************************************************************
// ******************** add_prime_sum ********************
/* QUESTION
Assignment name : add_prime_sum
Expected files : add_prime_sum.c
Allowed functions: write, exit
--------------------------------------------------------------------------------
Write a program that takes a positive integer as argument and displays the sum
of all prime numbers inferior or equal to it followed by a newline.
If the number of arguments is not 1, or the argument is not a positive number,
just display 0 followed by a newline.
Yes, the examples are right.
Examples:
$>./add_prime_sum 5
10
$>./add_prime_sum 7 | cat -e
17$
$>./add_prime_sum | cat -e
0$
$>
*/
/* ANSWER
// ÖZET = 5 => 2 , 3 , 5 => 2 + 3 + 5 = 10
#include <unistd.h>
// short atoi
int ft_atoi(char *str)
{
int n = 0;
while (*str >= '0' && *str <= '9')
{
n *= 10;
n += *str - '0'; // in-one-line => n = n * 10 + (*str - '0') ;
++str;
}
return (n);
}
// int to char
void ft_putnbr(int n)
{
if (n >= 10)
ft_putnbr(n / 10);
char c = (n % 10) + '0';
write(1, &c, 1);
}
int is_prime(int n)
{
int i = 2;
while (i < n)
{
if (n % i == 0)
return (0);
++i;
}
return (1);
}
int add_prime_sum(int n)
{
int sum = 0;
int i = 2;
while (i <= n)
{
if (is_prime(i) == 1)
sum += i;
++i;
}
return (sum);
}
int main(int argc, char **argv)
{
int n;
if (argc == 2 && (n = ft_atoi(argv[1])))
ft_putnbr(add_prime_sum(n));
else
ft_putnbr(0);
write(1, "\n", 1);
return (0);
}
*/
// ********************************************************************
// ********************************************************************
// ******************** atoi_base ********************
/* ANSWER
char to_lower(char c)
{
if (c >= 'A' && c <= 'Z')
return (c + ('a' - 'A')); // daha net (c - 'A') + 'a'
return (c);
}
int getdigit(char c, int digit_in_base)
{
int max_digit; // tabanın toplam kaç digit(basamak)
// find base
if(digit_in_base <= 10) // bence < 10 olmalı en yüksek taban 9 olması için
max_digit = digit_in_base + '0'; // 0123456789
else
max_digit = digit_in_base - 10 + 'a'; // abcdef
// find digit element as max_digit base
if (c >= '0' && c <= '9' && c <= max_digit)
return (c - '0');
else if (c >= 'a' && c <= 'f' && c <= max_digit)
return (10 + c - 'a');
else
return (-1);
}
int ft_atoi_base(const char *str, int str_base)
{
int result = 0;
int sign = 1;
int digit;
if (*str == '-')
{
sign = -1;
}
while ((digit = get_digit(to_lower(*str),str_base)) >= 0)
{
result = result * str_base;
result = result + (digit * sign);
++str;
}
return (result);
}
*/
// ********************************************************************
// ********************************************************************
***********************************************
Level_4
***********************************************
// ******************** flood_fill ********************
/* ANSWER
typedef struct s_point {
int x;
int y;
} t_point;
void fill(char **tab, t_point size, t_point cur ,char to_fill ) {
if (cur.y < 0 || cur.y >= size.y || cur.x < 0 || cur.x >= size.x
|| tab[cur.y][cur.x] != to_fill)
return;
tab[cur.y][cur.x] = 'F'; // fill with f
// fill begin to to end
fill(tab,size, (t_point){cur.x - 1,cur.y},to_fill); // öncesiden
fill(tab,size, (t_point){cur.x + 1,cur.y},to_fill); // sonrasından
fill(tab,size, (t_point){cur.x , cur.y - 1},to_fill); // yukarısından
fill(tab,size, (t_point){cur.x , cur.y + 1}, to_fill); // aşağısından
}
void flood_fill (char **tab,t_point size,t_point begin) {
fill (tab, size, begin, tab[begin.y][begin.x]);
}
*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// ******************** f_prime ********************
/* ANSWER
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int i;
int number;
if (ac == 2)
{
i = 1;
number = atoi(av[1]);
if (number == 1)
printf ("1");
while (number >= ++i) // i yi 2 leyip devam ettim
{
if (number % i == 0) // tam bölünürse
{
printf("%d",i); // yazıcaz
if ( number == i) // number böle böle 1 olduğundan i de her işlem sonunca 1 olacağından işlem sonlanır.
break;
printf("*");
number /= i; // number küüçlcek
i = 1; // !!! işlemler sonunda i tekrar 1 çevirlcek
}
}
}
}
*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// ******************** itoa ********************
/* ANSWER
#include <stdlib.h>
char *ft_itoa(int nbr)
{
int n = nbr;
int len = 0;
if (nbr <= 0)
len++; // negatif işareti için
// n sıfır olana kadar bölüp basmaak sayısını bulduk
while (n > 0)
{
n /= 10;
len++;
}
char *result = (char *)malloc(sizeof(char)*(len + 1));
if (result == NULL)
{
return NULL;
}
if (nbr == 0)
{
result[0] = '0';
return (result);
}
if (nbr < 0)
{
result [0] = '-';
nbr = -nbr; // numarayı pozitefe çek
}
while (nbr > 0)
{
result[--len] = nbr % 10 + '0'; // birler basamağını yazdık
nbr /= 10; // sayıyı küçülttük
}
return result;
}
*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// ******************** list_remove_if ********************
/*
typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;
void ft_list_remove_if(t_list **begin_list, void *data_ref , int (*cmp)())
{
if (begin_list == 0 || *begin_list == 0)
return;
t_list *cur = *begin_list;
if (cmp(cur -> data,data_ref) == 0)
{
*begin_list = cur->next;
free(cur);
ft_list_remove_if(begin_list,data_ref,cmp);
}
cur = *begin_list;
ft_list_remove_if(&cur->next,data_ref,cmp);
}
*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// ******************** ft_split ********************
/* ANSWER
#include <stdlib.h>
int ft_word_len(char *str)
{
int i = 0;
while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t' && str[i] != '\n')
i++;
return i;
}
char *word_dupe(char *str)
{
int i = 0;
int len = ft_word_len(str);
char *word = malloc(sizeof(char) * (len + 1));
word[len] == '\0';
while ( i < len )
{
word[i] = str[i];
i++;
}
return (word);
}
void fill_words (char **array, char *str)
{
int word_index = 0;
while (*str == ' ' || *str == '\t' || *str == '\n')
++str;
while (*str != '\0')
{
array[word_index] = word_dupe(str);
++word_index;
while (*str != '\0' && *str != ' ' && *str != '\t' && str != '\n')
++str;
while (*str == ' ' || *str == '\t' || *str == '\n')
++str;
}
}
int count_words(char *str)
{
int num_words;
while (*str == ' ' || *str == '\t' || *str == '\n')
++str;
while (*str != '\0')
{
++num_words;
while (*str != '\0' && *str != ' ' && *str != '\t' && *str != '\n')
++str;
while (*str == ' ' || *str == '\t' || *str == '\n')
++str;
}
return (num_words);
}
char **ft_split(char *str)
{
int num_words;
char **array;
num_words = count_words(str);
array = malloc(sizeof(char *)*(num_words + 1));
array[num_words] = 0 ;
fill_words(array,str);
}
*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// ******************** ft_list_foreach ********************
/* ANSWER
void ft_list_foreach(t_list *begin_list, void (*f)(void *))
{
while (begin_list != NULL)
{
if (beginlist -> data)
(*f)(beginlist -> data)
beginlist = begin_list -> next;
}
}
*/
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment