Created
July 23, 2022 11:29
-
-
Save spidey/2b9909257ba20f01847ac85b1d749f54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <unistd.h> | |
| void ft_putchar(char c) | |
| { | |
| write(1, &c, 1); | |
| } | |
| int ft_strlen(char *s) | |
| { | |
| int i; | |
| i = 0; | |
| while(s[i] != '\0') | |
| { | |
| i++; | |
| } | |
| return i; | |
| } | |
| void ft_putstr(char *s) | |
| { | |
| int i; | |
| i = 0; | |
| while(s[i] != '\0') | |
| { | |
| write(1, &s[i], 1); | |
| i++; | |
| } | |
| } | |
| void ft_putnum(int n) | |
| { | |
| char digits[20]; | |
| int i; | |
| i = 0; | |
| while(n >= 10) | |
| { | |
| digits[i] = n % 10; | |
| i++; | |
| n /= 10; | |
| } | |
| digits[i] = n; | |
| i++; | |
| while(i > 0) | |
| { | |
| char c; | |
| i--; | |
| c = '0' + digits[i]; | |
| write(1, &c, 1); | |
| } | |
| } | |
| void ft_swap(int *a, int *b) | |
| { | |
| int temp = *a; | |
| *a = *b; | |
| *b = temp; | |
| } | |
| int str_index_of(char *s, char c) | |
| { | |
| int i; | |
| i = 0; | |
| while (s[i] != '\0') | |
| { | |
| if (s[i] == c) | |
| { | |
| return i; | |
| } | |
| i++; | |
| } | |
| return -1; | |
| } | |
| void ft_rev_int_tab(int *tab, int size) | |
| { | |
| int i; | |
| i = 0; | |
| while(i != size/2) | |
| { | |
| ft_swap(&tab[i], &tab[size - 1 - i]); | |
| i++; | |
| } | |
| } | |
| void ft_print_tab(int *tab, int size) | |
| { | |
| int i; | |
| i = 0; | |
| while(i < size) | |
| { | |
| ft_putnum(tab[i]); | |
| ft_putchar(' '); | |
| i++; | |
| } | |
| ft_putchar('\n'); | |
| } | |
| void ft_sort_int_tab(int *tab, int size) | |
| { | |
| int n; | |
| n = 0; | |
| //printf("Sorting the array\n"); | |
| while(n < size - 1) | |
| { | |
| int i; | |
| int min; | |
| min = n; | |
| i = n + 1; | |
| while(i < size) | |
| { | |
| if (tab[i] < tab[min]) | |
| { | |
| min = i; | |
| } | |
| i++; | |
| } | |
| if(n != min) | |
| { | |
| ft_swap(&tab[n], &tab[min]); | |
| } | |
| n++; | |
| //ft_print_tab(tab, size); | |
| } | |
| } | |
| int main(void) | |
| { | |
| char *s; | |
| char c; | |
| int i; | |
| int v[10] = { 5, 3, 7, 8, 4, 2, 1, 0, 9, 6 }; | |
| s = "Help me!"; | |
| c = 'p'; | |
| i = str_index_of(s, c); | |
| ft_putstr("The index of letter "); | |
| ft_putchar(c); | |
| ft_putstr(" in the string "); | |
| ft_putstr(s); | |
| ft_putstr(" is "); | |
| ft_putnum(i); | |
| ft_putstr(".\n"); | |
| printf("The index of letter %c in the string %s is %d.\n", c, s, i); | |
| ft_putchar('\n'); | |
| printf("Table before reversing:\n"); | |
| ft_print_tab(v, 10); | |
| ft_rev_int_tab(v, 10); | |
| printf("Table after reversing:\n"); | |
| ft_print_tab(v, 10); | |
| ft_sort_int_tab(v, 10); | |
| printf("Sorted array\n"); | |
| ft_print_tab(v, 10); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment