473,322 Members | 1,690 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,322 software developers and data experts.

Please tell me if I've written this correctly

Hi friends,

I am beginning to practice C from K&R. Although I am only familiar
with the language, this time I want to invest time learning it
thoroughly. I've written this practice excercise program to replace
more than a single blank in an input with only a single blank in the
output. Here's the code.

#include <stdio.h>

//A program that displays keyboard input to the monitor, but trimming
more than a single space to a single space
//I've not replaced tabs with single spaces yet

#define SPACE 1

void main()
{

int c, state;
c=0;
state=SPACE;
while ((c=getchar()) != EOF)
{
if (!(state && (c==' '))) putchar(c);

if (c==' ')
{
state=SPACE;
}
else
{
state= !SPACE;
}

}
}

Although I've tested it four to five times, I don't know if I can be
sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and
the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on
purpose, I have not yet detected tabbed spaces.

Could you gurus please tell me if I've written this program correctly?
Nov 14 '05 #1
8 1220
On Tue, 02 Mar 2004 02:56:44 -0800, Sathyaish wrote:
Although I've tested it four to five times, I don't know if I can be
sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and
the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on
purpose, I have not yet detected tabbed spaces.
Logical NOT !
Bitwise NOT ~
Logical AND &&
Bitwise AND &

They are in section 2.9 of K&R second edition. Although there is no need
for bitwise operators for your program.
Could you gurus please tell me if I've written this program correctly?


Aside from the "void main()" issue it appears to be correct. Here's how I
would write it:
#include <stdio.h>
int main(){
/* main can't be void */
int c, space;
space = 0;
while ((c=getchar()) != EOF) {
/* compare c to space first, since the input will probably be
mostly non space this will save us one comparison for
each character */
if ((c != ' ') || !space) putchar(c);
/* set space true if c was a space otherwise false */
space = (c == ' ');
}
}

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 14 '05 #2
Sathyaish writes:
Although I've tested it four to five times, I don't know if I can be
sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and
the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on
purpose, I have not yet detected tabbed spaces.


Bitwise and logical things are fairly disjoint. WRT logical things, a value
of 0 is treated as logical false, anything else is true. This is not a
definition that one immediately falls in love with! The bitwise operators
do not recognize true or false, they are just numbers. Remember that there
are not "good" numbers and "bad" numbers. I ssume you realize the bitwise
NOT you refer to is denoted by ~.
Nov 14 '05 #3
Nils Petter Vaskinn wrote:

On Tue, 02 Mar 2004 02:56:44 -0800, Sathyaish wrote:
Although I've tested it four to five times, I don't know if I can be
sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and
the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on
purpose, I have not yet detected tabbed spaces.


Logical NOT !
Bitwise NOT ~
Logical AND &&
Bitwise AND &

They are in section 2.9 of K&R second edition. Although there is no need
for bitwise operators for your program.
Could you gurus please tell me if I've written this program correctly?


Aside from the "void main()" issue it appears to be correct. Here's how I
would write it:

#include <stdio.h>
int main(){
/* main can't be void */
int c, space;
space = 0;
while ((c=getchar()) != EOF) {
/* compare c to space first, since the input will probably be
mostly non space this will save us one comparison for
each character */
if ((c != ' ') || !space) putchar(c);
/* set space true if c was a space otherwise false */
space = (c == ' ');
}
}


/* BEGIN practice.c */

#include <stdio.h>

#define SPACE 1

int main(void)
{
int c, old_state, new_state;

old_state = SPACE;
c = getchar();
while (c != EOF) {
new_state = c == ' ';
if (!(new_state && old_state)) {
putchar(c);
}
old_state = c == '\n' ? SPACE : new_state;
c = getchar();
}
return 0;
}

/* END practice.c */

--
pete
Nov 14 '05 #4
nrk
pete wrote:
Nils Petter Vaskinn wrote:

On Tue, 02 Mar 2004 02:56:44 -0800, Sathyaish wrote:
> Although I've tested it four to five times, I don't know if I can be
> sure as to whether it is right, since I've made guesses about the
> BITWISE operators such as NOT to be the same as the logical NOT ! and
> the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on
> purpose, I have not yet detected tabbed spaces.


Logical NOT !
Bitwise NOT ~
Logical AND &&
Bitwise AND &

They are in section 2.9 of K&R second edition. Although there is no need
for bitwise operators for your program.
> Could you gurus please tell me if I've written this program correctly?


Aside from the "void main()" issue it appears to be correct. Here's how I
would write it:

#include <stdio.h>
int main(){
/* main can't be void */
int c, space;
space = 0;
while ((c=getchar()) != EOF) {
/* compare c to space first, since the input will probably be
mostly non space this will save us one comparison for
each character */
if ((c != ' ') || !space) putchar(c);
/* set space true if c was a space otherwise false */
space = (c == ' ');
}
}


/* BEGIN practice.c */

#include <stdio.h>

#define SPACE 1

int main(void)
{
int c, old_state, new_state;

old_state = SPACE;
c = getchar();
while (c != EOF) {
new_state = c == ' ';
if (!(new_state && old_state)) {
putchar(c);
}
old_state = c == '\n' ? SPACE : new_state;
c = getchar();
}
return 0;
}

/* END practice.c */


#include <stdio.h>

int main(void) {
int prev = 0;
int c;

while ( (c = getchar()) != EOF ) {
if ( c != ' ' || prev != ' ' )
putchar(c);
prev = c;
}

return 0;
}

-nrk.

--
Remove devnull for email
Nov 14 '05 #5
/************************************************** ******************
if (c==' ')
{
state=SPACE;
}
else
{
state= !SPACE;
}
*/
//I could better write the above if construct as
state = (c==' ');
//************************************************** ********************
Regards,
Sathyaish Chakravarthy
Nov 14 '05 #6
Vi****************@yahoo.com (Sathyaish) wrote in
news:7b**************************@posting.google.c om:
/************************************************** ******************
if (c==' ')
{
state=SPACE;
}
else
{
state= !SPACE;
}
*/
//I could better write the above if construct as
state = (c==' ');


What if SPACE was set to 17? Below would better match the if-else block:

state = ' ' == c ? SPACE : !SPACE;

--
- Mark ->
--
Nov 14 '05 #7
Mark A. Odell wrote:

Vi****************@yahoo.com (Sathyaish) wrote in
news:7b**************************@posting.google.c om:
/************************************************** ******************
if (c==' ')
{
state=SPACE;
}
else
{
state= !SPACE;
}
*/
//I could better write the above if construct as
state = (c==' ');


What if SPACE was set to 17?


I think he's removing SPACE from the code,
as nrk did elsewhere in this thread.

--
pete
Nov 14 '05 #8
Groovy hepcat Sathyaish was jivin' on 2 Mar 2004 02:56:44 -0800 in
comp.lang.c.
Please tell me if I've written this correctly's a cool scene! Dig it!
void main()


You have been told before that main() is supposed to return an int.
Pay attention to what you are told, lest you be ignored!

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #9

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

Similar topics

7
by: Candice | last post by:
Please somebody help! I've deleted my admin username and password which was initially set at test. Now I can't log into my website as the administrator. How do I put Username and Password back so...
26
by: Michael Strorm | last post by:
Hi! I posted a message a while back asking for project suggestions, and decided to go with the idea of creating an adventure game (although it was never intended to be a 'proper' game, rather an...
5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
39
by: gtippery | last post by:
Newbie-ish questions - I've been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways to do what this does. It does compile & run for me (with...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
7
by: Jeff | last post by:
Hi - For my VB.NET app, I have a SQL2K database that I use to create a dataset with multiple data tables. I've created a dataview (dvReportsTo) of one of the tables, SCPMaster, and I've bound a...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
118
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
4
by: Tonio Tanzi | last post by:
I have the following problem in a Win 2000 Server + SQL Server 2000 environment and I hope somewhat can help me to resolve it (after many days of useless attempts I am desperate). In my database...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.