Blame view

SD/TP1/chaine.c 640 Bytes
37a5f1f0   Vincent Benoist   tpSD jusqu'a comp...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <stdio.h>
  #include <ctype.h>
  #include <stdlib.h>
  #include "chaine.h"
  
  void init_chaine(struct chaine* A){
      
      A->i=0;
      A->n=4;
      A->s=(char*)malloc(A->n*sizeof(char));
  
  }
  
  void ajout_chaine(struct chaine* A,char c){
  
      if(A->i==A->n){
          A->n=A->n+4;
          A->s=realloc(A->s,A->n*sizeof(char));
          A->s[A->i]=c;
          A->i=A->i+1;
          
      }else{
          A->s[A->i]=c;
          A->i=A->i+1;
      }
  }
  
  
  void imprime_chaine(struct chaine A){
      int j;
      
      for (j=0;j<=A.i;j++){
          printf("%c",A.s[j]);
      }
      printf("\n");
      
      
  }
  
  void clear_chaine(struct chaine* A){
      free(A->s);
  }