6 Replies - 289 Views - Last Post: 29 May 2012 - 07:42 PM Rate Topic: -----

#1 LogicError  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 05-November 11

Simple I/O Program To Turn Tabs Into \t

Posted 29 May 2012 - 07:05 PM

#include <stdio.h>
#include <stdlib.h>

// Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash \\. This makes tabs and backspaces bisible in an unambigious way.

int main()
{
    int c;
    
    while((c = getchar()) != EOF){
    if (c == '\t')
        printf("\\t");
    else if (c == '\b')
        printf("\\b");
    else if(c == '\\')
        printf("\\\\");
    else 
        putchar(c);
    }
}



It simply prints out whatever I type in. I don't understand why it isn't changing the tabs into \t...

Any idea why this wouldn't be working?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Simple I/O Program To Turn Tabs Into \t

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Simple I/O Program To Turn Tabs Into \t

Posted 29 May 2012 - 07:19 PM

The tab part should be working -- what output do you get if you just enter:
<tab><enter>
?

The backspace is a problem because when you type a backspace on the keyboard it doesn't put a backspace character in the stream, it just erases the previous character (if any).

As to the backslash, I would interpret the assignment differently -- simply replace each single \ with "\\".
Was This Post Helpful? 0
  • +
  • -

#3 LogicError  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 05-November 11

Re: Simple I/O Program To Turn Tabs Into \t

Posted 29 May 2012 - 07:24 PM

When I <tab> <enter>, it just skips a line, which I am assuming is just outputting the tab.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Simple I/O Program To Turn Tabs Into \t

Posted 29 May 2012 - 07:33 PM

Funny, when I run it it prints \t.

I'll try it on Windows 7.

The tab works correctly for me in Windows too. How are you running it?
Was This Post Helpful? 0
  • +
  • -

#5 LogicError  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 05-November 11

Re: Simple I/O Program To Turn Tabs Into \t

Posted 29 May 2012 - 07:35 PM

I am running it through CodeBlocks with the GNU GCC compiler. (Windows 7 64-bit)
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Simple I/O Program To Turn Tabs Into \t

Posted 29 May 2012 - 07:38 PM

Well it works for me with GCC in Linux and in Win7 64 bit so I suspect that you aren't actually running the code you posted. Try rebuilding it, or set up a new project and copy/paste that code into its main.c and then build it.
Was This Post Helpful? 1
  • +
  • -

#7 LogicError  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 05-November 11

Re: Simple I/O Program To Turn Tabs Into \t

Posted 29 May 2012 - 07:42 PM

View Postr.stiltskin, on 29 May 2012 - 10:38 PM, said:

Well it works for me with GCC in Linux and in Win7 64 bit so I suspect that you aren't actually running the code you posted. Try rebuilding it, or set up a new project and copy/paste that code into its main.c and then build it.


Rebuilding worked. Heh. Sorry about that. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1