954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

check number is string

how to check there exist number in string using isalpha.if number exist, display "only alphabetic,please input again"

example :
user input = i had to 2 number

amen
Newbie Poster
17 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

Is it really that hard?

if(isalpha(char))


I don't know if that takes a string or not, but if it's only a character then just loop through.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

If isalpha() failes it does not mean that the character is a numeric digit, it could be anything else too. If you want to check for numeric digits then use isdigit(). And both only check one character, so if you have a whole string then you must check each character individually.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

If you want to make sure there are no numbers in a string, you can do a find_first_of( "01234567890"). If it equals anything other than string::npos, it's got a number in there.

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

what if we dont have to use library functions.

anandmadhab
Newbie Poster
2 posts since Jun 2010
Reputation Points: 9
Solved Threads: 0
 

Well, in that case just do it the hard way -- put this in a loop
if( string[i] >= '0' || string[i] <= '9' )

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You