Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     printf("function 1a\n");
  6.     function1a("abc", "aed");
  7.     printf("\nfunction 1b\n");
  8.     function1b("toot titi");
  9.     printf("\nfunction 1c\n");
  10.     function1c("tototiti", 'i');
  11.     printf("\nfunction replace\n");
  12.     replace("toto titi");
  13.     getchar();
  14.     return 0;
  15. }
  16.  
  17. int function1a(char* str1, char* str2)
  18. {
  19.     int i = 0;
  20.  
  21.     while (str1[i] == str2[i] && str1[i] != '\0')
  22.     {
  23.         //on avance tant que les caracteres sont identique ou que la fin de str1 n'est pas atteinte
  24.         i++;
  25.     }
  26.     if (str1[i] == str2[i])
  27.     {
  28.         printf("str1 = str2");
  29.         return (0);
  30.     }
  31.     else
  32.     {
  33.         printf("en position %d : str1[%d] = %c , str2[%d] = %c", i, i, str1[i], i, str2[i]);
  34.         return str1[i] - str2[i];
  35.     }
  36. }
  37.  
  38. int function1b(char* str)
  39. {
  40.     int i;
  41.     for (i = 0; str[i] != '\0'; ++i)
  42.     {
  43.         //on ne fait rien , i est incrémenté dans la boucle par ++i
  44.         ;
  45.     }
  46.     printf("Length of the string %s : %d", str, i);
  47.     return i;
  48. }
  49.  
  50. int function1c(char* str, char c)
  51. {
  52.     int i;
  53.     for (i = 0; str[i] != '\0'; i++)
  54.     {
  55.         if (str[i] == c)
  56.         {
  57.             printf("char %c found in string %s in position %d", c, str, i);
  58.             return &str[i];
  59.         }
  60.     }
  61.     printf("char %c not found in string %s", c, str);
  62.     return 0;
  63. }
  64.  
  65. int replace(char* str)
  66. {
  67.     int i, counter;
  68.     counter = 0;
  69.     char * output = (char*)malloc(strlen(str));
  70.     //on passe par une chaine temporaire car on ne peut pas modifier dirrectement str, cela lève une exception
  71.     for (int i = 0; i < strlen(output); i++)
  72.     {
  73.         output[i] = ' ';
  74.         //on vide cette chaine car le malloc la rempli de caracteres aleatoires
  75.     }
  76.     for (int i = 0; i < strlen(str); i++)
  77.     {
  78.         if (str[i] == ' ')
  79.         {
  80.             output[i] = '_';
  81.             counter++;
  82.         }
  83.         else
  84.         {
  85.             output[i] = str[i];
  86.         }
  87.     }
  88.     str = output;
  89.     //on remplace l'adresse de la chaine d'entree par la nouvelle chaine que l'on a creer
  90.     printf("function replaced %d space : %s \n", counter, str);
  91.     return counter;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement