Guest User

Untitled

a guest
Apr 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. /* Let's assume we got a C99-compliant compiler */
  5. #include <stdbool.h>
  6.  
  7. #define MAXLENGH 20000
  8.  
  9. bool caractere_de_debut(char c) {
  10.   return (c == '\t') || (c == ' ');
  11. }
  12.  
  13. int accolades(char chaine[]) {
  14.   int cpt = 0;
  15.   int i;
  16.  
  17.   for(i=0; chaine[i] != '\0'; i++)
  18.     if(chaine[i] == '{')
  19.       cpt++;
  20.     else if(chaine[i] == '}')
  21.       cpt--;
  22.  
  23.   return cpt;
  24. }
  25.  
  26. void enleverpremiersespaces(char chaine[]) {
  27.   int i = 0;
  28.   int j;
  29.   int lengh = strlen(chaine) + 1;
  30.  
  31.   while(caractere_de_debut(chaine[i]))
  32.     i++;
  33.  
  34.   for(j=i; j <= lengh; j++)
  35.       chaine[j-i] = chaine[j];
  36. }
  37.  
  38. void ecritpremiersespaces(char chaine[], int m) {
  39.   char chaine2[MAXLENGH];
  40.   int i;
  41.   int max = strlen(chaine);
  42.  
  43.   enleverpremiersespaces(chaine);
  44.   strcpy(chaine2, chaine);
  45.  
  46.   if(MAXLENGH>=(max+m)) {
  47.     for(i=0; i<m; i++)
  48.       chaine[i] = ' ';
  49.     chaine[m] = '\0';
  50.     strcat(chaine,chaine2);
  51.   }
  52. }
  53.  
  54. int main() {
  55.   int nbraclds = 0;
  56.   char chaine[MAXLENGH];
  57.  
  58.   while(fgets(chaine, MAXLENGH, stdin) != NULL) {
  59.       if(accolades(chaine) > 0) {
  60.     ecritpremiersespaces(chaine, (nbraclds*4));
  61.     nbraclds += accolades(chaine);
  62.       } else {
  63.     nbraclds += accolades(chaine);
  64.     ecritpremiersespaces(chaine, (nbraclds*4));
  65.       }
  66.     printf("%s", chaine);
  67.   }  
  68.  
  69.   printf("\n");
  70. }
Add Comment
Please, Sign In to add comment