In Get Values from Terminal, numbers were entered into the SimpleIDE Terminal and stored in variables. In case you were wondering if something similar can be done with text, the answer is yes.
A sequence of characters is commonly referred to as a character string, and it is often shortened to just string or strings when referring to more than one. Strings are typically stored in character arrays, and the addresses of the first elements of arrays that contain strings are used for both getting text from the SimpleIDE Terminal and also displaying it.
In addition to entering and displaying strings this activity will introduce one of the many C standard library string handling features, for making comparisons with a password checker.
If you haven't already installed the latest USB driver, SimpleIDE, or Learn folder, go to Propeller C - Set up SimpleIDE and Propeller C - Start Simple.
Test Code
- Click SimpleIDE’s New Project button.
- Navigate to …Documents/SimpleIDE/Learn/
- Click the New Folder button and add a folder named C Intermediate.
- Enter the Get Text.c code below.
- Set the power switch to position 1 if it isn't already (if applicable for your board).
- Click the Run with Terminal button.
- Type some text into SimpleIDE Terminal, then press Enter.
- Verify that the message “You typed: “ followed by what you typed appears on the next line.
/* Get Text.c */ #include "simpletools.h" // Include simpletools library char text[16]; // Declare character array int main() // Main function { print("Enter some text: "); // User prompt getStr(text, 15); // Tet string, max 15 characters print("You typed: %s \n", text); // Display the characters }
How it Works
This code declares a char (short for character) array with 16 elements. That’s 16 byte elements, each of which can contain a value in the 0 to 255 range. The values that correspond with printed characters will all be in the 32 to 126 range. For more info, see ASCII Table 0-127.
char text[16];
After a user prompt to type some characters and press Enter, the getStr(text, 15) function passes the text array’s starting address, and limits the total characters entered to 15. If you want to enter fewer than 15 characters, make sure to press Enter to terminate the getStr function. The getStr function will also self-terminate when you type the 15th character.
print("Enter some text: "); getStr(text, 15);
Why does getStr stop at 15 characters and not 16? Given the size of the text array, you might be wondering why getStr limits the characters entered to 15 and not 16. It’s because C language strings are zero-terminated, meaning there has to be a zero after the last character. The getStr function automatically appends the text you’ve typed with a zero. So, if there’s a char array with 16 elements, there’s enough room for 15 characters and one zero-terminator.
After typing the text, this print function then displays it.. The formatting string has %s indicating that it has to print a zero-terminated string. Since %s is the first formatting flag, the first argument has to have the starting address of an array that contains the characters. Remember from Variable Values and Addresses that the array’s variable name is used for passing the address of an array’s zeroth element to functions. So, text is the first argument after the format string.
print("You typed: %s \n", text);
Did You Know?
The three lines of code below all declare the same char array with 8 elements that stores the characters abc123, followed by a zero. A sequence of characters followed by a zero is commonly called a zero-terminated string. Even the first declaration, which uses quotes to define the array elements, automatically appends a zero to array[7]. So, there are seven characters, plus a zero terminator that’s built-in.
char pass[8] = {"abc 123"}; char pass[8] = {'a', 'b', 'c', ' ', '1', '2', '3', '\0'}; char pass[8] = {97, 98, 99, 32, 49, 50, 51, 0};
The simpletools library also includes the string standard C library. One of the functions in the string library is strcmp. The strcmp function takes the starting addresses of two zero-terminated strings stored in two character arrays, and returns a zero if they match, or nonzero if they do not match. Here is an example that prints “Same!” because the two strings match.
if(strcmp(pass, text) == 0) print("Same!"); else print("Different!");
The strcmp(pass, text) == 0 comparison returns 1 if it’s true, or 0 if it’s false. An if statement executes its code block if the comparison returns nonzero. The logical not ! operator changes zero to 1, and any nonzero value to 0. So, here is an equivalent statement in a style you are likely to see in published C language examples:
if(!strcmp(pass, text)) print("Same!"); else print("Different!");
If they match, strcmp returns 0, and the ! operator changes it to 1, so the if…else… statement will still print “Same!”. If they do not match, strcmp returns nonzero, which the ! operator changes to 0 and cause the if…else… statement to print “Different!”
Try This
Here is an example that uses zero-terminated strings, scan, and strcmp to make a simple password checker.
- Click SimpleIDE’s New Project button.
- Set the folder to …Documents\SimpleIDE\My Projects\
- Set the File name to Password Check.
- Copy the code below into SimpleIDE.
- Click the Run with Terminal button.
- Click to the right of “Enter password: “ in the SimpleIDE Terminal and type abc 123, then press Enter. Make sure to type a space between abc and 123.
- Verify that SimpleIDE Terminal displays the “(pass does match text)” message.
- Click Run with Terminal again, and this time, type in characters other than abc 123, and verify that it displays the “(pass DOES NOT match text)” message.
Your Turn
Many computer operating systems, automated teller machines, and other secure devices replace the password characters you type with asterisk * so that people nearby cannot see your password displayed on the screen. Although neither getStr nor scan have provisions for this, it’s not too difficult to write your own.
- Replace these two lines:
getStr(text, 15); print("You typed: %s \n", text);
with this:
This custom version of getStr behaves almost identically to what getStr actually does. To make it match the getStr function, simply replace putChar('*') with putChar(text[i]).