473,805 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2150
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
3767
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 (ClassB* b; b->func(); ) the base-class function is called instead of the new function in the derived class. All other similar functions (virtual in the base class and overwritten in the the derived class) work fine, it's just this one function. ...
7
3783
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 days and would appreciate this group's helpful expertise. My problem may be related to mixing the C and C++ languages together. Namely, I have a struct which I cannot change (as legacy code) similiar to this (I will change the names throughout as...
6
7939
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 Internet Explorer connection settings (proxy:8080). However, as soon as this setting is enabled, the remoting program running on their pc stops communicating with the server it sends data to. I've disabled proxy setting on the affected pc, rebooted...
28
5228
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(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
9
2827
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 facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form based "Smart
2
5438
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 recommended by many for web applications. I create the instances of Excel, Workbook and the worksheet. And later on Release the references by “System.Runtime.InteropServices.Marshal.ReleaseComObject(Object)” and making the object as null finally....
6
3818
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 is more that 1249 bytes, then the progress bar of the browser will move too slow and then displaying that the page not found!!!! If the message is less than or equal to 1249 then no problem.
8
9766
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 problem: my vb.net program has problems with UNC. If the UNC server is restarted or goes off-line, my VB.net program crashes. The code for UNC access to the file is included below and is put in the tick event of a form timer control running every...
2
4559
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 set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
6
2350
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 solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system (bash to be precise). All you need to know is that there are four classes. (Of course, you may...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.