Problem in C code


R

Raj

Following is a code to replace blanks in entered string with adequate
number of tabs & spacings as required. I've taken the width of tab as
5 characters here. The problem that occurs here is for the 2nd set of
blanks onwards, i.e. blanks after 2 words. For such blanks, the output
shows 1blank less than what has been entered in input. For 1st set of
blanks there is no problem, but for each successive set of blanks one
less blank is shown in output...
Code:
#include<stdio.h>
#define TABSTOP 5
main()
{
int c,i=0;
clrscr();
printf("Enter text\n");
while ((c=getchar())!='@') /* '@' signifies end of input */
if (c==' '){
while ((c=getchar())==' ')
i++;
if (i<TABSTOP){
while (i>=0){
printf(" ");
i--;
}
putchar(c);
continue;
}
if (i>=TABSTOP){
printf("\t");
i-=TABSTOP;
}
while (i>=0){
printf(" ");
i--;
}
}else
putchar(c);
getch();
return;
}

If anybody can help... thanks!
 
Ad

Advertisements

P

pete

Raj said:
Following is a code to replace blanks in entered string with adequate
number of tabs & spacings as required.

I have no idea what the adequate number of tabs and spacing is,
given that the number is different from what is typed in.
 
J

Jens Thoms Toerring

Raj said:
Following is a code to replace blanks in entered string with adequate
number of tabs & spacings as required. I've taken the width of tab as
5 characters here. The problem that occurs here is for the 2nd set of
blanks onwards, i.e. blanks after 2 words. For such blanks, the output
shows 1blank less than what has been entered in input. For 1st set of
blanks there is no problem, but for each successive set of blanks one
less blank is shown in output...

One problem is that you assume that a the first space you encounter
is always on a tab stop position. But you have to count not only
the number of spaces but also the position in the current line and,
if you find a set of spaces, output first as many spaces until you
are at a tab stop position and only then use tabs to replace spaces.
But there are also some more logical problems with your program.
If there are e.g TABSTOP spaces at the start of a line you don't
replaces it by a tab but instead output TABSTOP spaces. And if you
do a replacement you forget to output the following non-space
character you have already read in.
Code:
#include<stdio.h>
#define TABSTOP 5
main()

Make that

int main( void )
{
int c,i=0;
clrscr();

That's not a standard C function (and it may keep the output
of your program from being redirected to a file since it may
output some characters that the terminal interprets and which
shouldn't go into a file).
printf("Enter text\n");
while ((c=getchar())!='@') /* '@' signifies end of input */
if (c==' '){

You don't count this space into the number of spaces read in.
while ((c=getchar())==' ')
i++;
if (i<TABSTOP){
while (i>=0){
printf(" ");

I would use putchar() instead of printf() here.
i--;
}
putchar(c);
continue;
}
if (i>=TABSTOP){

Shouldn't this be

while ( i >= TABSTOP )

Otherwise you replace only the first TABSTOP spaces by a
tab.
printf("\t");

Again, I would use putchar().
i-=TABSTOP;
}
while (i>=0){
printf(" ");
i--;
}
}else
putchar(c);
getch();

Again not a standard C function, getchar() should do the same
job I guess.

main() is supposed to return an int, so make that

return 0;
Regards, Jens
 
T

Thad Smith

Raj said:
Following is a code to replace blanks in entered string with adequate
number of tabs & spacings as required. I've taken the width of tab as
5 characters here.

The fundamental problem is that the specification is incomplete. You
want "adequate number of tabs & spacings as required", but the actual
requirement is missing. How is adequacy defined? Does each tab always
expand to 5 spaces, or are there tab stops every 5 columns (more typical
use of tab)? If tab stops, where is the first tab stop? If tabs are
assigned to fixed columns, which character or character sequences resets
the line position to the beginning of the line?

Jens gave some good suggestions for improving the code and making it
more portable. In addition, I suggest using the standard EOF condition,
rather than a special character to designate end of input. Yoy might
want to modify the program to be usable as a filter, which reads from
stdin and writes to stdout until EOF is encountered, rather than
prompting for input. Such a program can normally be tested with console
entry, if desired.
 
R

Raj

The fundamental problem is that the specification is incomplete. You
want "adequate number of tabs & spacings as required", but the actual
requirement is missing. How is adequacy defined? Does each tab always
expand to 5 spaces, or are there tab stops every 5 columns (more typical
use of tab)? If tab stops, where is the first tab stop? If tabs are
assigned to fixed columns, which character or character sequences resets
the line position to the beginning of the line?

Jens gave some good suggestions for improving the code and making it
more portable. In addition, I suggest using the standard EOF condition,
rather than a special character to designate end of input. Yoy might
want to modify the program to be usable as a filter, which reads from
stdin and writes to stdout until EOF is encountered, rather than
prompting for input. Such a program can normally be tested with console
entry, if desired.

Well thanx! I agree with Jens that putchar wud be better than printf,
but i guess that wouldnot affect the output in any way.
And i found out the problem in my code, it was that i wasn't setting
the value of i to 0 again after it has been changed to -1 before...
hence the code was working fine for 1st set of blanks ands outputting
one less blank than required for next set of blanks onwards...

@thad... i really never know how to make EOF work... the thing is that
i dont know how to end the input if i use EOF... I once printed the
value of EOF in my computer & found it to be -1. So i used while
((c=getchar())!=-1).... but still i just kept entering input without
having a clue as to how to end it... any suggestions??
 
Ad

Advertisements

S

santosh

Raj wrote:


You should try to reply individually to each responder.
i really never know how to make EOF work... the thing is that
i dont know how to end the input if i use EOF... I once printed the
value of EOF in my computer & found it to be -1. So i used while
((c=getchar())!=-1).... but still i just kept entering input without
having a clue as to how to end it... any suggestions??

The very reason for the standard library to have a symbolic constant, EOF,
is because it's value can very from system to system. It need not be -1 on
the next system you program for, and your code will then break.

The common C idiom is:

while ((c = getc(s)) != EOF) /* ... */

To generate an end-of-file from the keyboard press CONTROL-D under UNIX
systems and CONTROL-Z for Windows and DOS. Be aware that EOF and
end-of-file are not synonymous. The former is an integer constant value
returned by C standard library functions to inform the caller that they
either encountered an end-of-file condition, or there was an error. To find
out which it was, you need to use feof or ferror on the relevant stream.
 
Ad

Advertisements


Top