Originally Posted by
std10093
No, Prelude wants to say that you may now implement your code, without assuming that "command line arguments which are assumed to be integers", from your exercise.
In other words, until now, you assume that the input is something like this : foo 3 4 8 9 6
Now, you may assume that the input can be this : foo t 4 h 5 . In this case, I would add 4 and 5 and discard letters t and h (you could add up their ascii value, but I can not see the point).
Code:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{
int i , sum=0;
for( i = 1; i < argc; i++)
{
if ( isalpha( *argv[i] ) )
continue;
sum += atoi(argv[i]);
}
printf("Total: %d\n" , sum);
return 0;
}
I think is what we want. Isn't it???
However I have some questions ...
1. If char str[] = ''11"; then atoi(str) converts 11\0 to 11 ? from string form to integer form I saw that if we give a string of alphabetic characters in atoi will return 0 for example int x = atoi("Hi"); // 0
2. How int main (argc , *argv[]) reads the arguments from the keyboard???? It seems like scanf.... for example
Code:
....
printf("Give x and y: ");
scanf("%d%d" , &x , &y);
printf(" %d , %d " , x , y);
INPUT: 234 23tak
OUTPUT: x is 234 y is 23
and
Code:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{
int i , sum=0;
for( i = 1; i < argc; i++)
{
/*if ( isalpha( *argv[i] ) )
continue;
sum += atoi(argv[i]);*/
printf(" %d " ,atoi(argv[i]));
}
///printf("Total: %d\n" , sum);
return 0;
}
INPUT : ./sum 23wtwttwtw 22aaaa 1
OUTPUT: 23 22 1