# 14.4: Comparing Character Strings

• • Carey Smith
• Oxnard College
$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

Comparing strings is different than comparing numbering. Comparing strings requires specific functions. In this section, tf is a logical variable meaning true or false.

Consider two character string variables. Two common string comparison functions are:

tf = strcmp(str1, str2); % case sensitive
tf = strcmpi(str1, str2); % case insensitive

These functions look for a match between strings of the same length. The user can specify to compare just the first n characters.

Study the attached file: String_compare_demo.m

##### Exercise $$\PageIndex{1}$$ Compare names

Create a Matlab script that does the following:
%% A. [1 pt] Create this array of names:
names = ['Alex';
'Gabe';
'Joan';
'John';
'Mary';
'Pete';
'Rosa';
'Tina']

%% B. [1 pt] Ask the user to enter a 4-letter name with this code:
name_in = input('Enter a 4-letter name: ','s');

%% C. [1 pt] Use length() to determine the length of the names array.

%% D. [5 pts] Write a for loop with loop index k to search for match of name_in
% to one of those in the names list. Use the following to find the match:

tf = strcmpi(name_in, names(k,:)); % case insensitive comparison

% When a match is found, display the index of that name in the list and break the loop
% Use disp() with num2str() to display the index with this code:
disp([name_in,' is number ',num2str(k),' in the list.'])

Extra Credit (2 pts) If a name is entered that is not in the list, figure out a way to send a message that the name is not in the list.

Are are 2 possible strategies:

Strategy A. Set a variable called valid_name = 0 before the loop.

Inside the loop, when a match is found, set valid_name = 1.

After the loop, if valid_name == 0, display a message that the name was not found.

Strategy B. Add an invalid name to the list, such as 'xxxx'. So, the length of the array will be 1 more than the number of valid names.

After the loop, if the loop index == the length of the array, than display a message that the name was not found.

The answer is not shown here.

Other string functions are:

tf = strncmp(s1,s2,n): Compare first n characters of strings (case sensitive)

tf = strncmpi(s1,s2,n): Compare first n characters of strings (case insensitive)

tf = strfind(str,pat): Find strings within other strings

.

This page titled 14.4: Comparing Character Strings is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.