The Piscine was good but the time has past. This serie of exercises will help you to remind all the basics you’ve learned during the piscine. Functions, loops, pointers, structures, let’s remind together the syntactic and semantic bases of the C
- Task:
$> ls -l
total 42
drwx--xr-x 2 login wheel XX Jun 1 20:47 test0
-rwx--xr-- 1 login wheel 4 Jun 1 21:46 test1
dr-x---r-- 2 login wheel XX Jun 1 22:45 test2
-r-----r-- 2 login wheel 1 Jun 1 23:44 test3
-rw-r----x 1 login wheel 2 Jun 1 23:43 test4
-r-----r-- 2 login wheel 1 Jun 1 23:44 test5
lrwxr-xr-x 1 login wheel 5 Jun 1 22:20 test6 -> test0
$>
Remember about -h
man touch
is reads
-t Change the access and modification times to the specified time instead of the current time of day. The argument is of the form
``[[CC]YY]MMDDhhmm[.SS]'' where each pair of letters represents the following:
CC The first two digits of the year (the century).
YY The second two digits of the year. If ``YY'' is specified, but ``CC'' is not, a value for ``YY'' between 69 and 99 results in a ``CC''
value of 19. Otherwise, a ``CC'' value of 20 is used.
MM The month of the year, from 01 to 12.
DD the day of the month, from 01 to 31.
hh The hour of the day, from 00 to 23.
mm The minute of the hour, from 00 to 59.
SS The second of the minute, from 00 to 61.
If the ``CC'' and ``YY'' letter pairs are not specified, the values default to the current year. If the ``SS'' letter pair is not specified, the
value defaults to 0.
- Result:
touch -h -t "06012220" test6
- Result:
z[enter]
In a file called clean place the command line that will search for all files - in the current directory as well as in its sub-directories - with a name ending by ~, or with a name that start and end by #
- The command line will show and erase all files found.
- Only one command is allowed: no ’;’ or ’&&’ or other shenanigans.
- You remember about -
touch none{0..10}~
for create 10 file's -name
-name pattern True if the last component of the pathname being examined matches pattern. Special shell pattern matching characters (
['',
]'',*'', and
?'') may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (``'').
- We will use next flags:
-delete
- all know about this, read this, for all information https://rtfm.co.ua/komanda-find-i-eyo-opcii-v-primerax/-name
- input your find's name-exec ls -l {} \;
- more-o
- connection
- Result:
find . -name "#*" -print -delete -o -name "*#" -delete -print -o -name "*~" -delete -print
- Result:
find . "*.sh" -print | awk -F "." '/.sh/{ print $2 }' | awk -F "/" '{ print $2 }'
man awk
- I will use grep
man ifconfig
man awk
- Result:
ifconfig en0 | grep ether -w | awk -F " " '{ print $2 }'
- Result:
touch '"\?$*’KwaMe’*$?\"'
p.s. Simple -
''
orecho 42 > '"\?$*’KwaMe’*$?\"'
- Task:
Create a function that displays the alphabet in lowercase, on a single line, by ascending order, starting from the letter ’a’.
- Classic
This function is easier to enter, that would constantly use. void - is NONE type, func - return NONE, but put
char
character
void ft_putchar(char c)
{
write(1, &c, 1);
}
2.1 man ASCII
97 a 98 b 99 c 100 d 101 e 102 f 103 g
104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o
112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w
120 x 121 y 122 z
(i >= 97 && i <= 122)
while i fits the condition
- Result:
void ft_print_alphabet(void)
{
int i = 97;
while (i >= 97 && i <= 122)
ft_putchar(i++);
ft_putchar('\n');
}
- Task:
Create a function that displays all digits, on a single line, by ascending order.
man ascii
:
48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7
56 8 57 9
- Result:
void ft_print_numbers(void)
{
int i = 48;
while (i >=47 && i <= 57)
ft_putchar(i++);
ft_putchar('\n');
}
- Task:
Create a function that displays ’N’ or ’P’ depending on the integer’s sign entered as a parameter. If n is negative, display ’N’. If n is positive or null, display ’P’.
- Result:
void ft_is_negative(int n)
{
if (n < 0)
ft_putchar('N');
else if (n > 0)
ft_putchar('P');
else
ft_putchar('P');
}
- Task:
Create a function that takes a pointer to int as a parameter, and sets the value "42" to that int.
- About pointers:
For check pointer, write program:
int main()
{
int i = 42;
ft_ft(&i);
return(0);
}
Attention in terminal was:
ft_ft.c:14:9: warning: incompatible integer to pointer conversion passing 'int' to
parameter of type 'int *'; take the address with & [-Wint-conversion]
ft_ft(i);
^
&
This message was displayed, because we did not insert a pointer to the link
- Result
void ft_ft(int *nbr)
{
int c;
c = *nbr;
}
- Task:
Create a function that swaps the value of two integers whose addresses are entered as parameters.
Here’s how it should be prototyped:
void ft_swap(int *a, int *b);
- About pointer's:
Watch video - it's help you.
Then, need understand, why I use pointers? Don't remember - Allowed functions : None
For check result, I was used:
int main()
{
int a = 1;
int b = 2;
ft_swap(&a, &b);
return(0);
}
- Result:
void ft_swap(int *a, int *b)
{
char c;
c = *a;
*a = *b;
*b = c;
}
- Task:
Create a function ft_div_mod prototyped like this :
void ft_div_mod(int a, int b, int *div, int *mod);
This function divides parameters a by b and stores the result in the int pointed by div. It also stores the remainder of the division of a by b in the int pointed by mod.
- About div & standart operation in C
Operator name | Syntax | C++ prototype examples |
---|---|---|
Multiplication | a * b | K::operator *(S b); |
Division | a / b | K::operator /(S b); |
Modulo | a % b | R K::operator %(S b); |
2.1 For check this exercise:
int main()
{
int a = 41;
int b = 11;
int *div;
int *mod;
ft_div_mod(a, b, div, mod);
printf("%d\n", *div);
printf("%d\n", *mod);
return(0);
}
don't remember about
#include <stdio.h>
- Result
void ft_div_mod(int a, int b, int *div, int *mod)
{
*div = a / b;
*mod = a % b;
}
- Task:
Create an iterated function that returns a number. This number is the result of a factorial operation based on the number given as a parameter.
If there’s an error, the function should return 0.
Here’s how it should be prototyped :
c int ft_iterative_factorial(int nb);
Your function must return its result in less than two seconds.
- About factorial:
- Result:
int ft_iterative_factorial(int nb)
{
int res;
int i;
res = 1;
i = 1;
if (nb >= 0 && nb <= 12)
{
while (i <= nb)
res *= i++;
return(res);
}
else
return(0);
}
- Task:
Reproduce the behavior of the function strlen (man strlen). Here’s how it should be prototyped:
int ft_strlen(char *str);
- Me help's watch this video:Рекурсия. Репка и матрёшка
- Result:
int ft_recursive_factorial(int nb)
{
if (nb == 0)
return(1);
else if (nb < 0 || nb > 12)
return(0);
else
{
c = c * nb;
ft_recursive_factorial(nb -1);
}
return (c);
}
p.s. Check this:
else if (nb < 0 || nb > 12)
, maybe is LYING? p.s.s. use global value.
- Task:
Create a function that returns the square root of a number (if it exists), or 0 if the square root is an irrational number. Here’s how it should be prototyped :
int ft_sqrt(int nb);
Your function must return its result in less than two seconds.
- About square, watch video Быстрое вычисление квадратных корней
- Result:
int ft_sqrt(int nb)
{
int i = 1;
if (nb < 0 || nb >= 32767)
return(0);
else
{
while (i < nb && (i * i) < nb)
i++;
}
return (i);
}
p.s. Rememder about
int
-if (nb < 0 || nb >= 32767)
Classic:
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str)
write(1, str++, 1);
}
Classic:
int ft_strlen(char *str)
{
int len = 0;
while (*str)
{
len++;
str++;
}
return(len);
}
Check this:
int main()
{
char str[] = "Hello world";
printf("Len of %s = %d\n", str, ft_strlen(str));
return(0);
}
p.s., don't remember about:
#include <stdio.h>
, if you want check this exercize
- About strcmp:
DESCRIPTION
The strcmp() and strncmp() functions lexicographically compare the null-
terminated strings s1 and s2.
The strncmp() function compares not more than n characters. Because
strncmp() is designed for comparing strings rather than binary data,
characters that appear after a `\0' character are not compared.
RETURN VALUES
The strcmp() and strncmp() functions return an integer greater than,
equal to, or less than 0, according as the string s1 is greater than,
equal to, or less than the string s2. The comparison is done using
unsigned characters, so that `\200' is greater than `\0'.
- Source of strcmp:
/* Compare S1 and S2, returning less than, equal to or
greater than zero if S1 is lexicographically less than,
equal to or greater than S2. */
int
STRCMP (const char *p1, const char *p2)
{
const unsigned char *s1 = (const unsigned char *) p1;
const unsigned char *s2 = (const unsigned char *) p2;
unsigned char c1, c2;
do
{
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 == '\0')
return c1 - c2;
}
while (c1 == c2);
return c1 - c2;
}
Note this: return only value c1 - c2;
- Result:
int ft_strcmp(char *s1, char *s2)
{
int lenS1 = 0;
int lenS2 = 0;
while (*s1)
{
lenS1++;
s1++;
}
while (*s2)
{
lenS2++;
s2++;
}
return (lenS2 - lenS1);
}
How to check this?
int main()
{
char str1[] = "Hello world";
char str2[] = "World Hello1";
printf("%d\n", ft_strcmp(str1, str2));
return(0);
}
p.s., don't remember about:
#include <stdio.h>
, if you want check this exercize
- Task:
$>./a.out test1 test2 test3
test1 test2
test3 $>
DO IT!
- Result:
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
while (*str)
ft_putchar(*str++);
ft_putchar('\n');
}
int main(int argc, char **argv)
{
int i = 1;
if (argc > 1)
while(argv[i])
ft_putstr(argv[i++]);
return(0);
}
p.s. - use
(argc > 1)
, remember about this!
- Task:
> • We’re dealing with a program here, you should therefore have a function main in your .c file.
> • Create a program that displays its given arguments sorted by ascii order.
> • It should display all arguments, except for argv[0].
> • All arguments have to have their own line.
## CXXIV Exercise 20 : ft_strdup
## XXV Exercise 21 : ft_range
## XXVI Exercise 22 : ft_abs.h
## XXVIIExercise 23 : ft_point.h
## XXVIIIExercise 24 : Makefile
## XXIX Exercise 25 : ft_foreach
## XXX Exercise 26 : ft_count_if
## XXXI Exercise 27 : display_file
## XXXIIRendu et peer-évaluation
---
CHEAT GIT
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
---
END