473,396 Members | 1,655 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,396 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 2116
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.