Thread: Need help with K&Rc entab exersice

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    7

    Need help with K&Rc entab exersice

    Code:
    #include<stdio.h>
    
    #define TAB 8 
    
    int main()
    {
        int tabs = 0, spaces = 0, pos = 1, c = 0;
        while((c = getchar()) != 0) {
            if(c == ' ') {
                ++spaces;
                if(spaces == 7) {
                    ++tabs;
                    spaces = 0;
                }
            } else {
                if((pos % TAB) - 1 == 0 && spaces > 0 && tabs == 0 ) {
                    putchar('\t');
                    spaces = spaces - (TAB- (pos-1 % TAB));
                }
                for(;0 < (spaces + tabs);) {
                    if(tabs > 0) {    
                        putchar('\t');
                        --tabs;
                    } else {
                        putchar('#');
                        --spaces;
                    
                    }
    
                }
                putchar(c);
                if(c == '\t')
                    pos = pos + (TAB - (pos-1 % TAB) );
                else
                    ++pos;
                if(c == '\n') 
                    pos = 1;
            }
        }
        
        return 0;
    }
    Thats my code it doesn't work for long inputs i can't understand why how do i fix it.
    this is k&Rc exersize 1-21 question
    Last edited by karthy; 12-01-2016 at 04:03 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    7
    pos is the position of the imaginary pointer. It prints # for spaces btw, It works if i replace \t in the program to 8 spaces lol I think evey time there is a tab and there is a tabstop before 7 spaces it gets offset by some spaces, I want it to print tab and then fill the rest with # because a tab is not exactly 8 spaces and i assumed that was the case when i wrote this code.

    If I replace \t in the program by 8 spaces it works fine
    here's tab = 7 spaces code

    Code:
    #include<stdio.h>
    
    #define TAB 8 
    
    int main()
    {
        int tabs = 0, spaces = 0, pos = 1, c = 0, sync = 0;
        while((c = getchar()) != EOF) {
            if(c == ' ') {
                ++spaces;
                if(spaces == 7) {
                    ++tabs;
                    spaces = 0;
                }
            } else {
                if((pos % TAB) - 1 == 0 && spaces > 0 && tabs == 0 ) {
                    printf("       ");
                    spaces = spaces - (TAB- (pos-1 % TAB));
                }
                for(;0 < (spaces + tabs);) {
                    if(tabs > 0) {
                    sync += (TAB -(pos-1 % TAB));    
                        printf("       ");
                        --tabs;
                    } else {
                        putchar('#');
                        --spaces;
                    
                    }
    
                }
                putchar(c);
                if(c == '\t')
                    pos = pos + (TAB - (pos-1 % TAB) );
                else
                    ++pos;
                if(c == '\n') 
                    pos = 1;
            }
        }
        
        return 0;
    }
    how to i make it so that if it tabstop's early it will fill the rest with #

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    This is just a while loop:
    Code:
                for ( ; 0 < (spaces + tabs); ) {
    It's also written backwards (are you arabic?) and has extraneous parens:
    Code:
                while (spaces + tabs > 0)
    This
    Code:
                if (spaces == 7) {
    should be
    Code:
                if (spaces == TAB - 1) {
    The following needs to be a loop so it will handle different TAB sizes.
    Code:
                    printf("       ");
    Can you see anything wrong with the following? Missing parens perhaps?
    Code:
        (pos - 1 % TAB)
    The tabsize should be a command line parameter.
    Code:
    #define DEFAULT_TABSIZE 8
    
    int main(int argc, char **argv) {
        int tabsize = DEFAULT_TABSIZE;
        if (argc > 2) { } // error: bad parameters
        if (argc == 2) tabsize = atoi(argv[1]);
        if (tabsize < 1) { } // error: insane tabsize
    You should put spaces after your 'if', 'while', 'for', keywords (and 'include', for that matter). That's what most people do (they're not function calls).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with an exersice in K&R c
    By karthy in forum C Programming
    Replies: 6
    Last Post: 09-13-2016, 10:09 AM
  2. Entab - replacing blanks with tabs and blanks
    By thames in forum C Programming
    Replies: 3
    Last Post: 10-20-2012, 03:11 PM
  3. entab---spacing problems
    By dgoodmaniii in forum C Programming
    Replies: 19
    Last Post: 11-19-2009, 08:17 PM

Tags for this Thread