+ 2
C question: why there's an "i" here ?
int main(int argc, string argv[ ]) { int trackdigit = 0; if (argc == 2) { for (int i = 0, n = strlen(argv[1]); i < n; i++) { if (isdigit(argv[1][i])) /////// here { trackdigit +=1; } And another question: why when im doing normal if else statement and trying to check if argv[1] is digit without adding "i" it doesn't work and give me segfault
5 Antworten
+ 5
Command line arguments are strings, but isdigit() is only defined for single characters (which a string is composed of), hence the loop and the additional index. The expression argv[1][i] refers to the (i + 1)th character in the second command line argument, so the loop counts how many digits are present in that string.
+ 2
May I know
Is there string type in c? Is it new feature? Shadow
+ 1
Jayakrishna🇮🇳 Not that I know of, I assumed it to be a typedef for char* maybe.
+ 1
Jayakrishna,
The below thread might have answer to your doubt. The code in that thread includes cs50 header, perhaps 'string' was a custom type introduced in that header.
https://www.sololearn.com/Discuss/2984614/?ref=app