473,715 Members | 4,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP!! Two questions from <<the c programming language>>

Tak
Exercise 1-9. Write a program to copy its input to its output,
replacing each string of one or
more blanks by a single blank.

Exercise 1-10. Write a program to copy its input to its output,
replacing each tab by \t, each
backspace by \b, and each backslash by \\. This makes tabs and
backspaces visible in an
unambiguous way.

Who could sent me the codes, Thanks very much! My email:
ka******@gmail. com

May 31 '07
27 2009
On May 31, 3:27 pm, Tak <kakat...@gmail .comwrote:
Algorithm trim_space
while EOF
do
begin
input char
if char = space then
begin
if flag = 0 then
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2
end while

My program of exercise 1-9

#include <stdio.h>

int main()
{
int flag=1;
int c;

while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}

}

It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?
yet smaller code..

#include<stdio. h>
main() {

int c;
int flag;

flag = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ') {
if (!flag) {
flag = 1;
putchar (c);
}
}
else {
flag = 0;
putchar (c);
}
}

}

Bye
Guru Jois

Jun 1 '07 #21
Tak
On 5ÔÂ31ÈÕ, ÏÂÎç10ʱ08·Ö, CBFalconer <cbfalco...@yah oo.comwrote:
Tak wrote:
I know how to do, Thank you!

How to do what? Read the following sig.

--
If you want to post a followup via groups.google.c om, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell. org/google/>

--
Posted via a free Usenet account fromhttp://www.teranews.co m
Sorry, I am new here.

Jun 1 '07 #22

"Ben Bacarisse" <be********@bsb .me.ukha scritto nel messaggio
news:87******** ****@bsb.me.uk. ..
#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?
}
return 0;
}

Jun 2 '07 #23
Army1987 wrote:
"Ben Bacarisse" <be********@bsb .me.ukha scritto nel messaggio
news:87******** ****@bsb.me.uk. ..
>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?
Assuming the implementation doesn't actively hinder the simplest I/O, it
gets converted to a negative value which will not compare equal to ' '.
> }
return 0;
}
Jun 2 '07 #24
Harald van Dijk <tr*****@gmail. comwrites:
Army1987 wrote:
>"Ben Bacarisse" <be********@bsb .me.ukha scritto nel messaggio
news:87******* *****@bsb.me.uk ...
>>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
>> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?

Assuming the implementation doesn't actively hinder the simplest I/O, it
gets converted to a negative value which will not compare equal to '
'.
Indeed. But the suggestion to use int last_char is, I think, still a
good one. It would allow the more obvious initial setting of EOF and
would simplify matters if one were to require character class testing
(if (!isspace(last_ char)... and so on).

I had originally used int and changed it for no good reason as far as
I can see.

--
Ben.
Jun 2 '07 #25
On Sat, 2 Jun 2007 15:19:41 +0200, "Army1987" <pl********@for .it>
wrote:
>
"Ben Bacarisse" <be********@bsb .me.ukha scritto nel messaggio
news:87******* *****@bsb.me.uk ...
>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?
Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?
> }
return 0;
}

Remove del for email
Jun 2 '07 #26
Barry Schwarz wrote:
On Sat, 2 Jun 2007 15:19:41 +0200, "Army1987" <pl********@for .it>
wrote:
>>
"Ben Bacarisse" <be********@bsb .me.ukha scritto nel messaggio
news:87****** ******@bsb.me.u k...
>>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
>> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?

Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?
getchar does not work with plain chars, it works with unsigned chars. 163
fits just fine in an unsigned char, so getchar is allowed to return 163.
>> }
return 0;
}
Jun 2 '07 #27

"Barry Schwarz" <sc******@doezl .netha scritto nel messaggio
news:17******** *************** *********@4ax.c om...
On Sat, 2 Jun 2007 15:19:41 +0200, "Army1987" <pl********@for .it>
wrote:
>>
"Ben Bacarisse" <be********@bsb .me.ukha scritto nel messaggio
news:87****** ******@bsb.me.u k...
>>#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
>> while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?

Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?
Assuming sizeof(int) 1, it isn't.
getchar() will return either EOF or a valid *unsigned* char, which
can't of course equal EOF.
Jun 3 '07 #28

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

Similar topics

13
1588
by: yy0127 | last post by:
I don't know why i entered the below code and it will miss some records. Anyone can help me??? users = {} users1 = {} while 1: user, serviceType, msgType, inOut, date, time, numBytes =
6
1186
by: stephane | last post by:
I am preparing an exam and I have a copy of last year exam. I answered the first questions and I wander if I am right or wrong. Can someone have a look at the questions and at my answers please? There is no code to write just general questions about the two class'. the code: /--
8
1175
by: t3hseen | last post by:
what the values of the following expressions: if illegal or unpredictable please state so. stat char *arrax ="C is my favourite"; stat char *lp=arrax; static int five=5; a)*(arrax + 8) b)*arrax+2 c)lp
0
2069
by: Henry | last post by:
Using ideas provided by some of you I was able to figure out how to get the names of the parameters fields of a crystal report specified at run time. The code below just basically puts the data into a comboBox. One thing I noticed, however, is that this method shows me both the parameters used by the main report and parameters used by the sub-report(s).
2
2763
by: KhzQas | last post by:
Hello mates. I am taking my very first programming language (C Programming) and as for practice purposes, I surf sites and try out to make programs. I am having problems with the following C Programming problem. 1) You are required to use at least one for() loop to control the printing of a table in this lab. 2) Do not use the scanf() function in this lab, use the getchar() instead. 3) Your table will consist of three data type...
20
2204
by: Mike | last post by:
Hi all, I am cramming for an technical interview about C++ and programming... I believe there will be problems on data-structures and algorithms... could anybody recommend some online resources/references/websites so that I can find summary sheet about the confusing points of C++ and typical storage/time complexity of common data structures and common algorithms? I do have those big textbooks but I found they don't provide a fast...
3
1735
by: Dr. Erhard Moenige | last post by:
Hi all C-Programmers everywhere ! I 've been learning programming c in console-applications for 1 year and now I'm tired of seeing this boring console-application in my programms. Because of that I decited to programme c in Windows in the CVI Development by National Instruments. I've bought this nice development,but when I'd installed it I found out that is more complex than I had expected. That is why I asked you please to help me...
2
1130
by: nachu | last post by:
I Would Like To Know How Can The Skilll In C Be Improved So As It Would Help For "c Programming Contests"(b'coz I Am In One, So...)
3
1184
by: fdu.xiaojf | last post by:
Hi all, I'm not skilled at programming, so sorry for my ignorance. My questions: (1) which is the better way to calculate the value of attributes of a class ? for example: (A) def cal_attr(self, args): #do some calculations
2
1456
by: yourchandrashekhar | last post by:
I've good programming experience in C++ and now i want to start programming a small UNIX OS kernel. I request in common to anyone over the net to come forward and give me the initial insight of how to get started.... Anyone who finds it interesting could please get to me at one of the follwowing email ID's: your_shekhar@hotmail.com
0
8821
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9047
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7973
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 project—planning, coding, testing, and deployment—without 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
6646
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
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
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
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.