473,395 Members | 1,613 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,395 software developers and data experts.

k&r ex1-22

Exercise 1-22. Write a program to ``fold'' long input lines into two
or more shorter lines after the last non-blank character that occurs
before the n-th column of input. Make sure your program does something
intelligent with very long lines, and if there are no blanks or tabs
before the specified column.

so we ensure that no blanks or tabs are the last character
at column_max - 1?

but if there are, what should we do?

what if the column width was set to 10
then the sentence:

"beautiful is a nine letter word"

the output should look like?

beautiful\n
is a nin\n
e letter\n
word\n

right?

here is my solution but I would like to know if it is correct
I can't make sense of the vaguely worded problem statement.

#include <stdio.h>

#define COLUMN 25

int main(void)
{
int c;
int i = 0;
int bt = 0;

while((c = getchar()) != EOF) {
if(i == (COLUMN - 1) && c == ' ' || c == '\t') {
++i;
++bt;
} else if(i == COLUMN) {
putchar('\n');
i = 0;
}
if(i < COLUMN && !bt) {
putchar(c);
++i;
} else if(bt) {
bt = 0;
}

}

return 0;
}
Nov 14 '05 #1
1 1677


aegis wrote:
Exercise 1-22. Write a program to ``fold'' long input lines into two
or more shorter lines after the last non-blank character that occurs
before the n-th column of input. Make sure your program does something
intelligent with very long lines, and if there are no blanks or tabs
before the specified column.

so we ensure that no blanks or tabs are the last character
at column_max - 1?

but if there are, what should we do?

what if the column width was set to 10
then the sentence:

"beautiful is a nine letter word"

the output should look like?

beautiful\n
is a nin\n
e letter\n
word\n

right?
Nope. I will write '*' instead of ' ' for clarity:
"beautiful*is*a*nine*letter*word"

1234567890
beautiful*is*a*nine*letter*word\0
becomes
beautiful\nis*a*nine*letter*word\0
Continue after \n:
1234567890
is*a*nine*letter*word\0
becomes
is*a*nine\nletter*word\0
Continue after \n:
1234567890
letter*word\0
becomes
letter\nword\0

Leading to

beautiful\n
is*a*nine\n
letter\n
word

Take 9 instead of 10 and you will have the same result (the \n
is not "visible"). Take 8 and you will have to do something
about beautiful:
12345678
beautifu\n
l*is*a\n
nine\n
letter\n
word\0

or

12345678
beautif-\n
ul*is*a\n
nine\n
letter\n
word\0

here is my solution but I would like to know if it is correct
I can't make sense of the vaguely worded problem statement.

#include <stdio.h>

#define COLUMN 25

int main(void)
{
int c;
int i = 0;
int bt = 0;

while((c = getchar()) != EOF) {
if(i == (COLUMN - 1) && c == ' ' || c == '\t') { Careful: You probably mean
if ( i == (COLUMN - 1) && (c == ' ' || c == '\t') )
&& has higher precedence than || (that is, your code
is equivalent to
if ( (i == (COLUMN - 1) && c == ' ') || c == '\t' )
which probably is not your intention ++i;
++bt;
} else if(i == COLUMN) {
putchar('\n');
i = 0;
}
if(i < COLUMN && !bt) {
putchar(c);
++i;
} else if(bt) {
bt = 0;
} You do not need the last: just set bt to zero every
time.
}

return 0;
}


If you use getchar(), then you either have to store the stuff
in a buffer or implement it the way you have been doing (but
for discarding unnecessary "visible" whitespaces).

BTW: I would give a meaningful name to 'bt' -- as it is no
loop counter but flags meeting a certain condition a name
indicating its purpose helps when revisiting the code after
some time...
If you do not restrict yourself to elementary stuff, you can
also have a look at a recent thread
groups.google.com/groups?selm=316ev7F384kvbU1%40individual.net
where I gave a function solving a slightly more complicated
problem.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2

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

Similar topics

9
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
7
by: El kroty | last post by:
Hi folks!! i'm trying to write k&r ex1-24 without a stack. I almost have it but there's a bug that makes me crazy and can't find it :'( whit some files it works and with others it doesn't. Here is...
2
by: Jim Bancroft | last post by:
Hi all, I'm writing an exception handler for one of my VB.Net methods and wondered how best to dynamically put the class and method name in my message string. My code looks like this...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
7
by: Bilal | last post by:
Hello all, I came across this problem while working out the bugs in my identity trasnformation stylesheets but sidestepped it for later to see if there is an easier/better solution. This is...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
5
by: pbhandari | last post by:
Hi, I have following line in my Perl code--> system("start my.exe myfile.ext") == 0 or die "error ($?) : $!"; Q1: How do I close my.exe? Q2: If I give myfile.ex1 instead of myfile.ext,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.