473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

exercise 1-20 K&R

write a program that replaces strings of blanks by the minimum number
of blanks to space to the next tab stop.

#include <stdio.h>
#define TABSIZE 8
int main()
{
int counter, c;

while ((c = getchar()) != EOF) {
if (c == '\t') {
for (counter = 1; counter <= TABSIZE; ++counter)
putchar(' ');
}
else
putchar(c);
}
return 0;
}

what do you think about this??

Jun 10 '06 #1
2 2209
Aenima1891 wrote:
write a program that replaces strings of blanks by the minimum number
of blanks to space to the next tab stop.

#include <stdio.h>
#define TABSIZE 8
int main()
int main (void) is preferred.
{
int counter, c;

while ((c = getchar()) != EOF) {
if (c == '\t') {
for (counter = 1; counter <= TABSIZE; ++counter)
putchar(' ');
This doesn't accomplish the goal of the exercise which is to replace
the tab with the number of spaces needed for the *next* tab stop, that
is, the next multiple of TABSIZE characters. Your version always
prints TABSIZE spaces which will only be the right thing to do when the
tab character is encountered immemdiately after a tab stop.
}
else
putchar(c);
}
return 0;
}

what do you think about this??


Robert Gamble

Jun 10 '06 #2
Aenima1891 schrieb:
write a program that replaces strings of blanks by the minimum number
of blanks to space to the next tab stop.

#include <stdio.h>
#define TABSIZE 8
int main()
int main (void)
{
int counter, c;
Note: It often helps understanding to separate variable declarations
by "purpose" and not only by type. counter is used just as that,
whereas c is intended to hold a character[*].
This also makes for easier maintenance if you find out that the
_guaranteed_ range of int is not sufficient for your purposes and
you have to switch to long, unsigned long or size_t as counter type.
int counter, anotherCounter;
int c, anotherTempForCharacters;
int resultOfSomeNoncounterCalculation;

Some company coding standards even forbid declaring more than one
variable per line if the declared variables are not loop variables.

while ((c = getchar()) != EOF) {
if (c == '\t') {
for (counter = 1; counter <= TABSIZE; ++counter)
putchar(' ');
You misunderstood the purpose of the task.
Every _line_ is to be formatted independently of the other. For every
line, you start anew with tab stops. This means that you need a
character count for the current line, not an overall character count.

Tab stops are at the same character number (modulo TABSIZE) throughout
the line, i.e. you insert space characters until your counter modulo
TABSIZE equals that number.

Example: For "#define TABSIZE 4",
"\ta\tab\tabc\tabcd\tabcde\t\n" has to become either
" a ab abc abcd abcde \n"
^ ^ ^ ^ ^ ^ ^ ^ ^
where the second line indicates the tab stop positions for clarity.

Or, if you admit the possibility of inserting 0 spaces to fit the
description, your result would be
"a ab abc abcdabcde \n"
^ ^ ^ ^ ^ ^ ^
This is merely the difference between a while() and a do--while()
loop (figure out which one for which case yourself) for you.
}
else
putchar(c);
}
return 0;
}

what do you think about this??

[*] To be more precise: A character cast to unsigned char or EOF.
This is what you typically pass to putchar(), [f]putc() and the
is....() functions and get from getchar(), etc.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: S Green | last post by:
I keep getting an error unresolved external symbol initexample when running python setup.py build, .....for a basic example.cpp, c++ extension exercise for python. I did however successfully...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
5
by: Charles | last post by:
I am going through the exercises and Bruce Eckel's Thinking in C++ and I ran into an exercise that wasn't included in his solutions that I think I could use some assistance with. Exercise 3-26...
10
by: Simon Mansfield | last post by:
I have been given a list of exercises to do by my tutor, one of which is this: http://www.cems.uwe.ac.uk/~amclymer/Software%20Design%20and%20C++/Exercises/Exercise%208/Exercise.html Which i am...
8
by: J Peterman | last post by:
I need to do this exercise, but am having problems. I need to write a program that firstly, sleeps for 5 seconds, then reads a line of input from file descriptor 0 and then writes the line back...
6
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
26
by: arnuld | last post by:
this is the programme i created, for exercise 2, assignment 3 at http://www.eskimo.com/~scs/cclass/asgn.beg/PS2.html it runs fine. i wanted to know if it needs any improvement: ...
0
by: liukaiyuan | last post by:
American Made Circuit Exercise Equipment Come look and see the circuit traning exercise equipment that is fully ajustible to fit all your work out needs. Start your own center today....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.