% strings.m
clear all;

% A string is entered by a sequence of characters enclosed 
% by a pair of single quaotes:
PresidentName = 'Obama'

% Blanks are allowed in a string:
SenatorNYC = 'Hilary Rodham Clinton',

% Digits are allowed also:
Company = 'Century 21',

% Punctuation marks and symbols on the keyboard are allowed:
Sentence = 'What the #\@*~!'

% To have a single quote in string type two single quotes:
'The time is 5 o'' clock.'

% Since a single string is stored as a row array in MATLAB,
% all array operations are allowed:
LastName = SenatorNYC(15:end)

% Making fun of Hilary's middle name:
SenatorNYC(10) = 't'; disp(SenatorNYC);

% Reversing the president's name:
PresidentName(end:-1:1)

% Concatenating strings horizontally using the square bracket notation:
Together = [ PresidentName ' and ' LastName ]

% Making fun of someone's name:
Name = 'Schmidt'; Name(2:2:end) = []; disp(Name);

% The following displays identical output for x and y on the screen,
% but x and y are very different entities,
% x is a single number while y is a string 
% (a row vector containing the digits 1, 2 and 3):
x = 123
y = '123'
% When numerical operations are performed on a string,
% each character is first converted to their ASCII values,
% then the numerical operations are performed on those values.
% This results in a row of doubles.
% For example:
Vec = x - y
% gives [ 123-49 123-50 123 - 51 ] = [ 74 73 72 ]

Indices = findstr('The clothes they wear are theirs.','the')

DEMOCRATES = char(PresidentName,LastName,'Gore')

% Get the ASCII codes for a string using the function double 
% (which cast the contents to a vector of doubles):
double(PresidentName)

% Conversely, function char returns a string from the ASCII codes:
char(65:70)
% returns the string ABCDEF
% If a string is used in a numerical calculation, then the calculation
% is performed on the ASCII values of the elements of the string
% otaining a row of numbers. For example,
Result = Company + 1
% The resulin vector can be cast back to a string, for example
char(PresidentName+1)
% gives the same as result as char(double(PresidentName)+1)
% and returns the string Pcbnb

% Comparing two strings having the same length results
% in a logical vector obtained by comparing the ASCII codes of their
% corresponding elements:
s1 = 'ann'
s2 = 'ban'
s1 < s2
% returns the logical vector: 1 0 0
% You cannot compare two strings this way if they do not have the same
% length.
% Similarly Comparing two strings having the same length results
% in a logical vector obtained by comparing the ASCII codes of their
% corresponding elements. For example
s1 == s2
% returns the logical vector: 0 0 1
% Special functions on strings can be used for strings having different
% lengths.

% The function strcmp compares two strings for equality.
% Two strings are considered equal if they have the same length
% and their corresponding elements are all equal. As an example,
strcmp(PresidentName,LastName)
%returns a value of false. 

%The function findstr (which stands for find string) 
% finds all the locations of a substring in a given string.
Indices = findstr('The clothes they wear are theirs.','the'),
%gives: Indices = [ 8 13 27 ]

% Function toupper converts lowercase letters to upper cases:
NewString = upper(PresidentName)
% gives NewString = 'OBAMA'
% Characters already in upper cases or characteros having no upper 
% case versions are not affected.

% MATLAB has many other string functions that a text-editor has,
% such as find-and-replace, and inserting a substring into another string.
% Type help strfun to see a list of built-in string functions.