开发者

How can a program detect whether a string is a number or a character?

开发者 https://www.devze.com 2023-03-13 02:12 出处:网络
I\'m working on a program that works with functions like x + 3. The pr开发者_如何转开发ogram is able to calculate the equation and give the user a table of values. What I want to do next is to add the

I'm working on a program that works with functions like x + 3. The pr开发者_如何转开发ogram is able to calculate the equation and give the user a table of values. What I want to do next is to add the option to enter something like x + a. If the user puts the x value at 1, for instance, I want it to be able to say 1 + a. What I believe that I need in order to do this is to separate the string into an array, and then have it detect any characters that are alphabet (aside from x). I already know how to separate the string, but is there a way to detect an alphabet character without having to do something like a huge case switch?


Compare each character to the end points of the alphabet.

char c = ...;
if( ( c >= 'a' && c <='z' ) || ( c >='A' && c <= 'Z' ) )
   it's a character;

(I don't know the objective C syntax)

(I'm so bad at formatting on this site it's not even funny)


Instead of splitting the string manually, you could use a NSScanner to examine the string components.

0

精彩评论

暂无评论...
验证码 换一张
取 消