Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- printf("function 1a\n");
- function1a("abc", "aed");
- printf("\nfunction 1b\n");
- function1b("toot titi");
- printf("\nfunction 1c\n");
- function1c("tototiti", 'i');
- printf("\nfunction replace\n");
- replace("toto titi");
- getchar();
- return 0;
- }
- int function1a(char* str1, char* str2)
- {
- int i = 0;
- while (str1[i] == str2[i] && str1[i] != '\0')
- {
- //on avance tant que les caracteres sont identique ou que la fin de str1 n'est pas atteinte
- i++;
- }
- if (str1[i] == str2[i])
- {
- printf("str1 = str2");
- return (0);
- }
- else
- {
- printf("en position %d : str1[%d] = %c , str2[%d] = %c", i, i, str1[i], i, str2[i]);
- return str1[i] - str2[i];
- }
- }
- int function1b(char* str)
- {
- int i;
- for (i = 0; str[i] != '\0'; ++i)
- {
- //on ne fait rien , i est incrémenté dans la boucle par ++i
- ;
- }
- printf("Length of the string %s : %d", str, i);
- return i;
- }
- int function1c(char* str, char c)
- {
- int i;
- for (i = 0; str[i] != '\0'; i++)
- {
- if (str[i] == c)
- {
- printf("char %c found in string %s in position %d", c, str, i);
- return &str[i];
- }
- }
- printf("char %c not found in string %s", c, str);
- return 0;
- }
- int replace(char* str)
- {
- int i, counter;
- counter = 0;
- char * output = (char*)malloc(strlen(str));
- //on passe par une chaine temporaire car on ne peut pas modifier dirrectement str, cela lève une exception
- for (int i = 0; i < strlen(output); i++)
- {
- output[i] = ' ';
- //on vide cette chaine car le malloc la rempli de caracteres aleatoires
- }
- for (int i = 0; i < strlen(str); i++)
- {
- if (str[i] == ' ')
- {
- output[i] = '_';
- counter++;
- }
- else
- {
- output[i] = str[i];
- }
- }
- str = output;
- //on remplace l'adresse de la chaine d'entree par la nouvelle chaine que l'on a creer
- printf("function replaced %d space : %s \n", counter, str);
- return counter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement