/* String Demo3
Brian Patton
A quick tour of some of string manipulation tools
*/
/* SKIF.ino
c.d.odom 10.19.14
a sketch that houses a variety of homemade functions to grab keyboard input
NB: No setup(), no loop()
*/
String words; // hold the intial large string to be parsed
int num; // hold the total number of characters
int counter; // hold the number of spaces
String myWords[50]; // Array to hold the individual words after they are parsed out
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();
toUp(); // intro to toUpperCase()
dramaticPause();
howLong(); // intro to length()
dramaticPause();
howManySpaces(); // intro to charAt() to search a string
dramaticPause();
catchWords(); //Capture all the words
}
//******************************* 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(500);
}
Serial.println("\n");
}
//******************************* Use the SKIF to get a string
void grabString() {
Serial.println("Please enter a sentence.");
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("\"");
}
//******************************* 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 ");
}
//******************************* Using charAt() to search a string
void howManySpaces() {
counter = 0;
for (int i = 0; i <= num - 1; i++) {
if (words.charAt(i) == ' ') {
counter++;
}
}
Serial.println("There are " + String(counter) + " spaces\'s " + " in \"" + words + "\" ");
Serial.println("That would imply there are " + String(counter + 1) + " words\'s " + " in \"" + words + "\" ");
}
//*******************************
void catchWords() {
Serial.println("I will next capture each word in your sentence and place it in the array");
int wordsIndex = 0;
int i;
int wEnd;
int wStart = -1; //I set to -1 since I want to start 1 past blank space but there
//is not black space at the beginning so....move back an extra space
for (i = 0; i <= num - 1; i++) {
if (words.charAt(i) == ' ') { // Find the next blank space
wEnd = i; // this will be the end of the word
myWords[wordsIndex] = words.substring(wStart + 1, wEnd);// capture word
wordsIndex++; //increase the index
wStart = wEnd; // set the next start to the previous end
}
}
myWords[wordsIndex] = words.substring(wEnd + 1);// last word does not need an end
// It just goes unitil the string ends
for (i = 0; i <= counter; i++) {
Serial.print("At index position " + String(i) + " you will find --> " );
Serial.println(myWords[i]);
}
Serial.println("\n " );
}
/* SKIF.ino
c.d.odom 10.19.14
a sketch that houses a variety of homemade functions to grab keyboard input
NB: No setup(), no loop()
*/
byte readByte() {
while (Serial.available() == 0); // pause indefinately until the serial port has data.
byte getByte = Serial.read(); // grab a byte (character) stored in the serial port buffer
// Serial.print("Character = " );
// Serial.write(getByte); // writes the binary data to the Serial Monitor
// Serial.println(", ASCII = " + String(getByte)); // writes the ASCII code of the binary data
// Serial.println("");
return getByte;
}
char readChar() {
while (Serial.available() == 0); // pause indefinately until the serial port has data.
char getChar = Serial.read(); // grab a character (byte) stored in the serial port buffer
// Serial.println("Character = " + String(getChar));
// Serial.println("");
// clean up the keyboard buffer in case user enters more than one character:
while (Serial.available() > 0)
{
char junk = Serial.read() ; // empty the keyboard buffer one character at a time
}
return getChar;
}
String readString() {
String buildString = "";
while (Serial.available() == 0); // pause indefinately until the serial port has data.
// loop while there are bytes in the serial port
while (Serial.available() > 0) {
char getChar = Serial.read(); // grab a character (byte) stored in the serial port buffer
if (getChar == '\n') { // if the character is a carriage return (ENTER) ...
// ... do nothing ... the input textbox is now empty
}
else {
buildString += getChar; // build the string one character at a time
}
}
return buildString;
}
int readInt() {
while (Serial.available() == 0); // pause indefinately until the serial port has data.
int getInt = Serial.parseInt(); // grab an integer from th the serial port buffer
// note delay with parseInt! try entering a letter
// Serial.println("Integer Value = " + String(getInt));
// Serial.println("");
// parse sometimes leaves junk characters, so this cleans up the keyboard buffer:
while (Serial.available() > 0)
{
char junk = Serial.read() ; // empty the keyboard buffer one character at a time
}
return getInt;
}
float readFloat() {
// NB: not accurate for large numbers. For more precision, use snprintf
// NB: does not recognize scientific notation
while (Serial.available() == 0); // pause indefinately until the serial port has data.
float getFloat = Serial.parseFloat(); // grab a floating number from the serial port buffer
// note delay with parseFloat! try entering a letter
// Serial.println("Float Value = " + String(getFloat)); // if you need more decimals, use snprintf.
// Serial.println("");
// Serial.println("");
// parse sometimes leaves junk characters, so this cleans up the keyboard buffer:
while (Serial.available() > 0)
{
char junk = Serial.read() ; // empty the keyboard buffer one character at a time
}
return getFloat;
}