/**********************************************
 * STRINGS - Demonstrate string.h02 functions *
 **********************************************/
 
//Specify System Header using -H option
#include <stddef.h02>
#include <stdlib.h02>
#include <stdio.h02>
#include <string.h02>
#include <stringx.h02>

char slen, scmp, spos;
const char s1  = "FIRST STRING.";
const char s2  = "SECOND STRING.";
const char stest = "123456789012345678901234567890";
char stemp[32];
const char smore = "THIS STRING IS MORE.";
const char sless = "THIS STRING IS LESS.";
char sprt[4];
const char pass = "PASS";
const char fail = "FAIL";

void anykey() {
  putln("*PRESS A KEY*");
  if (getkey() == #ESCKEY) goto exit;
  newlin();
}

main:
  //Demo strchr(), strrch()
  putln(&s1);               //Print s1 to screen
  puts("POSITION OF FIRST 'S' IS: ");
  spos = strchr('S', &s1);  //Get position of 's'
  ctoa(spos, &sprt);        //Convert to string
  putln(&sprt);             //Print position to screen
  puts("POSITION OF FIRST 'X' IS: ");
  spos = strchr('X', &s1);  //Get position of 's'
  ctoa(spos, &sprt);        //Convert to string
  putln(&sprt);             //Print position to screen
  puts("POSITION OF LAST 'S' IS: ");
  spos = strrch('S', &s1);  //Get position of 's'
  ctoa(spos, &sprt);        //Convert to string
  putln(&sprt);             //Print position to screen
  puts("POSITION OF LAST 'X' IS: ");
  spos = strrch('X', &s1);  //Get position of 's'
  ctoa(spos, &sprt);        //Convert to string
  putln(&sprt);             //Print position to screen
  newlin();

  //Demo strlen();  
  putln(&stest);         //Print stest to screen
  slen = strlen(&stest); //Get string length
  ctoa(slen, &sprt);     //Convert to string
  puts("LENGTH OF STRING IS ");
  putln(&sprt);          //Print length to screen
  newlin();

  //Demo setstr();
  strdst(&stemp);         //Set stemp as destination street
  puts("ADDRESS OF STEMP IS $");
  prbyte(dsthi);          //Print stored address to string
  prbyte(dstlo);
  newlin();
  newlin();

  anykey();

  //Demo strcpy()
  putln("COPYING S1 TO STEMP: ");
  strdst(&stemp);               //Set Destination String
  slen = strcpy(&s1);           //Copy s1 into Destination String
  puts("STRING STEMP CONTAINS: ");
  putln(&stemp);                //Print stest to screen
  ctoa(slen, &sprt);            //Convert to string
  puts("TOTAL CHARACTERS COPIED: ");
  putln(&sprt);                 //Print length to screen
  newlin();
  
  //Demo strcat()
  putln("CONCATENATING S2 TO STEMP: ");
  strdst(&stemp);               //Set Destination String
  slen = strcat(&s2);           //Copy s1 into Destination String
  putln("STRING STEMP CONTAINS: ");
  puts("  "); putln(&stemp);    //Print stest to screen
  ctoa(slen, &sprt);            //Convert to string
  puts("LENGTH OF CONCATENATED STRING IS ");
  putln(&sprt);                 //Print length to screen
  newlin();

  //Demo strcut()
  putln("CUTTING S2 AT POSITION 7 TO STEMP: ");
  strdst(&stemp);         //Set Destination String
  slen = strcut(7, &s2);     //Copy s1 into Destination String
  puts("STRING STEMP CONTAINS: ");
  putln(&stemp);          //Print stest to screen
  ctoa(slen, &sprt);      //Convert to string
  puts("LENGTH OF CUT STRING IS ");
  putln(&sprt);           //Print length to screen
  newlin();

  anykey();

  //Demo strcmp();
  puts("COMPARING SLESS TO SMORE: ");
  strdst(&sless);
  scmp = strcmp(&smore);  
  prbyte(scmp); newlin();
  puts("COMPARING SLESS TO SLESS: ");
  strdst(&sless);
  scmp = strcmp(&sless);  
  prbyte(scmp); newlin();
  puts("COMPARING SMORE TO SLESS: ");
  strdst(&smore);
  scmp = strcmp(&sless);
  prbyte(scmp); newlin();
  newlin();

  //Demo strspn();
  putln(&stest);         //Print test to screen
  puts("SPAN MATCHING \"0123\" IS: ");
  strdst(&stest);        //Set string to search
  slen = strspn("0123"); //Get position of "123"
  ctoa(slen, &sprt);     //Convert to string
  putln(&sprt);          //Print position to screen
  puts("SPAN MATCHING \"0123456789\" IS: ");
  strdst(&stest);        //Set string to search
  slen = strspn("0123456789");  //Get position of "123"
  ctoa(slen, &sprt);     //Convert to string
  putln(&sprt);          //Print position to screen
  puts("SPAN MATCHING \"789\" IS: ");
  strdst(&stest);        //Set string to search
  slen = strspn("789");  //Get position of "123"
  ctoa(slen, &sprt);     //Convert to string
  putln(&sprt);          //Print position to screen
  newlin();    

  //Demo strstr();
  putln(&stest);         //Print test to screen
  puts("POSITION OF \"234\" IS: ");
  strdst(&stest);        //Set string to search
  spos = strstr("234");  //Get position of "123"
  ctoa(spos, &sprt);     //Convert to string
  putln(&sprt);          //Print position to screen
  newlin();    

  newlin();

goto exit;

