This project is about what and how to use pointers and arrays and the differences between the two as well as how to use and manipulate strings in C.
- tests: Folder of test files. Provided by Holberton School.
- _putchar.c: C function that writes a character to
stdout
.
- main.h: Header file containing prototypes for all functions written in the project.
File | Prototype |
---|---|
0-reset_to_98.c |
void reset_to_98(int *n); |
1-swap.c |
void swap_int(int *a, int *b); |
2-strlen.c |
int _strlen(char *s); |
3-puts.c |
void _puts(char *str); |
4-print_rev.c |
void print_rev(char *s); |
5-rev_string.c |
void rev_string(char *s); |
6-puts2.c |
void puts2(char *str); |
7-puts_half.c |
void puts_half(char *str); |
8-print_array.c |
void print_array(int *a, int n); |
9-strcpy.c |
char *_strcpy(char *dest, char *src); |
100-atoi.c |
int _atoi(char *s); |
0-strcat.c |
char *_strcat(char *dest, char *src); |
1-strncat.c |
char *_strncat(char *dest, char *src, int n); |
2-strncpy.c |
char *_strncpy(char *dest, char *src, int n); |
3-strcmp.c |
int _strcmp(char *s1, char *s2); |
4-rev_array.c |
void reverse_array(int *a, int n); |
5-string_toupper.c |
char *string_toupper(char *); |
6-cap_string.c |
char *cap_string(char *); |
7-leet.c |
char *leet(char *); |
0-memset.c |
char *_memset(char *s, char b, unsigned int n); |
1-memcpy.c |
char *_memcpy(char *dest, char *src, unsigned int n); |
2-strchr.c |
char *_strchr(char *s, char c); |
3-strspn.c |
unsigned int _strspn(char *s, char *accept); |
4-strpbrk.c |
char *_strpbrk(char *s, char *accept); |
5-strstr.c |
char *_strstr(char *haystack, char *needle); |
7-print_chessboard.c |
void print_chessboard(char (*a)[8]); |
Compile's flags
-Wall -pedantic -Werror -Wextra -std=gnu89
Example
julien@ubuntu:~/$ cat 7-main.c
#include "main.h"
/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
char *str;
str = "0123456789";
puts_half(str);
return (0);
}
julien@ubuntu:~/$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 7-main.c 7-puts_half.c -o 7-puts_half
julien@ubuntu:~/$ ./7-puts_half
56789
julien@ubuntu:~/$
-
0. 98 Battery st.
- 0-reset_to_98.c: C function that takes a pointer to an
int
as a parameter and updates the value it points to to98
.
- 0-reset_to_98.c: C function that takes a pointer to an
-
1. Don't swap horses in crossing a stream
- 1-swap.c: C function that swaps the value of two integers.
-
2. This report, by its very length, defends itself against the risk of being read
- 2-strlen.c: C function that returns the length of a string.
-
3. I do not fear computers. I fear the lack of them
- 3-puts.c: C function that prints a string, followed by a new line,
to
stdout
.
- 3-puts.c: C function that prints a string, followed by a new line,
to
-
4. I can only go one way. I've not got a reverse gear
- 4-print_rev.c: C function that prints a string, in reverse, followed by a new line.
-
5. A good engineer thinks in reverse and asks himself about the stylistic consequences of the components and systems he proposes
- 5-rev_string.c: C function that reverses a string.
-
6. Half the lies they tell about me aren't true
- 6-puts2.c: C function that prints every other character of a string, followed by a new line.
-
7. Winning is only half of it. Having fun is the other half
- 7-puts_half.c: C function that prints the second half of a string, followed by a new line.
-
8. Arrays are not pointers
- 8-print_array.c: C function that prints an input
n
elements of an array of integers, followed by a new line.- Numbers are separated by a comma followed by a space.
- Numbers are displayed in the same order as they are stored in the array.
- 8-print_array.c: C function that prints an input
-
9. strcpy
- 9-strcpy.c: C function that copies the string pointed to by
src
, including the terminating null byte (\0
), to the buffer pointed to bydest
.- Returns the pointer to
dest
.
- Returns the pointer to
- 9-strcpy.c: C function that copies the string pointed to by
-
10. Great leaders are willing to sacrifice the numbers to save the people. Poor leaders sacrifice the people to save the numbers
- 100-atoi.c: C function that converts a string to an integer
without using
long
, new variable arrays, or hard-coded special values, as follows:- The number in the string can be preceded by an infinite number of characters.
- Takes into account all
+
and-
signs before the number. - If there are no numbers in the string, the function returns
0
.
- 100-atoi.c: C function that converts a string to an integer
without using
-
0. strcat
- 0-strcat.c: C function that concatenates two strings.
- Adds a terminating null byte at end.
- 0-strcat.c: C function that concatenates two strings.
-
1. strncat
- 1-strncat.c: C function that concatenates two strings using at most
an inputted number of bytes.
- Adds a terminating null byte at end, unless source string is longer than maximum byte number.
- 1-strncat.c: C function that concatenates two strings using at most
an inputted number of bytes.
-
2. strncpy
- 2-strncpy.c: C function that copies a string, including the
terminating null byte, using at most an inputted number of bytes.
- If the length of the source string is less than the maximum byte number, the remainder of the destination string is filled with null bytes.
- Works identically to the standard library function
strncpy
.
- 2-strncpy.c: C function that copies a string, including the
terminating null byte, using at most an inputted number of bytes.
-
3. strcmp
- 3-strcmp.c: C function that compares two strings.
- Returns the difference in bytes at point of difference.
- Works identically to the standard library function
strcmp
.
- 3-strcmp.c: C function that compares two strings.
-
4. I am a kind of paranoid in reverse. I suspect people of plotting to make me happy
- 4-rev_array.c: C function that reverses the content of an array of integers.
-
5. Always look up
- 5-string_toupper.c: C function that changes all lowercase letters of a string to uppercase.
-
6. Expect the best. Prepare for the worst. Capitalize on what comes
- 6-cap_string.c: C function that capitalizes all words of a string.
-
7. Mozart composed his music not for the elite, but for everybody
- 7-leet.c: C function that encodes a string into 1337, without
switch
or ternary operations and using only oneif
and two loops.- Letters
a
andA
are replaced by4
. - Leters
e
andE
are replaced by3
. - Letters
o
andO
are replaced by0
. - Letters
t
andT
are replaced by7
. - Letters
l
andL
are replaced by1
.
- Letters
- 7-leet.c: C function that encodes a string into 1337, without
-
0. memset
- 0-memset.c: C function that fills the first
n
bytes of memory area pointed to bys
with the constant byteb
.- Returns a pointer to the filled memory area
s
.
- Returns a pointer to the filled memory area
- 0-memset.c: C function that fills the first
-
1. memcpy
- 1-memcpy.c: C function that copies
n
bytes from memory areasrc
to memory areadest
.- Returns a pointer to the memory area
dest
.
- Returns a pointer to the memory area
- 1-memcpy.c: C function that copies
-
2. strchr
- 2-strchr.c: C function that returns a pointer to the first occurence of
the character
c
in the strings
.- If the character is not found, the function returns
NULL
.
- If the character is not found, the function returns
- 2-strchr.c: C function that returns a pointer to the first occurence of
the character
-
3. strspn
- 3-strspn.c: C function that returns the number of bytes in the intitial
segment of memory area
s
which consist only of bytes from a substringaccept
.
- 3-strspn.c: C function that returns the number of bytes in the intitial
segment of memory area
-
4. strpbrk
- 4-strpbrk.c: C function that locates the first occurence in a
string
s
of any of the bytes in a stringaccept
.- Returns a pointer to the byte in
s
that matches one of the bytes inaccept
. - If no matching byte is found, the function returns
NULL
.
- Returns a pointer to the byte in
- 4-strpbrk.c: C function that locates the first occurence in a
string
-
5. strstr
- 5-strstr.c: C function that finds the first occurence of a
substring
needle
in a stringhaystack
.- The terminating null bytes (
\0
) are not compared. - Returns a pointer to the beginning of the located substring.
- If the substring is not found, the function returns
NULL
.
- The terminating null bytes (
- 5-strstr.c: C function that finds the first occurence of a
substring
-
6. Chess is mental torture
- 7-print_chessboard.c: C function that prints the chessboard.