Raj <za*****@gmail.comwrote:
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.
return;
main() is supposed to return an int, so make that
return 0;
}
Regards, Jens
--
\ Jens Thoms Toerring ___
jt@toerring.de
\__________________________
http://toerring.de