|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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;
}
|
| Sponsored Links |
|
|
|
|||
|
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);
}
}
|
|
|||
|
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;
}
|
|
|||
|
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.
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |