Programmation C
Tableaux et chaînes de caractères - Correction des exercices
Cours Exercices Correction des exercices

Exercice 1 Exercice 2 Exercice 3 Exercice 4
Exercice 5 Exercice 6 Exercice 7 Exercice 8

Exercice 1

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

int main(void) {
  int t[50000];
  printf("%p\n", t);
  printf("%p\n", &t[0]);
  printf("%p\n", &t[1]);
  printf("%p\n", &t[2]);
  printf("%zu octets\n", sizeof(t));
  for (int i = 0; i < 50000; i++) {
    t[i] = rand() % 101;
  }
  double somme = 0.0;
  for (int i = 0; i < 50000; i++) {
    somme += t[i];
  }
  double moyenne = somme / 50000;
  printf("Somme    : %lf\n", somme);
  printf("Moyenne  : %lf\n", moyenne);
  return 0;
}

05-Exercice1.c - 05-Exercice1.exe
***
  • RAS

Exercice 2

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

#define TAILLE 2000000

double t[TAILLE];

int main(void) {
  for (int i = 0; i < TAILLE; i++) {
    t[i] = (double) rand() / (RAND_MAX + 1) * 20.0;
  }
  int effectifs[20];
  for (int i = 0; i < 20; i++) {
    effectifs[i] = 0;
  }
  for (int i = 0; i < TAILLE; i++) {
    effectifs[(int) t[i]]++;
  }
  for (int i = 0; i < 20; i++) {
    printf("%2d : %6d\n", i, effectifs[i]);
  }
  return 0;
}

05-Exercice2.c - 05-Exercice2.exe
***
  • RAS

Exercice 3

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

#define NL 9
#define NC 17

int main(void) {
  srand(3);
  long t[NL][NC];
  printf("%zu octets\n", sizeof(t));
  printf("\n");
  for (int l = 0; l < NL; l++) {
    for (int c = 0; c < NC; c++) {
      t[l][c] = rand() % 1000;
    }
  }
  for (int l = 0; l < NL; l++) {
    for (int c = 0; c < NC; c++) {
      printf("%4ld", t[l][c]);
    }
    printf("\n");
  }
  printf("\n");
  for (int l = 0; l < NL; l++) {
    long somme = 0;
    for (int c = 0; c < NC; c++) {
      somme += t[l][c];
    }
    printf("%4d : %7ld\n", l, somme);
  }
  printf("\n");
  int l = 0;
  int c = 0;
  bool zeroTrouve = false;
  do {
    if (t[l][c] == 0) {
      zeroTrouve = true;
    }
    else {
      c++;
      if (c == NC) {
        c = 0;
        l++;
      }
    }
  } while ((l < NL) && (!zeroTrouve));
  if (zeroTrouve) {
    printf("Il y a un zero en position %d %d\n", l, c);
  }
  else {
    printf("Il n'y a pas de zero\n");
  }
  return 0;
}

05-Exercice3.c - 05-Exercice3.exe
***
  • RAS

Exercice 4

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

#define TAILLE 70

void initialiserAvecMajuscules(char* t, size_t taille) {
  for (int i = 0; i < taille; i++) {
    t[i] = (char) ('A' + rand() % 26);
  }
}

void afficherTableauChar(char* t, size_t taille) {
  for (int i = 0; i < taille; i++) {
    printf("%c", t[i]);
  }
  printf("\n");
}

bool contenirToutesLesMajuscules(char* t, size_t taille) {
  bool trouve[26];
  for (int i = 0; i < 26; i++) {
    trouve[i] = false;
  }
  bool toutesMajusculesTrouvees = false;
  int i = 0;
  int cpt = 0;
  do {
    int indice = t[i] - 'A';
    if (!trouve[indice]) {
      trouve[indice] = true;
      cpt++;
      if (cpt == 26) {
        toutesMajusculesTrouvees = true;
      }
    }
    i++;
  } while ((i < taille) && (!toutesMajusculesTrouvees));
  return toutesMajusculesTrouvees;
}

int main(void) {
  srand(0);
  char t[500];
  initialiserAvecMajuscules(t, TAILLE);
  afficherTableauChar(t, TAILLE);
  printf("%d %d\n", 50, contenirToutesLesMajuscules(t, 50));
  printf("%d %d\n", TAILLE, contenirToutesLesMajuscules(t, TAILLE));
  return 0;
}

05-Exercice4.c - 05-Exercice4.exe
***
  • RAS

Exercice 5

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

#define TAILLE 20

int myRand(void) {
  return rand() * (RAND_MAX + 1) + rand();
}

void initialiserAvec0x00(unsigned char* t, size_t n) {
  for (size_t i = 0; i < n * n * n; i++) {
    t[i] = 0x00;
  }
}

void initialiserAvec0x00et0xFF(unsigned char* t, size_t n, int m) {
  initialiserAvec0x00(t, n);
  for (int i = 0; i < m; i++) {
    int i1;
    int i2;
    int i3;
    unsigned char* uc;
    do {
      i1 = myRand() % n;
      i2 = myRand() % n;
      i3 = myRand() % n;
      uc = &t[i1 * n * n + i2 * n + i3];
    } while (*uc == 0xFF);
    *uc = 0xFF;
  }
}

void compter0x00et0xFF(unsigned char* t, size_t n, size_t m, size_t p, int *nb0x00,int *nb0xFF) {
  unsigned char* ptr = t;
  *nb0x00 = 0;
  *nb0xFF = 0;
  for (size_t i = 0; i < n * m * p; i++) {
    switch (*ptr) {
      case 0x00: {
        (*nb0x00)++;
        break;
      }
      case 0xFF: {
        (*nb0xFF)++;
        break;
      }
    }
    ptr++;
  }
}

int main(void) {
  srand(0);
  unsigned char t1[TAILLE][TAILLE][TAILLE];
  initialiserAvec0x00((unsigned char*) t1, TAILLE);
  unsigned char t2[TAILLE][TAILLE][TAILLE];
  int m = 4000;
  initialiserAvec0x00et0xFF((unsigned char*) t2, TAILLE,m);
  int nb0x00;
  int nb0xFF;
  compter0x00et0xFF((unsigned char*) t1, TAILLE, TAILLE, TAILLE, &nb0x00, &nb0xFF);
  printf("%6d 0x00 et %6d 0xFF\n", nb0x00, nb0xFF);
  compter0x00et0xFF((unsigned char*) t2, TAILLE, TAILLE, TAILLE, &nb0x00, &nb0xFF);
  printf("%6d 0x00 et %6d 0xFF\n", nb0x00, nb0xFF);
  return 0;
}

05-Exercice5.c - 05-Exercice5.exe
***
  • RAS

Exercice 6

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

int myRand(void) {
  return rand() * (RAND_MAX + 1) + rand();
}

int randDansIntervalle(int bi, int bf) {
  return bi + myRand() % (bf - bi + 1);
}

void afficherTableauChar(char* t, int n) {
  for (int i = 0; i < n; i++) {
    printf("%c", t[i]);
  }
  printf("\n");
}

void creerAlphabetTrie(char* t) {
  for (int i = 0; i < 26; i++) {
    t[i] = (char) ('a' + i);
  }
}

void creerAlphabetMelange(char* t) {
  creerAlphabetTrie(t);
  for (int i = 0; i < 25; i++) {
    int ind = randDansIntervalle(i, 25);
    char aux = t[i];
    t[i] = t[ind];
    t[ind] = aux;
  }
}

int main(void) {
  char alphabet1[26];
  creerAlphabetTrie(alphabet1);
  afficherTableauChar(alphabet1, 26);
  char alphabet2[26];
  creerAlphabetMelange(alphabet2);
  afficherTableauChar(alphabet2, 26);
  return 0;
}

05-Exercice6.c - 05-Exercice6.exe
***
  • RAS

Exercice 7

#include <stdio.h>

int calculerLongueur(char* src) {
  int cpt = 0;
  char* s = src;
  while (*s != 0x00) {
    cpt++;
    s++;
  }
  return cpt;
}

void copier(char* src, char* dst) {
  char* dt = dst;
  char* s = src;
  while (*s != 0x00) {
    *dt = *s;
    s++;
    dt++;
  }
  *dt = 0x00;
}

void concatener(char* src1, char* src2, char* dst) {
  char* dt = dst;
  char* s = src1;
  while (*s != 0x00) {
    *dt = *s;
    s++;
    dt++;
  }
  s = src2;
  while (*s != 0x00) {
    *dt = *s;
    s++;
    dt++;
  }
  *dt = 0x00;
}

void inserer(char* src, int p, char* dst) {
  int ldst = calculerLongueur(dst);
  if ((p < 0) || (p > ldst)) {
    return;
  }
  int lsrc = calculerLongueur(src);
  char* d1 = &dst[ldst + lsrc];
  char* d2 = &dst[ldst];
  printf("%d %d %d\n", ldst, lsrc, ldst - p + 1);
  for (int i = 0; i < ldst - p + 1; i++) {
    *d1 = *d2;
    d1--;
    d2--;
  }
  d2 = &src[lsrc - 1];
  for (int i = 0; i < lsrc; i++) {
    *d1 = *d2;
    d1--;
    d2--;
  }
}

void supprimer(char c, char* src) {
  char* d = src;
  char* s = src;
  while (*s != 0x00) {
    if (*s != c) {
      *d = *s;
      d++;
    }
    s++;
  }
  *d = 0x00;
}

void tester1(void) {
  char str[50];
  char* s1 = "abcdef";
  char* s2 = "xyz";
  concatener(s1, s2, str);
  printf("\"%s\" + \"%s\" : \"%s\"\n", s1, s2, str);
  printf("\n");
}

void tester2(void) {
  char str[50];
  char* s1 = "0123456789";
  char* s2 = "xyz";
  int pos = 5;
  copier(s1, str);
  inserer(s2, pos, str);
  printf("\"%s\", %d, \"%s\" : \"%s\"\n", s1, pos, s2, str);
  printf("\n");
}

void tester3(void) {
  char str[50];
  char* s = "xyxzxxxyyyzxxzzzyxzyxzyxxx";
  char car = 'x';
  copier(s, str);
  printf("\"%s\", ", str);
  supprimer(car, str);
  printf(" %c : \"%s\"\n", car, str);
  car = 'y';
  printf("\"%s\", ", str);
  supprimer(car, str);
  printf(" %c : \"%s\"\n", car, str);
  car = 'z';
  printf("\"%s\", ", str);
  supprimer(car, str);
  printf(" %c : \"%s\"\n", car, str);
  printf("\n");
}

int main(void) {
  tester1();
  tester2();
  tester3();
  return 0;
}

05-Exercice7.c - 05-Exercice7.exe
***
  • RAS

Exercice 8

#include <stdio.h>

void chiffrer(char* src, char* clef, char* dst) {
  char* d = dst;
  char* s = src;
  while (*s != 0x00) {
    *d = clef[*s];
    s++;
    d++;
  }
  *d = 0x00;
}

int main(void) {
  char clef[256] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                      'E',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                      'M', 'N', '8', 'F', 'X', 'S', 'T', 'Y', 'Z', 'U',0x00,0x00,0x00,0x00,0x00,0x00,
                     0x00, 'C', 'D', 'A', 'B', ' ', 'J', 'K', 'L', '4', 'H', 'I', '3', '2', '7', 'G',
                      'P', '6', 'O', 'Q', 'R', '5', '9', '1', '0', 'V', 'W' };
  char messageChiffre[1000];
  char* message1 = "0123456789";
  chiffrer(message1, clef, messageChiffre);
  printf("%s : %s\n", message1, messageChiffre);
  char* message2 = "BERLIN VIENNE BRUXELLES SOFIA NICOSIE ZAGREB COPENHAGUE MADRID TALLINN HELSINKI \
PARIS ATHENES BUDAPEST DUBLIN ROME RIGA VILNIUS LUXEMBOURG LA VALLETTE AMSTERDAM VARSOVIE LISBONNE \
PRAGUE BUCAREST BRATISLAVA LJUBLJANA STOCKHOLM";
  chiffrer(message2, clef, messageChiffre);
  printf("%s :\n%s\n", message2, messageChiffre);
  return 0;
}

05-Exercice8.c - 05-Exercice8.exe
***
  • RAS