Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > to get more knowledge in 'c'

Reply
Thread Tools

to get more knowledge in 'c'

 
 
anand devarajan
Guest
Posts: n/a
 
      09-24-2006
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

 
Reply With Quote
 
 
 
 
Spiros Bousbouras
Guest
Posts: n/a
 
      09-24-2006
anand devarajan 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


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.

 
Reply With Quote
 
 
 
 
Spiros Bousbouras
Guest
Posts: n/a
 
      09-24-2006
Spiros Bousbouras wrote:

> anand devarajan 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

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

 
Reply With Quote
 
Ico
Guest
Posts: n/a
 
      09-24-2006
anand devarajan <(E-Mail Removed)> 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.

 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      09-24-2006
Spiros Bousbouras wrote:
> anand devarajan 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

>
> 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?

--
Simon.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-24-2006
Spiros Bousbouras wrote:
> Spiros Bousbouras wrote:
>> anand devarajan wrote:
>>
>>> 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.

>
> 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:
<news:news.announce.newusers
<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/>

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-24-2006
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."

--

Frederick Gotham
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      09-25-2006
Ico wrote:
> anand devarajan <(E-Mail Removed)> 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'?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-25-2006
Joe Wright wrote:
> Ico wrote:
>> anand devarajan <(E-Mail Removed)> wrote:
>>>
>>> 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'?


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:
<news:news.announce.newusers
<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/>


 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-25-2006
In article <(E-Mail Removed)>,
Joe Wright <(E-Mail Removed)> wrote:

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


>And add a spell checker. 'interpunction'?


"Interpunction" is a perfectly cromulent word.

-- Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need more knowledge about the memory! vib.cpp@gmail.com C++ 2 01-03-2009 06:47 AM
Imagination is more important than knowledge. worldforever.suji@gmail.com HTML 0 04-09-2008 08:53 AM
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM



Advertisments