Did you start by sketching out on paper all the things you needed to do, or did you just hack together some code?
For example, do you know what the following should do (where <s> and <t> are space and tab respectively)?
hello
<s>hello
<s><s><s><s><t>hello
<t>hello
Try using a debugger to follow the actual program flow.
I typed in "<s><s>hello\n" and followed what happened.
Code:
$ gcc -Wall -g foo.c
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) list
1 #include<stdio.h>
2
3 #define TAB 8
4
5 int main()
6 {
7 int tabs = 0, spaces = 0, pos = 1, c = 0;
8 while((c = getchar()) != 0) {
9 if(c == ' ') {
10 ++spaces;
(gdb) b 9
Breakpoint 1 at 0x40059f: file foo.c, line 9.
(gdb) run
Starting program: /home/sc/Documents/a.out
hello
Breakpoint 1, main () at foo.c:9
9 if(c == ' ') {
(gdb) print c
$1 = 32
(gdb) n
10 ++spaces;
(gdb)
11 if(spaces == 7) {
(gdb)
8 while((c = getchar()) != 0) {
(gdb)
Breakpoint 1, main () at foo.c:9
9 if(c == ' ') {
(gdb)
10 ++spaces;
(gdb)
11 if(spaces == 7) {
(gdb)
8 while((c = getchar()) != 0) {
(gdb)
Breakpoint 1, main () at foo.c:9
9 if(c == ' ') {
(gdb) print (char)c
$3 = 104 'h'
(gdb) n
16 if((pos % TAB) - 1 == 0 && spaces > 0 && tabs == 0 ) {
(gdb) print pos
$4 = 1
(gdb) n
17 putchar('\t');
(gdb)
What is pos actually supposed to mean?
You're only supposed to be outputting two spaces in this instance.
Finally, your while loop should read
Code:
while((c = getchar()) != EOF)