Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: C  |  size: 3.32 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void entab(char *);
  6. void detab(char *);
  7. void tab(char *);
  8.  
  9. int main(){
  10.     char buff[] = "l      o l";
  11.     detab(buff);
  12.     tab(buff);
  13.     printf("%s\n", buff);
  14.     return 0;
  15. }
  16.  
  17. void entab (char * s) {
  18.     int i = 0;
  19.     char * buff = (char *)malloc (strlen (s)+1) ;
  20.     char * d = buff ;
  21.     char * scopy = s;
  22.     while (*s != '\0'){
  23.         if(*s != ' '){
  24.             *d++ = *s++ ;
  25.         }else{
  26.             i++;
  27.             s++;
  28.  
  29.             if(*s != ' '){
  30.                 while(i > 0){
  31.                     if(i < 4){
  32.                         while(i > 0){
  33.                             *d++ = ' ';
  34.                             i--;
  35.                         }
  36.                     }else{
  37.                         *d++ = '\t';
  38.                         i -= 4;
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.     }
  44.     *d = '\0' ;
  45.     strcpy(scopy, buff);
  46.     free(buff);
  47. }
  48.  
  49. void detab(char *s){
  50.     int i = 0;
  51.     char * buff = (char *)malloc (strlen (s)+1) ;
  52.     char * scopy = s;
  53.     char * buffc = buff;
  54.  
  55.     while(*s != '\0'){
  56.         if(*s == '\t'){
  57.             while(i < 4){
  58.                 *buff++ = ' ';
  59.                 i++;
  60.             }
  61.             s++; //Jei s++; ikisi i while - nemeta situ error.
  62.             i = 0;
  63.         }else{
  64.             *buff++ = *s++;
  65.         }
  66.     }
  67.     strcpy(scopy, buffc);
  68.     free(buff);
  69. }
  70.  
  71. void tab(char *s){
  72.     while(*s != '\0'){
  73.         if(*s == '\t'){
  74.             printf("TAB\n");
  75.         }else{
  76.             printf("NO TAB\n");
  77.         }
  78.         s++;
  79.     }
  80. }
  81.  
  82. /*Output:
  83. arnas@arnas:~/Desktop/Programming$ gcc -Wall tab.c -o tab
  84. arnas@arnas:~/Desktop/Programming$ ./tab
  85. *** glibc detected *** ./tab: free(): invalid pointer: 0x0815d012 ***
  86. ======= Backtrace: =========
  87. /lib/i386-linux-gnu/libc.so.6(+0x73e42)[0xb75eae42]
  88. ./tab[0x80486b7]
  89. ./tab[0x80484f5]
  90. /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75904d3]
  91. ./tab[0x8048431]
  92. ======= Memory map: ========
  93. 08048000-08049000 r-xp 00000000 08:01 11403295   /home/arnas/Desktop/Programming/tab
  94. 08049000-0804a000 r--p 00000000 08:01 11403295   /home/arnas/Desktop/Programming/tab
  95. 0804a000-0804b000 rw-p 00001000 08:01 11403295   /home/arnas/Desktop/Programming/tab
  96. 0815d000-0817e000 rw-p 00000000 00:00 0          [heap]
  97. b7542000-b755e000 r-xp 00000000 08:01 11407213   /lib/i386-linux-gnu/libgcc_s.so.1
  98. b755e000-b755f000 r--p 0001b000 08:01 11407213   /lib/i386-linux-gnu/libgcc_s.so.1
  99. b755f000-b7560000 rw-p 0001c000 08:01 11407213   /lib/i386-linux-gnu/libgcc_s.so.1
  100. b7576000-b7577000 rw-p 00000000 00:00 0
  101. b7577000-b7716000 r-xp 00000000 08:01 11407774   /lib/i386-linux-gnu/libc-2.15.so
  102. b7716000-b7718000 r--p 0019f000 08:01 11407774   /lib/i386-linux-gnu/libc-2.15.so
  103. b7718000-b7719000 rw-p 001a1000 08:01 11407774   /lib/i386-linux-gnu/libc-2.15.so
  104. b7719000-b771c000 rw-p 00000000 00:00 0
  105. b7731000-b7734000 rw-p 00000000 00:00 0
  106. b7734000-b7735000 r-xp 00000000 00:00 0          [vdso]
  107. b7735000-b7755000 r-xp 00000000 08:01 11403324   /lib/i386-linux-gnu/ld-2.15.so
  108. b7755000-b7756000 r--p 0001f000 08:01 11403324   /lib/i386-linux-gnu/ld-2.15.so
  109. b7756000-b7757000 rw-p 00020000 08:01 11403324   /lib/i386-linux-gnu/ld-2.15.so
  110. bfdd5000-bfdf6000 rw-p 00000000 00:00 0          [stack]
  111. Aborted (core dumped)*/