Thread: Problem in solving a character array problem from K&R book

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    Problem in solving a character array problem from K&R book

    Excercise:
    Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab. When either a tab or a single blank would suffice to reach a tab stop, which should be given
    preference?
    My solution has not taken the final shape. Right now I am stuck in the last else-if. Program is working fine if l < 4 but not when l>4. I've been trying to debug for a while before giving up & looking for clues here.
    Please help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SPACE ' '
    #define IN 1
    #define OUT 0
    #define TAB '\t'
    
    int main(void) {
    	int buffer[50];
    	int i;
    	int l =0;
    	int m=0;
    	int c;
    	int STATE = OUT;
    for (i=0; i < 49 && (c = getchar()) != 'X' && c != '\n'; ++i){
    	buffer[i] = c; 
    }
    buffer [i+1] = '/0';
    while (buffer[m] != '/0') 
    	{
    	if (buffer[m] != SPACE){
    		STATE = IN; //In the word
    		l=0;
    	    putchar(buffer [m]); //print the input as output
    	    m++;
    	}
    	else if (buffer[m] == SPACE)
    	{
    	STATE = OUT; //Now outside the word
    	if (STATE == OUT)
    	{
    		l = l+1; //start counting the spaces
    
    if (l< 5){ //ignore if number of spaces are less than 5
    	putchar(buffer [m]); 
    	m++;
    }
    		else if (l >= 4) 
    		{
    			m = m - 4; //since 5 continuous spaces are detected push array index back
    			l= l-4; //push offset back
    			buffer[m] = TAB; //insert TAB at appropriate array field
    			putchar (buffer[m]); //stream the output with TAB
    			m++;
    		}
    
    
    		}
    
    	}
    		}
    
    		}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first step would be to learn how to indent code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SPACE ' '
    #define IN 1
    #define OUT 0
    #define TAB '\t'
    
    int main(void)
    {
      int buffer[50];
      int i;
      int l = 0;
      int m = 0;
      int c;
      int STATE = OUT;
      for (i = 0; i < 49 && (c = getchar()) != 'X' && c != '\n'; ++i) {
        buffer[i] = c;
      }
      buffer[i + 1] = '/0';
      while (buffer[m] != '/0') {
        if (buffer[m] != SPACE) {
          STATE = IN;               //In the word
          l = 0;
          putchar(buffer[m]);       //print the input as output
          m++;
        } else if (buffer[m] == SPACE) {
          STATE = OUT;              //Now outside the word
          if (STATE == OUT) {
            l = l + 1;              //start counting the spaces
            if (l < 5) {            //ignore if number of spaces are less than 5
              putchar(buffer[m]);
              m++;
            } else if (l >= 4) {
              m = m - 4;            //since 5 continuous spaces are detected push array index back
              l = l - 4;            //push offset back
              buffer[m] = TAB;      //insert TAB at appropriate array field
              putchar(buffer[m]);   //stream the output with TAB
              m++;
            }
          }
        }
      }
    }
    First point, /0 is not the same as \0

    Next, you need to count all the spaces before deciding what mix of spaces and tabs to output. So you only do this when you change state from OUT to IN.
    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
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by alter.ego View Post
    Program is working fine if l < 4 but not when l>4. I've been trying to debug for a while before giving up & looking for clues here.
    One helpful tool is to run your program from the commandline with the following:

    entab | tr '\t' '$'

    Then you can see the actual tab characters in the output as '$' characters. On Windows, the program `tr' with mingw or you can download it from Coreutils Gnuwin32.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character array problem
    By alter.ego in forum C Programming
    Replies: 5
    Last Post: 09-04-2012, 12:19 PM
  2. character array problem .. please help :((
    By amerjamil in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2010, 04:02 AM
  3. Character array initialization problem
    By ThatDudeMan in forum C Programming
    Replies: 7
    Last Post: 12-03-2010, 03:56 PM
  4. Problem with setting a character array in a class member.
    By swbluto in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2006, 02:41 AM
  5. Problem with moving character around(using array for map)
    By o0obruceleeo0o in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2003, 05:29 AM