/* String Demo
Brian Patton
A quick tour of some of string manipulation tools
*/
String words;
String word1, word2, word3, word4;
int num;
int space1;
int space2;
int space3;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
}
void loop() {
// put your main code here, to run repeatedly:
grabString();
dramaticPause();
trimIt(); // intro to trim()
dramaticPause();
toLow(); // intro to toLowerCase()
dramaticPause();
toUp(); // intro to toUpperCase()
dramaticPause();
howLong(); // intro to length()
dramaticPause();
howFar(); // intro to indexOf()
dramaticPause();
firstLetter(); // intro to charAt()
dramaticPause();
wordChunks(); // intro to substring()
dramaticPause();
howMany(); // intro to charAt() to search a string
dramaticPause();
}
//******************************* Just a fun pause
void dramaticPause() { // just a cool delay so there is time to read the text at each step
for (int i = 0; i <= 10; i++) {
Serial.print("*");
delay(400);
}
Serial.println("\n");
}
//******************************* Use the SKIF to get a string
void grabString() {
Serial.println("Please enter a four word sentence. For example \"The fox ran fast\"");
Serial.println("Feel free to try and trick me with a few leading or trailing spaces");
words = readString();
Serial.print("You have given me \"");
Serial.print(words);
Serial.println("\"");
}
//******************************* trim();
void trimIt() {
Serial.println("First I'll get rid of any stray spaces at the beginning or end");
words = words.trim();
Serial.print("This should be free of leading and trailing spaces ------> \"");
Serial.print(words);
Serial.println("\"");
}
//******************************* toUpperCase();
void toUp() {
Serial.println("Now I'll switch all the letters to Uppercase");
words = words.toUpperCase();
Serial.print("This should be all Upper case ------> \"");
Serial.print(words);
Serial.println("\"");
}
//******************************* toLowerCase();
void toLow() {
Serial.println("Now I'll switch all the letters to Lowercase");
words = words.toLowerCase();
Serial.print("This should be all lower case ------> \"");
Serial.print(words);
Serial.println("\"");
}
//******************************* length()
void howLong() {
Serial.println("Now I will calculate the total number of characters and spaces in your string");
num = words.length();
Serial.print(words);
Serial.println(" Has " + String(num) + " characters and spaces ");
}
//******************************* indexOf()
void howFar() {
Serial.println("I will next tell you the location of each of the spaces between the words in your sentence");
space1 = words.indexOf(" ");
Serial.print(words);
Serial.println(" Has it's first space after " + String(space1) + " positions from the beginning of the sentence");
space2 = words.indexOf(" ", space1 + 1);
Serial.print(words);
Serial.println(" Has it's second space after " + String(space2) + " positions from the beginning of the sentence");
space3 = words.indexOf(" ", space2 + 1 );
Serial.print(words);
Serial.println(" Has it's third space after " + String(space3) + " positions from the beginning of the sentence");
}
//******************************* charAt()
void firstLetter() {
Serial.println("I will next tell the first letter of each word in your sentence");
String let1 = words.charAt(0);
Serial.println("The first letter of the first word is \"" + let1 + "\"");
String let2 = words.charAt(space1 + 1);
Serial.println("The second letter of the first word is \"" + let2 + "\"");
String let3 = words.charAt(space2 + 1);
Serial.println("The third letter of the first word is \"" + let3 + "\"");
String let4 = words.charAt(space3 + 1);
Serial.println("The fourth letter of the first word is \"" + let4 + "\"");
}
//******************************* substring()
void wordChunks() {
Serial.println("Now I will tell you each word and the length of each one in your sentence");
word1 = words.substring(0, space1);
Serial.println("The first word is \"" + word1 + "\" and has a length of " + String(word1.length()) + " characters");
word2 = words.substring(space1 + 1, space2);
Serial.println("The Second word is \"" + word2 + "\" and has a length of " + String(word2.length()) + " characters");
word3 = words.substring(space2 + 1, space3);
Serial.println("The third word is \"" + word3 + "\" and has a length of " + String(word3.length()) + " characters");
word4 = words.substring(space3 + 1);
Serial.println("The forth word is \"" + word4 + "\" and has a length of " + String(word4.length()) + " characters");
}
//******************************* Using charAt() to search a string
void howMany() {
int counter = 0;
Serial.println("Which character would you like me to count in \"" + words + "\" ");
String charOfInterest = readString();
charOfInterest.toUpperCase();
char Chr = charOfInterest.charAt(0);
for (int i = 0; i <= num - 1; i++) {
if (words.charAt(i) == Chr) {
counter++;
}
}
Serial.println("There are " + String(counter) + " " + String(Chr) + "\'s " + " in \"" + words + "\" ");
}