Results 1 to 7 of 7
I am currently working on c and I have no way of checking if a string is an integer....
- 06-17-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 8
checking if a string is an integer
I am currently working on c and I have no way of checking if a string is an integer.
- 06-17-2009 #2Just Joined!
- Join Date
- Apr 2009
- Posts
- 33
there might be already a function that does that.
But right off the bat I would do that: loop thru the string and
check each character. If I find something different than '0, 1, ..., 9' I
exit the loop and I return something that says it's not an integer
If the loop terminates normally, then yes it's an integer
- 06-17-2009 #3
You can use the "isdigit" function in the ctype.h library to do something like vonbiber suggested.
You can also use "ispunct" to test for a decimal to distinguish between integers and rational numbers.Code:#include <ctype.h> for(i = 0; i < stringLength; i++) { if(isdigit(string[i]) == 0) break; } if(i != stringLength) printf("Noninteger value\n");
The isdigit and ispunct functions return a non-zero value if the tested variable falls into the class, and 0 if not. We want the loop to break when a noninteger value or a "." decimal value is encountered, that way the variable 'i' will not increment to the value of stringLength, which we will use to determine if the string is integer or not.Code:if(isdigit(string[i]) == 0 || ispunct(string[i]) != 0)
Here is a function that you can use, that would work as so...
Code:if(isInt(string) == 0) *integer* else if(isInt(string) == 1) *is not integer*
Code:#include <string.h> #include <ctype.h> int isInt(char *string) { int i, stringLength = strlen(string); for(i = 0; i < stringLength; i++) { if(isdigit(string[i]) == 0 || ispunct(string[i]) != 0) break; } if(i != stringLength) return 1; else return 0; }
- 06-17-2009 #4Just Joined!
- Join Date
- May 2009
- Posts
- 8
thanks guys i think i sort of got it working really appreciate the help
- 06-23-2009 #5Just Joined!
- Join Date
- May 2008
- Posts
- 5
You could also use the "strtol" function provided in <stdlib.h>.
Sample from the man page.
Code:#include <stdlib.h> #include <limits.h> #include <stdio.h> #include <errno.h> int main(int argc, char *argv[]) { int base; char *endptr, *str; long val; if (argc < 2) { fprintf(stderr, "Usage: %s str [base]\n", argv[0]); exit(EXIT_FAILURE); } str = argv[1]; base = (argc > 2) ? atoi(argv[2]) : 10; errno = 0; /* To distinguish success/failure after call */ val = strtol(str, &endptr, base); /* Check for various possible errors */ if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) { perror("strtol"); exit(EXIT_FAILURE); } if (endptr == str) { fprintf(stderr, "No digits were found\n"); exit(EXIT_FAILURE); } /* If we got here, strtol() successfully parsed a number */ printf("strtol() returned %ld\n", val); if (*endptr != '\0') /* Not necessarily an error... */ printf("Further characters after number: %s\n", endptr); exit(EXIT_SUCCESS); }
- 06-23-2009 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Personally, I prefer to use strtol() for this sort of task. SInce it is part of libc, it is likely highly optimized, utilizing processor instructions as efficiently as possible to handle the task.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-23-2009 #7Just Joined!
- Join Date
- Apr 2009
- Posts
- 33
or you could write your own function, then
if (isinteger(your_string))
printf("yes it's an integer\n");
else
printf("nope\n");
int isinteger(char *str)
{
char *p;
p = str;
if (p == NULL)
return 0;
while (*p != 0)
{
if (*p != '0' && *p != '1' && *p != '2' &&
*p != '3' && *p != '4' && *p != '5' &&
*p != '6' && *p != '7' && *p != '8' && *p != '9')
return 0;
p++;
}
return 1;
}


Reply With Quote
