eg
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
char *join_strings(char *strings[], int count){
|
||||
|
||||
if (strings == NULL) return NULL;
|
||||
|
||||
int tot = 0;
|
||||
int index = 0;
|
||||
//connaitre taille des str dans strings
|
||||
for (int i = 0; i < count; i++){
|
||||
|
||||
index = 0;
|
||||
//binf
|
||||
while (strings[i][index]){
|
||||
tot ++;
|
||||
index ++;
|
||||
//printf("boucle inf ici");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
char *rslt = malloc((tot + count) * sizeof(char));
|
||||
if (rslt == NULL) return NULL;
|
||||
|
||||
tot = 0;
|
||||
|
||||
for (int i = 0; i < count; i ++){
|
||||
index = 0;
|
||||
//binf
|
||||
while (strings[i][index]){
|
||||
|
||||
*(rslt + tot) = strings[i][index];
|
||||
index ++;
|
||||
tot ++;
|
||||
}
|
||||
if (i == count - 1)
|
||||
*(rslt + tot) = '\0';
|
||||
else *(rslt + tot) = ' ';
|
||||
tot ++;
|
||||
}
|
||||
//free(strings);
|
||||
return rslt;
|
||||
}
|
||||
/*
|
||||
int main(){
|
||||
|
||||
char *str[] = {"Hello", "from ", "Java", "swimming pool"};
|
||||
char *test = join_strings(str, 4);
|
||||
printf("%s\n", test);
|
||||
free (test);
|
||||
|
||||
test = join_strings(NULL, 0);
|
||||
printf("%s\n", test);
|
||||
free(test);
|
||||
|
||||
}*/
|
||||
|
||||
Reference in New Issue
Block a user