Quote (AbDuCt @ Sep 9 2012 10:03am)
it looks okay, but if it were me id skip using the isdigit function and just use
Code
if((int)x[i] >= 48 || (int)x[i] <= 58)
{
ret++;
}
compares the current characters integer value to its ascii decimal integer value.
either way works tbh.
i think you can use any integer datatype for strlen. the only reason to use something other than integer would be if your string contains a higher number than what a integer value can hold. in which case you would have to use a different data type for holding numbers.
in the cplusplus function information section they just output the output of strlen to printf using %u and casting the function output as a unsigned integer
just for shits and giggles if you wanted you could take the integer output from strlen and cast it as a character and use that rofl. could also use a simple algorithm so create a string of chars that fall between a-z to simulate 1-25. ex:
"c" would be 3, and "zb" would be 27.
just saying that you dont necessarily need to follow the correct data types. it just makes life easier if you do.
edit:: also give your variables realistic names. if you work on an actual project and name 10 strings x, asd,afgjasghdjkansdghasd youll be hated lol (you prolly already do but its a good habit to do 24/7)
Your code snippet will always pass true. I think you were looking for something more like this:
Code
if((int)x[i] >= 48 && (int)x[i] <= 58)
{
ret++;
}
Also, let me get nitpicky here for a sec and point out the things I would yell at the programmers working under me for.
In your numchecker function (and in any piece of code for that matter) always some whitespace between your variables and any kind of logic structure (whether it's your for loop or some other function call).
Always put whitespace between logic structures that aren't directly interacting with each other. i.e. You don't need white space after your beginning for loop line because the inner if statement is only being hit because something passes the for loop, but I would absolutely put white space between the end of the for loop and your if ret check.
These are little things that when it comes to letting team members maintain your code in the future add up to huge gains in productivity. (keep in mind the whitespace stuff is nitpicky and isn't a massive thing but it's more a show of experience. You'll almost never find a programmer with 10+ years of experience that doesn't use ample whitespace (but doesn't over use it) because they've had to go through code before that was all jammed together and know what a hassle it is.