APL/APL1.2/TP07/chaines.c

59 lines
967 B
C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct char_ {
char letter;
struct char_* next;
};
typedef struct char_ string_char;
typedef string_char* string;
void printString(string str) {
while (str != NULL) {
printf("%c", str->letter);
str = str->next;
}
printf("\n");
}
string toString(char* str) {
string_char* last_c = NULL;
for (int i = strlen(str)+1; i >= 0; i--) {
string_char* c = malloc(sizeof(string_char));
c->letter = str[i];
c->next = last_c;
last_c = c;
}
return last_c;
}
char* toCharT(string str) {
int count = 0;
string str2 = str;
while (str2 != NULL) {
count++;
str2 = str2->next;
}
char* new_str = calloc(sizeof(char), count);
for (int i = 0; i < count; i++) {
new_str[i] = str->letter;
str = str->next;
}
return new_str;
}
int main(int argc, char * argv[]) {
string c = toString("Hello World !");
printString(c);
char* c2 = toCharT(c);
printf("%s\n", c2);
return EXIT_SUCCESS;
}