/*
 * controls.go
 *-------------------------------------------------------
 * if, for, and switch is all you get
 * Jim Fawcett, CSE775 - Distributed Objects, Spring 2016
 *-------------------------------------------------------
 */
 package main
 
 import (
   "fmt"
   "strings"
   "fawcett/utils"
 )
 
 func display1(s string) {
   fmt.Print("\n  ")
   for i := 0; i < len(s); i++ {  // classic for loop
     fmt.Printf("%c", s[i])
   }
 }

 func display2(s string) {
   fmt.Print("\n  ")
   for _, ch := range s {    // range returns index and value at index then increments
     fmt.Printf("%c", ch)    // we are ignoring index by using _ receiver
   }
 }

 func display3(s string) {
   fmt.Print("\n  ")
   i := 0
   for {                     // like while(true) { ... }
     fmt.Printf("%c", s[i])
	 i++
	 if i == len(s) {        // if statements must have scope braces
	   break
	 }
   }
 }
 
 func countWords(s string) (theWords, forWords, toWords int) {
   words := strings.Split(s, " ")
   for i:=0; i<len(words); i++ {
     switch(words[i]) {
	 case "the":
	   theWords++
	  case "for":
	    forWords++
	  case "to":
	    toWords++
	  default:
	 }
   }
   return theWords, forWords, toWords 
 }
 
 func main() {
   utils.TitleM("Demonstrating for, if, and switch controls")
   s := "now is the hour for all good men to come to the aid of their country"
   display1(s)
   display2(s)
   display3(s[4:15])
   theWords, forWords, toWords := countWords(s)
   fmt.Printf("\n  the words: %v, for words: %v, to words: %v", theWords, forWords, toWords)
   fmt.Println()
 }
 
