# Determine if string contains anything but only one letter

• 02-26-2012, 07:55 AM
atomant
Determine if string contains anything but only one letter
I want to determine if a string contains anything but only one certain character. For example, "AA" = true, "AAAAAAAAA" = true, "AAAXAAA" = False, "EKNFLGKGOKE" = False. I'm not sure how to easily do this.

Thanks
• 02-26-2012, 08:10 AM
pbrockway2
Re: Determine if string contains anything but only one letter
Quote:

"AA" = true, "AAAAAAAAA" = true, "AAAXAAA" = False, "EKNFLGKGOKE" = False.
How do you know that?

What I mean is what thought process told you that "AAAAAAAAA" was true while "AAAXAAA" was false? Figuring out how you decided that is the first (and biggest) step towards writing code to do the same thing. Our eyes are very good at detecting patterns (and especially breaks in patterns) - they act quickly and beneath conscious awareness - so we are not really given a chance to be reflective about how we solve problems like this.

One way of achieving this awareness is to imagine you can only see one character at a time. For instance suppose people entered your room through a door on the left carrying a letter on a cardboard sign and exited again through a door on the right. You knew in advance you were going to be asked if all the people were displaying the same character. What would you do while the people came and went to ensure you could answer the question? Could you answer it "early": ie would you have to pay attention to all the people?

-----

After that it's a case of looking at the String API, and reviewing what you know about loops and the "break" keyword. But think about the problem first! In "slow motion" as suggested above or some other way. You need a precise and comprehensive description of the process (== "algorithm") before you can start to write code.