cours_progra/bac2/os/chap2/ex2.c
Debucquoy b0f02b0d5d
.
2023-10-18 20:27:40 +02:00

35 lines
770 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* expand_tabs(const char* string);
int main(void)
{
const char* str = "This is a text This is another one. ' ' <- space; ' ' <-tab";
printf("%s - %d\n", str, (int)strlen(str));
printf("%s - %d\n", expand_tabs(str),(int) strlen(expand_tabs(str)));
}
char* expand_tabs(const char* string){
int tab_count = 0, letter_count = 0;
for(int i = 0; string[i] != '\0'; i++){
letter_count++;
if(string[i] == '\t')
tab_count++;
}
char* ret = malloc(sizeof(char) * letter_count + 3*tab_count);
char* filler = ret;
for(int i = 0; string[i] != '\0'; i++){
if(string[i] == '\t'){
for (int j = 0; j < 4; ++j) {
*(filler++) = ' ';
}
}else{
*(filler++) = string[i];
}
}
return ret;
}