[Courses] Lesson 8: First task... A solution

Bidea Cristian phaser_programmer at yahoo.com
Sat Nov 9 04:37:41 EST 2002


This is the first task!
-----------------------------------------------------------------------
#include <stdio.h>
/*This is a word that can contain 255 letters*/
char string[255];
/* 
   A word can contain any letter from a..z and any
digit from 1..9.
   A word can't contain ".",";","!","?",",",",":"," "
etc.
 */
#define boolean int
#define true 1
#define false 0

/*
  Function isvalid test if a caracter is
[.],[;],[!]... etc.
  This is the first solution...
*/
boolean isvalid(char c)
{
  boolean is = true;
  switch (c)
  {
    case ' ': is = false ; break;
    case '(': is = false ; break;
    case ')': is = false ; break;
    case '{': is = false ; break;
    case '}': is = false ; break;
    case '[': is = false ; break;
    case ']': is = false ; break;
    case '.': is = false ; break;
    case ';': is = false ; break;
    case '!': is = false ; break;
    case '?': is = false ; break;
    case ',': is = false ; break;
    case ':': is = false ; break;
    case '\0': is = false ; break;
    case '\n': is = false ; break;
  }
  return is;
}

int main(void)
{
  int iCounter = 0; //Our counter
  boolean isstarted = false; //we use this variable to
know when we finished 
                             //reading a word         
           
  char c; //we use this to read characters
    
  printf("Enter the string please: ");
  do
    {
      scanf("%c", &c);
      if (isvalid(c) == true)
        {
          if (isstarted == false) 
            {  
              iCounter++;
              isstarted = true; //we start reading a
word
            }
        } 
      else
        {
          //if we are in the middle of reading a word
then 
          //we stop because we found a character that
is not 
          //valid (c != a..z && c != 1..9). 
          if (isstarted == true) isstarted = false;
        }
    } while ((c != '\0') && (c != '\n')); //our string
ends in "\0" 
                                          //or "\n" 
  printf("\nThere are %d words", iCounter);    
  return 0;
}
-------------------------------------------------------------------------
gcc -o task1 task1.c

Test #1:
-----------------------------------
Input: Please tell me what you got
Output: There are 6 words

Test #2:
-----------------------------------
Input: Please sir! Give me one dollar... My dad is not
home. My mother is at work,
       cooking donuts for people.
Output: There are 20 words

Test #3:
-----------------------------------
Input: Nobody... ?!?.., is (,,,,)[] something.
Output: There are 3 words.

OK! If we have this string: Maria + Boby = Love the
program will return
There are 5 words. And this is wrong because we have
only 3 words...
We can put + and = in our isvalid function but there
are a
lot of separators that we must put there so there must
be a better solution.

We know that any letter or digit is valid so we make a
isvalid function that
test for these.

boolean isvalid(char c)
{
  char i;
  
  for (i = 'a'; i<='z'; i++)
    if (c == i) return true; 
  for (i = '0'; i<='9'; i++)
    if (c == i) return true; 
  return false;
}

That's all for now! Bye... Peace!


__________________________________________________
Do you Yahoo!?
U2 on LAUNCH - Exclusive greatest hits videos
http://launch.yahoo.com/u2



More information about the Courses mailing list