Programmation bas niveau (BNV) Help

Correction : Exercices Bonus

Correction Exercice Bonus 1 :

Objectif : créer une fonction qui va échanger les contenus de deux variables en utilisant des pointeurs.

swap_ptr.c

void swap_ptr(int * ptr1, int *ptr2) { int tmp = *ptr1; *ptr1 = *ptr2; *ptr2 = tmp; }

swap_ptr.h

#ifndef __SWAP_PTR_H__ #define __SWAP_PTR_H__ void swap_ptr(int *ptr1, int *ptr2); #endif

main.c

#include <stdlib.h> #include <stdio.h> #include "swap_ptr.h" int main() { int val1 = 42; int val2 = 21; printf("val1 : %d, val2 : %d\n", val1, val2); swap_ptr(&val1, &val2); printf("val1 : %d, val2 : %d\n", val1, val2); exit(0); }

Makefile

# Makefile NAME = ex-1-bonus SRCS = main.c \ swap_ptr.c all : $(NAME) $(NAME): $(SRCS) gcc $(SRCS) -o $(NAME) fclean: rm -f $(NAME) re: fclean all

Correction Exercice Bonus 2 :

Objectif : créer une fonction qui compte le nombre de mots dans une chaine de caractères et le retourne. Compiler le programme et l'exécuter.

count_words.c

int count_words(char *str) { int nb_words = 0; int i = 0; int in_word = 0; while (str[i] != '\0') { if (str[i] == ' ' || str[i] == '\'') { in_word = 0; } else { if (in_word == 0) { nb_words++; in_word = 1; } } i++; } return (nb_words); }

count_words.h

#ifndef __COUNT_WORDS_H__ #define __COUNT_WORDS_H__ int count_words(char *str); #endif

main.c

#include <stdlib.h> #include <stdio.h> #include "count_words.h" int main() { int nb_words; nb_words = count_words("Hello World!"); printf("La phrase contient %d mots\n", nb_words); nb_words = count_words("La vie l'univers et tout le reste"); printf("La phrase contient %d mots\n", nb_words); exit(0); }

Makefile

# Makefile NAME = ex-2-bonus SRCS = main.c \ count_words.c all : $(NAME) $(NAME): $(SRCS) gcc $(SRCS) -o $(NAME) fclean: rm -f $(NAME) re: fclean all

Correction Exercice Bonus 3 :

Objectif : créer une fonction qui affiche les mots d'une chaine de caractères à l'envers. Compiler le programme et l'exécuter.

str_len.c

int str_len(char *str) { int i; int len; i = 0; len = 0; while(str[i] != '\0') { i++; len++; } return(len); }

str_len.h

#ifndef __STR_LEN_H__ #define __STR_LEN_H__ int str_len(char *str); #endif

revert_words.c

#include <stdio.h> #include "str_len.h" void revert_words(char *str) { int len = str_len(str); int i = len - 1; int is_first_word = 1; while(i >= 0) { while (i >= 0 && (str[i] == ' ' || str[i] == '\'')) { i--; } if (i < 0) { break; } int end = i; while (i >= 0 && str[i] != ' ' && str[i] != '\'') { i--; } int start = i + 1; if (!is_first_word) { printf(" "); } else { is_first_word = 0; } int j = start; while (j <= end) { printf("%c", str[j]); j++; } } printf("\n"); }

revert_words.h

#ifndef __REVERT_WORDS_H__ #define __REVERT_WORDS_H__ void revert_words(char *str); #endif

main.c

#include <stdlib.h> #include "revert_words.h" int main() { revert_words("Hello World!"); revert_words("La vie, l'univers et tout le reste"); exit(0); }

Makefile

# Makefile NAME = ex-3-bonus SRCS = main.c \ revert_words.c \ str_len.c all : $(NAME) $(NAME): $(SRCS) gcc $(SRCS) -o $(NAME) fclean: rm -f $(NAME) re: fclean all
14 October 2025