How can I write a method that will check if the letters in a string are all the same?

Answers (4)

George - use the unique function to find the unique characters in the string. If there is only one unique character then you know that all letters are the same
function [bAllSame] = allSameLetter(str)
bAllSame = length(unique(str))==1;
Using diff would be much faster than unique:
>> str = 'aba';
>> any(diff(+str))
ans = 1
>> str = 'aaa';
>> any(diff(+str))
ans = 0
First you have to show us your String class. MATLAB does not have a String class.
It looks to me that you are asking homework questions about a different programming language.

Categories

Tags

No tags entered yet.

Asked:

on 2 Dec 2015

Answered:

on 2 Dec 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!