how do i figure out if a string has a number in it?
hey guys how would i figure out if a string has a number in it without using any aditional libraries?
revenge2
Junior Poster in Training
79 posts since Feb 2007
Reputation Points: 10
Solved Threads: 2
store a char array = ["0123456789"]
Loop through said string to check if it contains any of those numbers. Boom job done.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
ah thanks alot. My question was wrong ealier i was actually trying to fingure out if there were any characters in a character string. (the input is binary or decimal number). i wanted to check whether it contained any characters so that i could print out "invalid number" .
would this be correct for the above situation?
#include <stdio.h>
int main() {
char input[80];
printf("number: );
gets(input);
int i=0;
while (i != '\0') {
if ( input[i]<0 || input[i]>9) {
printf("invalid number!");
}
}
}
revenge2
Junior Poster in Training
79 posts since Feb 2007
Reputation Points: 10
Solved Threads: 2