Detab Function Loop Hang

This is a discussion on Detab Function Loop Hang within the C Programming forums, part of the General Programming Boards category; Hello, I'm a scheme programmer teaching myself C with the K and R book (what can I say, I'm bored ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Detab Function Loop Hang

    Hello, I'm a scheme programmer teaching myself C with the K and R book (what can I say, I'm bored with my Java OOP class). The end of chapter one asks you to write a function to replace all tabs with a set number of spaces. So I wrote the function below. Anyhow, the program works fine if I enter no tabs, but if I enter a tab it goes into a never ending loop.

    Two questions:
    1) Why
    2) A *tip* on what a better way to solve this is

    I know there are answers that can be found online, but they are not very helpful to a C-novice like myself because they do not explain precisely what they or are doing, or more importantly, why.

    Code:
    void detab(char s[], char x[], int space)
    {
    	int i;
    
    	i = 0;
    	while(s[i] != '\0')
    	{
    		if (s[i] == '\t') {
    			for(int y = space; y == 0; y--)
    				x[i+y] = ' ';
    
    		} else {
    			x[i] = s[i];
    			i++;
    		}
    	}
    }
    
    /** note: to compile this use: gcc -std=gnu99

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A big problem is how the tab got there -- while there is a character '\t', if the string was supplied from keyboard input or from a text file, for example, it will be a number of spaces and cannot possibly be an actual '\t'.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    A couple things.

    1) You need two index variables. You can't do it with 1. One for s[] and one for x[]. Right now, you are losing the position in x[] since i is used for s[].
    2) Your tab replacement FOR loop is bad. If there are 4 spaces to add, the largest offset you should use is 3, not 4.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    In my opinion, transducer-like functions such as this are more easily understood using pointers rather than indexes.

    Code:
    void detab(char s[], char x[], int space)
    {
        char *in = s;
        char *out = x;
        for(; *in; ++in)
        {
            if(*in == '\t')
                for(int y = 0; y < space; ++y)
                    *out++ = ' ';
            else
                *out++ = *in;
        }
        *out = '\0';
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Your code doesn't show the portion that reads input; and how many spaces make up a <TAB>? Post the entire code here.
    Reason it's stuck in an infinite loop: the if statement doesn't increment i after you have replaced the <TAB> with spaces.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Variable in is updated 1 byte each time through the FOR loop. See ++in, so that's not the problem.

    [EDIT] Sorry itCbitC - I was looking at brewbuck's code. I think the OP is long gone.
    Last edited by Dino; 01-27-2009 at 05:26 PM. Reason: typo
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    for(int y = space; y == 0; y--)
    This for loop is all wrong - unless space is 0, it wont loop even once. If space is 0, it will loop once, then break the loop, with y = -1.

    Look at brewbucks code for a correct loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function (modules) and loop help.
    By GCNDoug in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 12:43 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21