473,715 Members | 3,751 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
Guru Jois wrote:

A bunch of not-C, for no terribly good reason but:
while EOF
I don't think so.
do
begin
input char
if char = space then
begin
if flag = 0 then
No declaration for `flag`; hence, no know state.
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2
You have no step 2 (in fact you have no steps).

You don't need `goto` for this problem, especially if you're
going to write in an invented pseudocode.
end while
Make up your mind whether you're using begin-end blocks or
whether your control-structures have end-Whatever syntax.

--
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

May 31 '07 #11
On May 31, 2:24 pm, Chris Dollin <chris.dol...@h p.comwrote:
Guru Jois wrote:

A bunch of not-C, for no terribly good reason but:
while EOF

I don't think so.
do
begin
input char
if char = space then
begin
if flag = 0 then

No declaration for `flag`; hence, no know state.
begin
let flag = 1;
print char;
end
end
else
begin
let flag = 0
print char
end
end
goto step 2

You have no step 2 (in fact you have no steps).

You don't need `goto` for this problem, especially if you're
going to write in an invented pseudocode.
end while

Make up your mind whether you're using begin-end blocks or
whether your control-structures have end-Whatever syntax.

--
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
just a steps to that asker... sir. I wrote it as steps, but later
removed, but forget this one....
Please identify notable mistakes...not a silly ones

Bye
Guru Jois

May 31 '07 #12
Tak
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?

May 31 '07 #13
Guru Jois wrote:
On May 31, 2:24 pm, Chris Dollin <chris.dol...@h p.comwrote:
>Guru Jois wrote:

A bunch of not-C, for no terribly good reason but:
SNIP SIGNATURES.
just a steps to that asker... sir. I wrote it as steps, but later
removed, but forget this one....
Please identify notable mistakes...not a silly ones
(a) I did. The very first line is a notable mistake, rendering the
algorithm useless:
while EOF
(b) Computers can't tell the difference between a silly mistake and
a notable one.

(c) Programmers have to take care /all the time/. Sloppiness feeds
on itself.

--
"It is seldom good news." ~Crystal Ball~, /The Tough Guide to Fantasyland/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 31 '07 #14
Tak <ka******@gmail .comwrites:
>Algorithm trim_space
<bad algorithm snipped>
>
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?
Yes. What happens when the input ends with one or more spaces?

--
Ben.
May 31 '07 #15
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 from http://www.teranews.com

May 31 '07 #16
On May 31, 6:04 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Tak <kakat...@gmail .comwrites:
Algorithm trim_space

<bad algorithm snipped>


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?

Yes. What happens when the input ends with one or more spaces?

--
Ben.
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

May 31 '07 #17
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

May 31 '07 #18
Guru Jois <gu*******@gmai l.comwrites:
On May 31, 6:04 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>Tak <kakat...@gmail .comwrites:
>Algorithm trim_space

<bad algorithm snipped>
>>
My program of exercise 1-9
#include <stdio.h>
int main()
{
<snipped>
}
It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?

Yes. What happens when the input ends with one or more spaces?

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);
}
}

}
I'd write it:

#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
}
return 0;
}

because I am not a fan of flag variables and this pattern is very
useful for all sorts of filtering and counting programs.

--
Ben.
May 31 '07 #19
On May 31, 6:04 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Tak <kakat...@gmail .comwrites:
Algorithm trim_space

<bad algorithm snipped>


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?

Yes. What happens when the input ends with one or more spaces?

--
Ben.
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 #20

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
9196
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...
1
9103
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
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();...
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
3
2118
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.