to get more knowledge in 'c'

A

anand devarajan

hi friends,
i need some one to frame me questions for to write programs,it
might be simple ones like prime no. prg but i need this type of
coaching for to develop my programming knowledge
 
S

Spiros Bousbouras

anand said:
hi friends,
i need some one to frame me questions for to write programs,it
might be simple ones like prime no. prg but i need this type of
coaching for to develop my programming knowledge

Ok , write a programme which eliminates the blanks from
the end of every line in its standard input and prints the
result on standard output.
 
S

Spiros Bousbouras

Spiros said:
Ok , write a programme which eliminates the blanks from
the end of every line in its standard input and prints the
result on standard output.

Oh yeah , to make it more interesting the programme should
use a constant amount of memory regardless of how large
the lines are.
 
I

Ico

anand devarajan said:
hi friends,
i need some one to frame me questions for to write programs,it
might be simple ones like prime no. prg but i need this type of
coaching for to develop my programming knowledge

Here is one for you: write a program that adds proper capitalization and
interpunction to a usenet message.
 
S

Simon Biber

Spiros said:
Ok , write a programme which eliminates the blanks from
the end of every line in its standard input and prints the
result on standard output.

Good idea.

There are some idioms in C which you can re-use over and over every time
you see a problem like this.

Here is one that copies each character from standard input to standard
output:

First you define an int variable to hold the result of getchar. That
will be a value from 0 to UCHAR_MAX if successful, and a negative value
called EOF if unsuccessful. Don't try to use a char variable for this.

int ch;

Then you start a loop. Each time through the loop, there is a complex
expression that is evaluated. First, the getchar function is called and
the result is stored in ch. Then the result is compared against the
constant EOF. The loop continues while the value returned is not EOF.

while( (ch = getchar()) != EOF )

{

/* Inside the loop, for each character, you must output it */

putchar(ch);

}

And make sure to remember to end your main function with
return 0;

Here's the complete program:

#include <stdio.h>

int main(void)
{
int ch;
while((ch = getchar()) != EOF)
{
putchar(ch);
}
return 0;
}

Now think about how to modify this basic copying program, to remove any
spaces from the end of lines.

You can test whether you have read a space, a newline character, or some
other type like this:

if(ch == ' ')
{
/* do something */
}
else if(ch == '\n')
{
/* do something else */
}
else
{
/* do something different */
}

There is a tricky part though, as you will be reading the space
characters before the newline comes in. You don't know how many space
characters will come in. You might read 10 of them and then find there
is a non-space character. In that case the 10 spaces you read were not
at the end of the line but in the middle, and they must be preserved in
the output.

I would introduce a new variable, to keep track of how many spaces have
been read. When I read a space, I will just increment the counter, but
not actually output anything. When I read a newline, I will reset the
counter to zero but not output anything except the newline. But when I
read something that is neither a space nor a newline, I must print some
spaces, according to the current value of the counter. I will then reset
the counter and print the actual character that was read.

Can you implement this in C? You will need to add one or two new
variables and another loop inside.

Or perhaps you can think of an even better way to solve the problem?
 
C

CBFalconer

Spiros said:
Oh yeah , to make it more interesting the programme should
use a constant amount of memory regardless of how large
the lines are.

And just to make it more elegant, handle embedded tabs, assuming
tabstops every 8th position.

If anand doesn't learn to stop top-posting he will stop getting any
help here.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
F

Frederick Gotham

anand devarajan posted:
hi friends,
i need some one to frame me questions for to write programs,it
might be simple ones like prime no. prg but i need this type of
coaching for to develop my programming knowledge

How about this:

Write a function with the following signature:

void FindReplace(char *,char const *,char const *)

The first argument is a pointer to a modifiable null-terminated string
which is to be processed and altered. The second argument is a pointer to a
null-terminated string indicating which characters are to be searched for.
The third argument is a pointer to a null-terminated string specifying the
replacement characters. So for instance, if the function were invoked as
follows:

char str[] = "The man walked into the bar."

FindReplace(str,"aeiou","vwxyz");

, then "str" will be changed to: "Thw mvn wvlkwd xnty thw bvr."
 
J

Joe Wright

Ico said:
Here is one for you: write a program that adds proper capitalization and
interpunction to a usenet message.
And add a spell checker. 'interpunction'?
 
C

CBFalconer

Joe said:
And add a spell checker. 'interpunction'?

No problem. Interpunction refers to the act of transferring
punctures between pneumatic tires. :) This is different from
'extreme interpunction', which has to do with the final rites for
interpreters.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
C

Chris Torek

Here is one for you: write a program that adds proper capitalization and
Chuck Falconer wrote (in a message whose ID I failed to capture):
Interpunction refers to the act of transferring
punctures between pneumatic tires. :)

"Interpunction" is a perfectly cromulent word.

"Cromulent" is not (yet?) an accepted word in most dictionaries
(see <http://www.urbandictionary.com/define.php?term=Cromulent>),
but "interpunction" today might be the act of inserting the
interpunct, a sort of center decimal point ("&middot;" in HTML-ese),
that was used in Latin. See <http://en.wikipedia.org/wiki/Interpunct>.
This word-separating character was apparently originally called an
"interpunction", so it is now actually valid as both a noun and a verb.
 
I

Ico

Joe Wright said:
And add a spell checker. 'interpunction'?

I must admit I usually don't use spellcheckers, but I believe 'interpunction'
is a perfectly valid word ?

$ dict interpunction

1 definition found

From The Collaborative International Dictionary of English v.0.48 [gcide]:

Interpunction \In`ter*punc"tion\, n. [L. interpunctio, fr.
interpungere, interppunctum, to interpoint. See {Inter-}, and
{Point}.]
The insertion of points between words or sentences;
[1913 Webster]
 
M

Michael Mair

Ico said:
Joe Wright said:
And add a spell checker. 'interpunction'?

I must admit I usually don't use spellcheckers, but I believe 'interpunction'
is a perfectly valid word ?

$ dict interpunction

1 definition found

From The Collaborative International Dictionary of English v.0.48 [gcide]:

Interpunction \In`ter*punc"tion\, n. [L. interpunctio, fr.
interpungere, interppunctum, to interpoint. See {Inter-}, and
{Point}.]
The insertion of points between words or sentences;
[1913 Webster]

FWIW:
interpunction(Engl.) != Interpunktion(German) == punctuation(Engl.)
Maybe this is a similar case of "false friends" for you.

-Michael
 
I

Ico

Michael Mair said:
Ico said:
Joe Wright said:
Ico wrote:

hi friends,
i need some one to frame me questions for to write programs,it
might be simple ones like prime no. prg but i need this type of
coaching for to develop my programming knowledge

Here is one for you: write a program that adds proper capitalization and
interpunction to a usenet message.

And add a spell checker. 'interpunction'?

I must admit I usually don't use spellcheckers, but I believe 'interpunction'
is a perfectly valid word ?

$ dict interpunction

1 definition found

From The Collaborative International Dictionary of English v.0.48 [gcide]:

Interpunction \In`ter*punc"tion\, n. [L. interpunctio, fr.
interpungere, interppunctum, to interpoint. See {Inter-}, and
{Point}.]
The insertion of points between words or sentences;
[1913 Webster]

FWIW:
interpunction(Engl.) != Interpunktion(German) == punctuation(Engl.)
Maybe this is a similar case of "false friends" for you.

Yes, you're right. Germany is only next door here, so that's where my
mixup came from. Punctuation it is :)
 
E

Eric Sosman

CBFalconer said:
If anand doesn't learn to stop top-posting he will stop getting any
help here.

While it's true that anand top-posted in his only message
to this thread (the only one my news server sees, anyhow), I
feel that he should be forgiven. It is, after all, no easy
matter to avoid top-posting in a thread's first message.
 
J

jmcgill

Ico said:
$ dict interpunction

1 definition found

From The Collaborative International Dictionary of English v.0.48 [gcide]:

Interpunction \In`ter*punc"tion\, n. [L. interpunctio, fr.
interpungere, interppunctum, to interpoint. See {Inter-}, and
{Point}.]
The insertion of points between words or sentences;
[1913 Webster]

It's in a 1996 Webster's as well. I like the word, because I've been
frustrated before trying to describe it when doing various forms of text
processing ;-)
 
J

jmcgill

Chris said:
Chuck Falconer wrote (in a message whose ID I failed to capture):



"Cromulent" is not (yet?) an accepted word in most dictionaries
> but "interpunction" today might be the act of inserting the
interpunct, a sort of center decimal point ("&middot;" in HTML-ese),
that was used in Latin. See <http://en.wikipedia.org/wiki/Interpunct>.
This word-separating character was apparently originally called an
"interpunction", so it is now actually valid as both a noun and a verb.

The word "interpunction" long predates HTML.

The word "cromulent" is a humorous neologism from a popular animated TV
show.
 
C

Coos Haak

Op 25 Sep 2006 10:09:34 GMT schreef Ico:
Michael Mair said:
Ico said:
Ico wrote:

hi friends,
i need some one to frame me questions for to write programs,it
might be simple ones like prime no. prg but i need this type of
coaching for to develop my programming knowledge

Here is one for you: write a program that adds proper capitalization and
interpunction to a usenet message.

And add a spell checker. 'interpunction'?

I must admit I usually don't use spellcheckers, but I believe 'interpunction'
is a perfectly valid word ?

$ dict interpunction

1 definition found

From The Collaborative International Dictionary of English v.0.48 [gcide]:

Interpunction \In`ter*punc"tion\, n. [L. interpunctio, fr.
interpungere, interppunctum, to interpoint. See {Inter-}, and
{Point}.]
The insertion of points between words or sentences;
[1913 Webster]

FWIW:
interpunction(Engl.) != Interpunktion(German) == punctuation(Engl.)
Maybe this is a similar case of "false friends" for you.

Yes, you're right. Germany is only next door here, so that's where my
mixup came from. Punctuation it is :)

But in Dutch it's interpunctie again :)
No word like punctuatie.
 
M

Mabden

I agree! ;-)

Eric Sosman said:
While it's true that anand top-posted in his only message
to this thread (the only one my news server sees, anyhow), I
feel that he should be forgiven. It is, after all, no easy
matter to avoid top-posting in a thread's first message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top