.
This commit is contained in:
34
bac2/os/chap2/ex2.c
Normal file
34
bac2/os/chap2/ex2.c
Normal file
@ -0,0 +1,34 @@
|
||||
#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;
|
||||
}
|
Reference in New Issue
Block a user