Newbie needs help with something simple! 
Author Message
 Newbie needs help with something simple!

I'm simply trying to figure out how to write a program that works in
this format:

Please enter a letter: a

Result: apple
...
and if "b" was entered a word starting with "b" would be returned.
Can you help me? Thanks. - Cyrid



Fri, 06 Apr 2001 03:00:00 GMT  
 Newbie needs help with something simple!
There's a lot of ways to do what your asking.  Here is the simplest way I
can think of.
  #include <stdio.h>

       int main ()
        {
          char input;  /*defind character variable input*/

          input =getchar(); /* read one character from keyboard */
          if (input =='a') printf("apple \n");
          if (input =='b') printf("book \n");
              return 0;
        }


Quote:
> I'm simply trying to figure out how to write a program that works in
> this format:

> Please enter a letter: a

> Result: apple
> ...
> and if "b" was entered a word starting with "b" would be returned.
> Can you help me? Thanks. - Cyrid



Sat, 07 Apr 2001 03:00:00 GMT  
 Newbie needs help with something simple!
This looks like homework, so I won't give you all the answers, but I'll give
you some hints: the first character in a string is accessible as *string, if
you want something generic (e.g. given "mac" it will look for "mac*") look
at strncmp().

Russell

Hello wrote ...
...

Quote:
>Please enter a letter: a

>Result: apple
>...
>and if "b" was entered a word starting with "b" would be returned.

...


Sat, 07 Apr 2001 03:00:00 GMT  
 Newbie needs help with something simple!

Quote:

> I'm simply trying to figure out how to write a program that works in
> this format:

> Please enter a letter: a

> Result: apple
> ...
> and if "b" was entered a word starting with "b" would be returned.
> Can you help me? Thanks. - Cyrid

Three methods come to mind:
1. Use many "if" statements such as:
    if (letter == 'a') printf("apple");

2. Use a "switch" statement:
    switch (letter)
    {
      case 'a':
        printf("apple");
        break;
      case 'b':
        printf("book");
        break;
      /* ... */
    }

3. Use an array of strings, index by the letter input:
   char * dictionary[26] = { "apple", "book", "cat", /*...*/ };

   printf(dictionary[letter - 'a']);

Note that you have to safe-guard against any invalid characters being entered.
For example, what happens if the user presses '6' or '#'?  Also think about a
method for the user to nicely quit the program.  Be aware that 'a' is not equal
to 'A', meaning the language and input are case sensitive.

Look up the following functions or headers:
toupper()
strcmp()
fgetc()
fgets()
string.h
ctype.h

There are more methods, but many are advanced.  Just think about how you would
do this using pen & paper.  Write down each step, then convert into a program.

--
Thomas Matthews



Sat, 07 Apr 2001 03:00:00 GMT  
 Newbie needs help with something simple!
Thanks for the feedback guys. And no Harper it wasn't a homework
assignment. My class hasn't even got passed "scanf" yet (class is moving
way to slow). That stuff I already know, so I was just trying to figure
out some new things on my own. Again, thanks. I learn more here than in
that class.   - Cyrid


Sun, 08 Apr 2001 03:00:00 GMT  
 Newbie needs help with something simple!
Groovy hepcat Rick49 was jivin' on Tue, 20 Oct 1998 01:52:34 GMT in
comp.lang.c.
Re: Newbie needs help with something simple!'s a cool scene! Dig it!

Quote:
>      if (input =='a') printf("apple \n");
>      if (input =='b') printf("book \n");

  A much easier way than this would use a switch here instead of
multiple if(...) statements.
  But a lokup table would be cleaner. For example (assuming a system
in which the letters a to z are contiguous and in order in the
prevailing character set, eg. ASCII):

#include <stdio.h>
#include <ctype.h>

int main(void)
{
  char *word[] = {"Apple",
                  "Book",
                  ... };
  int c;

  printf("Enter a letter: ");
  fflush(stdout);

  c = tolower(getchar()) - 'a';
  printf("%s\n", word[c]);

  return 0;

Quote:
}

--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Thu, 12 Apr 2001 02:00:00 GMT  
 Newbie needs help with something simple!

Quote:

> Groovy hepcat Rick49 was jivin' on Tue, 20 Oct 1998 01:52:34 GMT in
> comp.lang.c.
> Re: Newbie needs help with something simple!'s a cool scene! Dig it!

> >         if (input =='a') printf("apple \n");
> >         if (input =='b') printf("book \n");

>   A much easier way than this would use a switch here instead of
> multiple if(...) statements.
>   But a lokup table would be cleaner. For example (assuming a system
> in which the letters a to z are contiguous and in order in the
> prevailing character set, eg. ASCII):

> #include <stdio.h>
> #include <ctype.h>

> int main(void)
> {
>   char *word[] = {"Apple",
>                   "Book",
>                   ... };
>   int c;

>   printf("Enter a letter: ");
>   fflush(stdout);

>   c = tolower(getchar()) - 'a';

A minor nitpick: this assumes a character set where the letters
have adjacent codes in ascending order. A switch would not suffer
from this limitation:
   switch( tolower( getchar() ) )
   {
       case 'a': printf( "Apple\n" ); break;
       case 'b': printf( "Banana\n" ); break;
   }

Quote:
>   printf("%s\n", word[c]);

Hidden danger: this results in undefined behaviour, if the user
enters a leter that is not in the lookup table. The "if" and "swicth"
versions do not suffer from this potential problem.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Fri, 13 Apr 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Newbie needs help on simple problem

2. Newbie needs urgent help, simple question

3. Something simple -Help

4. help me i need something

5. Need help on something.

6. Newbie in need: Simple Application Design

7. NewBie in C needs help - C simple questions(not for me) help help help help!

8. Help C newbie need help please

9. Help C newbie need help please

10. HELP -- NEWBIE NEEDS HELP

11. Help for C newbie with simple stat() program.

12. Something Seemingly Simple.

 

 
Powered by phpBB® Forum Software