E
Eric
Hello,
I have been working through the K&R book (only chapter one so far) examples
and exercises. After much sweat and hair pulling, I think I have a
solution for ex 1-18 on page 31.
It seems to work but may be missing some error checking. Can you please
take a look and see if my logic is correct and any other improvements.
This is not a homework but it could just as well be. I am trying to get a
handle on C programming and would eventually like to help out with some of
the GNU projects as I get closer to retirement.
/* function to strip trailing blanks and tabs from the input line */
/* and not to print any blank lines */
#include <stdio.h>
#define MAXIMUM 1000 /* maximum line length including '\0' marker */
void remblank(char line[]);
int main()
{
int c, i;
i = 0;
char line[MAXIMUM];
c = getchar();
while (c != EOF)
{
line = c;
if (i == (MAXIMUM - 1)) /* make sure line is <= 1000 */
line = '\n';
if (line == '\n')
{
line[i + 1] = '\0';
remblank(line);
i = 0;
}
else
++i;
c = getchar();
}
/* print the last bit if there is more */
/* after the last newline */
if (c == EOF)
{
line = '\0';
while (line[i - 1] == ('\t') || line[i - 1] == (' '))
{
line[i - 1] = '\0';
--i;
}
printf("%s", line);
}
return 0;
}
void remblank(char s[])
{
int i;
for(i = 0; s != '\n'; i++) /* count characters in line */
;
/* if character before newline is a blank or tab */
/* replace it with a newline and make the next */
/* character an end of array marker */
while (s[i - 1] == ('\t') || s[i - 1] == (' '))
{
s[i -1] = '\n';
s = '\0';
--i;
}
/* if line is not blank, print the line without */
/* the trailing blanks or tabs */
if(s[0] != '\n')
printf("%s", s);
}
Thank you,
Eric
I have been working through the K&R book (only chapter one so far) examples
and exercises. After much sweat and hair pulling, I think I have a
solution for ex 1-18 on page 31.
It seems to work but may be missing some error checking. Can you please
take a look and see if my logic is correct and any other improvements.
This is not a homework but it could just as well be. I am trying to get a
handle on C programming and would eventually like to help out with some of
the GNU projects as I get closer to retirement.
/* function to strip trailing blanks and tabs from the input line */
/* and not to print any blank lines */
#include <stdio.h>
#define MAXIMUM 1000 /* maximum line length including '\0' marker */
void remblank(char line[]);
int main()
{
int c, i;
i = 0;
char line[MAXIMUM];
c = getchar();
while (c != EOF)
{
line = c;
if (i == (MAXIMUM - 1)) /* make sure line is <= 1000 */
line = '\n';
if (line == '\n')
{
line[i + 1] = '\0';
remblank(line);
i = 0;
}
else
++i;
c = getchar();
}
/* print the last bit if there is more */
/* after the last newline */
if (c == EOF)
{
line = '\0';
while (line[i - 1] == ('\t') || line[i - 1] == (' '))
{
line[i - 1] = '\0';
--i;
}
printf("%s", line);
}
return 0;
}
void remblank(char s[])
{
int i;
for(i = 0; s != '\n'; i++) /* count characters in line */
;
/* if character before newline is a blank or tab */
/* replace it with a newline and make the next */
/* character an end of array marker */
while (s[i - 1] == ('\t') || s[i - 1] == (' '))
{
s[i -1] = '\n';
s = '\0';
--i;
}
/* if line is not blank, print the line without */
/* the trailing blanks or tabs */
if(s[0] != '\n')
printf("%s", s);
}
Thank you,
Eric