#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Exercice 1

void echange(int *x, int *y)
{
  int z = *x;
  *x = *y;
  *y = z;
}

int main1()
{
  int a = 4;
  int b = 6;
  echange(&a, &b);
  printf("Après l'appel à echange, a vaut %d et b vaut %d.\n", a, b);
  return 0;
}

// Exercice 2

void echange_tab(int t[])
{
  int z = t[0];
  t[0] = t[1];
  t[1] = z;
}

int main2()
{
  int tab[3] = { 13 , 9 , 19 };
  echange_tab(tab);
  printf("Les trois éléments de tab sont désormais %d, %d et %d.\n", tab[0], tab[1], tab[2]);
  return 0;
}

// Exercice 3

void ejecte(int t[], int n)
{
  int premier = t[0];
  for (int i = 0 ; i < n-1 ; i += 1) t[i] = t[i+1];
  t[n-1] = premier;
}

int main3()
{
  int tab[3] = { 13 , 9 , 19 };
  ejecte(tab, 3);
  printf("Les trois éléments de tab sont désormais %d, %d et %d.\n", tab[0], tab[1], tab[2]);
  return 0;
}

// Exercice 4

int trouve(int t[], int n, int x)
{
  for (int i = 0 ; i < n ; i += 1)
  {
    if (t[i] == x) return i;
  }
  return -1;
}

int main4()
{
  int tab[3] = { 13 , 9 , 19 };
  int i1 = trouve(tab, 3, 19);
  int i2 = trouve(tab, 3, 22);
  printf("Position de 19 dans tab : %d, position de 22 dans tab : %d.\n", i1, i2);
  return 0;
}

// Exercice 5

int nombre_occurrences(int t[], int n, int x)
{
  int rep = 0;
  for (int i = 0 ; i < n ; i += 1)
  {
    if (t[i] == x) rep += 1;
  }
  return rep;
}

int main5()
{
  int tab[10] = { 0 , 1 , 0 , 2 , 0 , 1 , 0 , 3 , 0 , 1 };
  int n1 = nombre_occurrences(tab, 10, 0);
  int n2 = nombre_occurrences(tab, 10, 1);
  printf("Nombre de 0 dans tab : %d, nombre de 1 dans tab : %d.\n", n1, n2);
  return 0;
}

// Exercice 6

void bubble(int t[], int n)
{
  int buff;
  for (int i = 0 ; i < n-1 ; i += 1)
  {
    for (int j = 0 ; j < n-1-i ; j += 1)
    {
      if (t[j] > t[j+1])
      {
        buff = t[j];
        t[j] = t[j+1];
        t[j+1] = buff;
      }
    }
  }
}

int main6()
{
  int tab[10] = { 0 , 1 , 0 , 2 , 0 , 1 , 0 , 3 , 0 , 1 };
  bubble(tab, 10);
  printf("Nouvelle version de tab :\n");
  for (int i = 0 ; i < 10 ; i += 1)
  {
    printf("%d ", tab[i]);
  }
  printf("\n");
  return 0;
}

// Exercice 7

void posmax(int **tab, int lignes, int colonnes, int rep[2])
{
  rep[0] = 0;
  rep[1] = 0;
  for (int i = 0 ; i < lignes ; i += 1)
  {
    for (int j = 0 ; j < colonnes ; j += 1)
    {
      if (tab[i][j] > tab[rep[0]][rep[1]])
      {
        rep[0] = i;
        rep[1] = j;
      }
    }
  }
}

int main7()
{
  int tab[3][3] = { { 4 , 3 , 8 } , { 9 , 5 , 1 } , { 2 , 7 , 6 } };
  int *tab_pointe[3] = {tab[0], tab[1], tab[2]};
  int rep[2];
  posmax(tab_pointe, 3, 3, rep);
  printf("Position du maximum de tab : (%d, %d).\n", rep[0], rep[1]);
  return 0;
}

// Exercice 8

unsigned long int taillechaine(char str[])
{
  unsigned long int reponse = 0;
  while (str[reponse] != '\0') reponse += 1;
  return reponse;
}

int main8()
{
  char chaine[32] = "Bonjour le monde !";
  printf("Pour vérifier, strlen(chaine) donne %lu.\n", strlen(chaine));
  printf("Et la fonction taillechaine donne %lu.\n", taillechaine(chaine));
  return 0;
}

// Exercice 9

bool palindrome(char str[])
{
  unsigned long int n = strlen(str) - 1;
  unsigned long int i = 0;
  while (i <= n)
  {
    if (str[i] != str[n]) return false;
    i += 1;
    n -= 1;
  }
  return true;
}

int main9()
{
  char chaine[32] = "eluparcettecrapule";
  if (palindrome(chaine))
  {
    printf("La chaîne %s est bien un palindrome.\n", chaine);
  }
  else
  {
    printf("Ce n'est pas normal, %s est pourtant un palindrome.\n", chaine);
  }
  return 0;
}

// Exercice 10

void recopie_maj(char chaine[], char chaine_maj[])
{
  char c;
  int n = strlen(chaine);
  for (unsigned long int i = 0 ; i <= n ; i += 1) // <= : astuce pour incorporer le \0
  {
    c = chaine[i];
    if ('a' <= c && c <= 'z') c -= 32;
    else if ('A' <= c && c <= 'Z') c += 32;
    chaine_maj[i] = c;
  }
}

int main10()
{
  char chaine[32] = "Bonjour le monde !";
  char chaine_maj[32];
  recopie_maj(chaine, chaine_maj);
  printf("Après transformation, chaine_maj est %s", chaine_maj);
  return 0;
}

// Fonction main

int main()
{
  int zero = main1();
  exit(zero);
}
