
Untitled
By: a guest on
May 6th, 2012 | syntax:
C | size: 3.32 KB | hits: 21 | expires: Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void entab(char *);
void detab(char *);
void tab(char *);
int main(){
char buff[] = "l o l";
detab(buff);
tab(buff);
printf("%s\n", buff);
return 0;
}
void entab (char * s) {
int i = 0;
char * buff = (char *)malloc (strlen (s)+1) ;
char * d = buff ;
char * scopy = s;
while (*s != '\0'){
if(*s != ' '){
*d++ = *s++ ;
}else{
i++;
s++;
if(*s != ' '){
while(i > 0){
if(i < 4){
while(i > 0){
*d++ = ' ';
i--;
}
}else{
*d++ = '\t';
i -= 4;
}
}
}
}
}
*d = '\0' ;
strcpy(scopy, buff);
free(buff);
}
void detab(char *s){
int i = 0;
char * buff = (char *)malloc (strlen (s)+1) ;
char * scopy = s;
char * buffc = buff;
while(*s != '\0'){
if(*s == '\t'){
while(i < 4){
*buff++ = ' ';
i++;
}
s++; //Jei s++; ikisi i while - nemeta situ error.
i = 0;
}else{
*buff++ = *s++;
}
}
strcpy(scopy, buffc);
free(buff);
}
void tab(char *s){
while(*s != '\0'){
if(*s == '\t'){
printf("TAB\n");
}else{
printf("NO TAB\n");
}
s++;
}
}
/*Output:
arnas@arnas:~/Desktop/Programming$ gcc -Wall tab.c -o tab
arnas@arnas:~/Desktop/Programming$ ./tab
*** glibc detected *** ./tab: free(): invalid pointer: 0x0815d012 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x73e42)[0xb75eae42]
./tab[0x80486b7]
./tab[0x80484f5]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75904d3]
./tab[0x8048431]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 11403295 /home/arnas/Desktop/Programming/tab
08049000-0804a000 r--p 00000000 08:01 11403295 /home/arnas/Desktop/Programming/tab
0804a000-0804b000 rw-p 00001000 08:01 11403295 /home/arnas/Desktop/Programming/tab
0815d000-0817e000 rw-p 00000000 00:00 0 [heap]
b7542000-b755e000 r-xp 00000000 08:01 11407213 /lib/i386-linux-gnu/libgcc_s.so.1
b755e000-b755f000 r--p 0001b000 08:01 11407213 /lib/i386-linux-gnu/libgcc_s.so.1
b755f000-b7560000 rw-p 0001c000 08:01 11407213 /lib/i386-linux-gnu/libgcc_s.so.1
b7576000-b7577000 rw-p 00000000 00:00 0
b7577000-b7716000 r-xp 00000000 08:01 11407774 /lib/i386-linux-gnu/libc-2.15.so
b7716000-b7718000 r--p 0019f000 08:01 11407774 /lib/i386-linux-gnu/libc-2.15.so
b7718000-b7719000 rw-p 001a1000 08:01 11407774 /lib/i386-linux-gnu/libc-2.15.so
b7719000-b771c000 rw-p 00000000 00:00 0
b7731000-b7734000 rw-p 00000000 00:00 0
b7734000-b7735000 r-xp 00000000 00:00 0 [vdso]
b7735000-b7755000 r-xp 00000000 08:01 11403324 /lib/i386-linux-gnu/ld-2.15.so
b7755000-b7756000 r--p 0001f000 08:01 11403324 /lib/i386-linux-gnu/ld-2.15.so
b7756000-b7757000 rw-p 00020000 08:01 11403324 /lib/i386-linux-gnu/ld-2.15.so
bfdd5000-bfdf6000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)*/