IsPalindrome: Matlab Function to Check Word

  • Thread starter Moi
  • Start date
  • Tags
    Matlab
In summary, the conversation is about creating a function called ispalindrome in Matlab that checks if a word is a palindrome or not and returns a value of True or False. The person encountered an error while running the function and found out about the strcmpi function, which compares strings for equality, and tried using it instead. However, they realized that strcmpi does not work for checking palindromes and found information about the strcmp function.
  • #1
Moi
3
0
I am trying to write a function called ispalindrome, with Matlab, It is supposed to check if a word is a palindrom or not.
It will then return a value True (1)or False(0) for each word.

I have tried to run this, but something seems to be wrong...
function isPalindrome (word)
word = input('isPalindrome ', 's');

if word == strcmpi(word);
disp ('1')
else
disp ('0')
end
 
Physics news on Phys.org
  • #2
A google will tell you, that:

TF = strcmpi(string,string) compares two strings for equality, ignoring any differences in letter case. The strings are considered to be equal if the size and content of each are the same. The function returns a scalar logical 1 for equality, or scalar logical 0 for inequality.

Thus strcmpi('yes' , 'Yes') = true.

But 'yes' is not a palindrom. A palindrom is a word that is spelled the same way forward/backward, like 'refer' or 'bob'.
 
  • #3
I used 'strcmp' instead? But I still get Error :-)
 

What is the purpose of the IsPalindrome Matlab function?

The IsPalindrome Matlab function is designed to check whether a given word is a palindrome or not. A palindrome is a word or phrase that reads the same backward as it does forward.

How does the IsPalindrome function work?

The function takes in a string as its input and compares the first and last characters of the string. If they are the same, it then moves on to compare the second and second to last characters, and so on until it reaches the middle of the string. If all the characters match, the word is considered a palindrome. Otherwise, it is not.

What happens if I input a sentence instead of a single word?

If a sentence is inputted, the function will remove all spaces and punctuation before checking if it is a palindrome. This means that phrases such as "nurses run" or "A man, a plan, a canal, Panama!" will still be recognized as palindromes.

Are uppercase and lowercase letters treated differently by the IsPalindrome function?

No, the function is case-insensitive, meaning it will treat uppercase and lowercase letters as the same. This ensures that words such as "Racecar" and "racecar" are both recognized as palindromes.

Can the IsPalindrome function handle non-alphabetic characters?

Yes, the function is designed to handle non-alphabetic characters such as numbers and symbols. These characters will be ignored when checking for palindromes, so a word like "2dad2" will still be recognized as a palindrome.

Similar threads

  • Programming and Computer Science
Replies
2
Views
751
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
801
  • Engineering and Comp Sci Homework Help
Replies
11
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
949
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top