Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-17-2008, 10:04 PM
british0zzy british0zzy is offline
Newbie
 
Join Date: Jul 2008
Posts: 3
Rep Power: 0
british0zzy is on a distinguished road
Exclamation Newbie Question

I'm writing a program that removes tabs from the input and replaces it with the correct number of spaces. It's giving me odd results, which I can't quite figure out why. If you could look at this short program and give me some pointers, it'd be greatly appreciated

Code:
#include <stdio.h>

#define TABLENGTH 4

main()
{
    int n; /* Position of cursor relative to tab stops */
    int c;
    
    n = TABLENGTH;  /* Setting n to TABLENGTH is functionally equivalent to setting it to zero, but will make calculations easier */
    while ((c=getchar()) != EOF) {
        if (n > TABLENGTH)
            n = 1;  /* Should this be n = n - TABLENGTH? */
        if (c != '\t') {
            putchar(c);
            if (c == '\n')
                n = TABLENGTH; /* A newline resets the cursor to 'zero' */
            else
                ++n;
        }
        else {
            for (c = 0; c < n; ++c)
                putchar(' ');
            n = TABLENGTH;  /* Tabs reset cursor to 'zero' */
        }
    }
    return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-17-2008, 11:16 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: Newbie Question

You're making this too complicated. Try this.

Code:
#include <stdio.h>

#define    TABLEN    4

void main(void)
{
    int ch, i;
    while((ch = getchar()) != EOF)
    {
        if(ch == '\t')
        {
            for(i = 0; i < TABLEN; ++i)
                putchar(' ');
        }
        else
            putchar(ch);
    }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-17-2008, 11:27 PM
british0zzy british0zzy is offline
Newbie
 
Join Date: Jul 2008
Posts: 3
Rep Power: 0
british0zzy is on a distinguished road
Default Re: Newbie Question

ok, I didn't explain the objective of the program clearly. I am assuming there are fixed tab stops every n columns. So if I the input is "12'\t'34", the output should be "12**34". Or if I typed "12345'\t'6", the output should be "12345***6". (The *'s are spaces and '\t' is obviously a tab.)
Does that make more sense?
My earlier version works perfectly except when you skip to a new line, it breaks because skipping to a newline resets the cursor to the beginning.
Here's the earlier code:
Code:
#include <stdio.h>

#define TABLENGTH   4

main()
{
    int n; /* Position of cursor relative to tab stops */
    int c;
    
    n = TABLENGTH;  /* Setting n to TABLENGTH is functionally equivalent to setting it to zero, but will make calculations easier */
    while ((c=getchar()) != EOF) {
        if (n > TABLENGTH)
            n = 1;  /* Should this be n = n - TABLENGTH? */
        if (c != '\t') {
            putchar(c);
            ++n;
        }
        else {
            for (c = 0; c < n; ++c)
                putchar(' ');
            n = TABLENGTH;  /* Tabs reset cursor to 'zero' */
        }
    }
    return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-18-2008, 10:57 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: Newbie Question

OOOH. I see now...so I'm assuming you get n somewhere beforehand, along with some input to parse, right? If you don't want to have it reset at a newline, just add another if statement.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question cowdog88 C# Programming 5 02-11-2008 06:55 PM
Addition in html - Newbie question help plz ! xsystemx HTML Programming 2 09-18-2007 12:33 PM
Doubt re: interface LED with TMS320F2812 ?? Newbie question skn82 C and C++ 3 05-07-2007 12:04 PM
File I/O (newbie question) ntmurphy C and C++ 4 04-04-2007 03:30 AM
Newbie Question Toxic11 Visual Basic Programming 5 08-20-2006 08:52 AM


All times are GMT -5. The time now is 11:43 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads