473,219 Members | 2,124 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,219 software developers and data experts.

Problem in C code

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!

Aug 19 '07 #1
5 2104
Raj wrote:
>
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.
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!
--
pete
Aug 19 '07 #2
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
Aug 19 '07 #3
Raj wrote:
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.

--
Thad
Aug 19 '07 #4
Raj
On Aug 20, 2:04 am, Thad Smith <ThadSm...@acm.orgwrote:
Raj wrote:
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.

--
Thad
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??

Aug 20 '07 #5
Raj wrote:

<snip>
@thad...
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.

Aug 20 '07 #6

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

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
7
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial but I have struggled with it for the last two...
6
by: harry | last post by:
Hi, I have a program that runs on multiple client pc's. Occasionally one or more of those pc's use VPN to connect to another corporate network. When using VPN they need to set proxy server in...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.