190 likes | 329 Views
Strings Built-In Functions. Comparing Strings Converting strings/numbers Additional String Functions. 1. Comparing Strings. Curious: What would using == do between strings? >> 'hi' == 'by' <enter> ans = ___________________ >> 'HI' == 'hi' < enter> ans = ___________________
E N D
StringsBuilt-In Functions Comparing Strings Converting strings/numbers Additional String Functions
1. Comparing Strings • Curious: What would using == do between strings? >> 'hi' == 'by' <enter> ans = ___________________ >> 'HI' == 'hi' <enter> ans= ___________________ >> 'yes' == 'no' <enter> ans= ___________________ >> 'word1'== 'answer1'<enter> ans = ___________________
1. Comparing Strings • Curious: What would using == do? • MATLAB evaluates equality letter by letter, assigning a 0 (for false) or a 1 (for true) • THIS ONLY WORKS FOR STRINGS OF IDENTICAL LENGTHS!!! • so it can't possibly be the way to go in 99% of the cases…
1. Comparing Strings, cont. • Two built-in functions are commonly used to compare strings: • strcmp()– STRingCoMPare returns true if the two arguments (both strings) are identical (CaSesEnSItiVE) Practice: strcmp('hi', 'hi') evaluates to ______ Practice: strcmp('HI', 'hi') evaluates to ______ Practice: str = 'yes'; strcmp(str, ‘yes') evaluates to _____ strcmp(‘yes',str) evaluates to _____
1. Comparing Strings, cont. • Two built-in functions are commonly used to compare strings: • strcmpi()– STRingCoMPare Insensitive returns true if the two arguments (both strings) are identical WITHOUT REGARD TO CASE Practice: strcmpi('hi', 'hi') evaluates to ______ Practice: strcmpi('HI', 'hi') evaluates to ______ Practice: str = 'yes'; strcmpi(str, 'Yes') evaluates to _____ strcmpi('YeS',str) evaluates to _____
Example: Access Granted % ask for username username = input('Enter username: ', 's'); if %correct username % ask for a passwd if %correct password grant access… else quit/end code end else % quit/end code end if time..
Example: Access Granted % ask for username username = input('Enter username: ', 's'); ifstrcmpi(username, 'John') % correct username %ask passwd pass = input('Enter password: ', 's'); ifstrcmp(pass, 'u23!9s2')%if correct password %grant access... else %quit/end code... end else % quit/end code... end The user name may not be case-sensitive… …but a password is usually case-sensitive this should probably be changed to a loop…
Again… BAD!!! GOOD!!! fprintf('Blindfolded. Type two words (hit enter in between each)!\n'); %gettwowords w1 = input('Enter word1: '); w2 = input('Enter word2: '); ifstrcmp(w1,w2) == 1 %equalwords fprintf('Yay!!! Identical words!\n'); else fprintf('Booo.... unidentical\n'); end fprintf('Blindfolded. Type two words (hit enter in between each)!\n'); %gettwowords w1 = input('Enter word1: '); w2 = input('Enter word2: '); if w1 == w2 %equalwords fprintf('Yay!!! Identical words!\n'); else fprintf('Booo.... unidentical\n'); end the == 1 is not necessary. if 1 in itself means if true!
2. Converting strings • Convert: string numbers str2num() str2double() [will convert an entire cell arrays of strings] • Convert: number string int2str() num2str()
Example: Validating input • “What happens when the user enters letters (instead of numbers)?”
Example: Validating input • To solve this problem, every input must now be considered as a string, even if they are numbers! • Algorithm % Grab user's input as a string % Use str2double() to convert to numbers % while isnan() is true % grab again, convert again % Continue with calculations if it did if time..
Example: Validating input • What does str2double() and isnan() do? • Example when string does look like a number:
Example: Validating input • What does str2double() and isnan() do? • Example when string has an ERROR:
Example: Validating input • Now, prompt the user for a value, then put the str2double()and isnan()in a loop! EXTREMELY IMPORTANT: isnan()MUST BE THE FIRST CONDITION.
Get Bounded Integer • Write a function that accepts a prompt, a lower limit, and an upper limit and validate the input, re-prompting until the user enters a valid number. • Algorithm: % Print prompt and collect input as a string % Identify if input is a number % Identify if input is between lower and upper limits % As long as input is invalid, print an error message and re-prompt
Main code clc clear %prompt age age = getBoundedInt('Enter your age (1-100): ',1,100); %prompt how many kids nbKids = getBoundedInt('\nHow many kids do you have (1-30): ',1,30); %prompt how many house you own nbHouses = getBoundedInt('\nHow many houses do you own (1-5): ',1,5); … huh? no loop???
ReUsable Function!! functionValidIntNum = getBoundedInt( Prompt, LowerLimit, UpperLimit ) % GETBOUNDEDINT % ValidIntNum= getBoundedInt( Prompt, LowerLimit, UpperLimit); % Prompts for an integer between 2 values. Returns a valid integer. %Print prompt and collect input as a string ValidIntStr= input(Prompt, 's'); ValidIntNum = str2double(ValidIntStr); %convert string to double %Identify if input is a number, is between lower and upper limits, is whole whileisnan(ValidIntNum) || ValidIntNum<LowerLimit|| ValidIntNum>UpperLimit || … floor(ValidIntNum) ~= ValidIntNum %print an error message and re-prompt fprintf('Enter a valid integer between %d and %d\n', LowerLimit, UpperLimit ); ValidIntStr= input(Prompt, 's'); ValidIntNum= str2double(ValidIntStr); %convert string to double end
3. Additional String Functions • lower() – converts a string to lowercase • upper() – converts a string to uppercase • isletter() – which characters in the string are letters?