#include <iostream.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>


void usage(char *s)
{
  printf("usage: %s -s string to parse [-p echo input] \n%s -p -s \"this is my string\" \n",s,s);
}

int main (int argc, char *argv[])
{

  int i;              // loop counter
  int count;          // token count
  char *token[10];    // hold tokens
  char *str=NULL;     // ASSUME NO INPUT

  int c;              // getopt count
  int print_line= 0;  // don't echo the line


  /* get command line opts */ 
  while ((c = getopt(argc, argv, "ps:")) != EOF)
    {
      switch (c)
        {
	case 's': str = optarg; break;  // assign input string to str
	
        case 'p': print_line = 1; break; // echo the line

	default:
	  usage(argv[0]);
	  exit(1);
        }
    }


if(str)  // if we got a string
 {
  count = 0; // count starts at 0
             // parse for string with " " delimitter  
  while ( (token[count] = strtok( ((count == 0) ? str : NULL), " ")) )
      ++count;

  if(print_line == 1) // if we want to echo the string
   {
     for(i=0; i < count; ++i)
       printf("token is: %s\n", token[i]);
   }
  }
 
else
  {
    usage(argv[0]);
  }


}

