Skip to content

Files

Latest commit

d32ec91 · Mar 27, 2023

History

History

pointers_arrays_strings

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Mar 27, 2023
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Feb 3, 2023
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Oct 21, 2022
Feb 5, 2023
Mar 27, 2023
Feb 3, 2023

C - Pointers, arrays and strings 💀

Description 😶‍🌫️

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 ✔️

  • tests: Folder of test files. Provided by Holberton School.

Helper File 🙌

  • _putchar.c: C function that writes a character to stdout.

Header File 📁

  • 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]);

Formatting and Examples 🤓

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:~/$ 

Tasks 📃

  • 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 to 98.
  • 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.
  • 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

  • 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.
  • 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 by dest.
      • Returns the pointer to dest.
  • 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.

  • 0. strcat

    • 0-strcat.c: C function that concatenates two strings.
      • Adds a terminating null byte at end.
  • 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.
  • 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.
  • 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.
  • 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

  • 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 one if and two loops.
      • Letters a and A are replaced by 4.
      • Leters e and E are replaced by 3.
      • Letters o and O are replaced by 0.
      • Letters t and T are replaced by 7.
      • Letters l and L are replaced by 1.

  • 0. memset

    • 0-memset.c: C function that fills the first n bytes of memory area pointed to by s with the constant byte b.
      • Returns a pointer to the filled memory area s.
  • 1. memcpy

    • 1-memcpy.c: C function that copies n bytes from memory area src to memory area dest.
      • Returns a pointer to the memory area dest.
  • 2. strchr

    • 2-strchr.c: C function that returns a pointer to the first occurence of the character c in the string s.
      • If the character is not found, the function returns NULL.
  • 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 substring accept.
  • 4. strpbrk

    • 4-strpbrk.c: C function that locates the first occurence in a string s of any of the bytes in a string accept.
      • Returns a pointer to the byte in s that matches one of the bytes in accept.
      • If no matching byte is found, the function returns NULL.
  • 5. strstr

    • 5-strstr.c: C function that finds the first occurence of a substring needle in a string haystack.
      • 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.
  • 6. Chess is mental torture