Monday, May 25, 2015

C : Program to display tabs , backspaces visible in an unambiguous way

In my earlier post, we have seen "C : Program to replace multiples spaces with a single space" , today lets modify the same code to display tabs, backspaces.

Below is the program which does this job.
 #include   
 int main()  
 {  
   char c;  
   printf("Enter a line to display tabs \n");  
   while((c=getchar()) != '\n')  
   {  
     if(c == '\t')  
     {  
         putchar('\\');  
         putchar('t');  
     }  
     putchar(c);  
   }  
   printf("\n");  
   return 0;  
 }  
In above program,
  • We read input character by character
  • Check if its tab (\t). If tab, we output (\\) followed by (t) character which displays \t.
  • If its not tab, we simply output the character.
Output of this program,
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 Enter a line to display tabs   
 This  program     displays     the tabs     entered  
 This \t     program\t     displays\t     the tabs\t     entered  

No comments:

Post a Comment