Replacing tabs in the input

This is a discussion on Replacing tabs in the input within the C Programming forums, part of the General Programming Boards category; Hey everyone, I am currently working through the K&R book trying to teach myself C and one of the exercises ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    1

    Question Replacing tabs in the input

    Hey everyone, I am currently working through the K&R book trying to teach myself C and one of the exercises is confusing me. The exercise is this:
    Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?

    I think I understand what it is asking and have written a program that seems to work. I wrote the program to display the modified tabs rather than changing the string entered itself though I don't think it would be too difficult to switch in any case. Here is the function I wrote:
    Code:
    void printstring(char string[], int n)
    {
      int currentcolumn=1;
      int i, x;
      for(i=0; string[i]!='\0'; i++, currentcolumn++)
        {
          if(string[i]=='\t')
    	for(x=0; currentcolumn%n!=0; x++)
    	  {
    	    putchar(' ');
    	    currentcolumn++;
    	  }
          else
    	putchar(string[i]);
          if(currentcolumn==80 || string[i]=='\n')
    	currentcolumn=0;
        }
    }
    Does anyone have any suggestions on how that could be improved or changed? Or maybe I am missing the point of the exercise entirely . I am very new to C so any suggestions would be greatly appreciated.

    Thanks!
    Last edited by dineren; 08-22-2003 at 04:50 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Slight issue here:
    Code:
    int currentcolumn=1
    You set it to one first off, then every time through the loop, you do this:
    Code:
    if(currentcolumn==80 || string[i]=='\n')
    	currentcolumn=0
    So the first line will be one off.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 12:09 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  5. improving input (replacing "cin")
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2001, 05:55 PM

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