A user wants a C program that will indicate whether its argument is a valid inte
ID: 3602652 • Letter: A
Question
A user wants a C program that will indicate whether its argument is a valid integer or not. The program, called isint, accepts one argument. If the argument is a string that contains only numeric characters, optionally preceded by a '-' or '+' character, the program exits indicating success. Otherwise the program exits indicating failure (false). The program also exits with failure if the value of the argument is 0 and it is preceded by a '-' or '+' character (since zero is neither positive or negative). The program can also take an optional argument, -p, which requires that the integer be positive for successful termination. For instance, the program will exit-with-success in the following cases: isint 2345 isint -2345 isint 0 isint +2 isint -p 2345 inint -p +1 However, the program will terminate with a non-zero exit status for: isint d345 isint -2d45 isint +2d isint -p -2345 isint -p 00 Design, implement, test, and document such a program. The program does not generate any output, either on stdin or stderr. To see the result of the program, output the value of the shell variable ? after executing the command. (The $ shell variable contains the exit status of the last program executed.) For example, you could exercise the program in the following way: bash-4.3$ isint 000 bash-4.3$ echo $? 0 bash-4.3$ isint -p "2 3" bash-4.3$ echo $? 1 bash-4.3$ where "bash-4.3$ " is the bash shell prompt. Use filenames beginning with q2 for your annotated test log and program documentation. Submit them and your isint.c file as your solution to this question. Notes: You may find the man page for isdigit(3) of use. Even though the program takes few options and arguments, students may find it useful to consider using the functions in getopt(3). The above example uses an exit value (by isint) of 1 for a failing case. You can use any other non-zero value instead.
Explanation / Answer
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
main(int argc, char *argv[])
{
int flags, opt;
int nsecs, tfnd;
int i = 0;
int totalarg = argc;
nsecs = 0;
tfnd = 0;
flags = 0;
if(argc<2)
{
printf("Usage : ./isint valid_number "); //if less argument give usage
exit(2);
}
if(argc > 2)
{
while ((opt = getopt(argc, argv, "p")) != -1) // check for option
{
switch (opt)
{
case 'p':
if(argv[2][0] == '-') // if give minus in p then give fail and return 1
{
exit(1);
}
if(argv[2][1] == '0') // if 0 then invalied so return 1
{
if(!atoi(argv[1]))
exit(1);
}
for(i=0 ;i< argv[2][i] ;i++) // check digits one by one
{
if(isdigit( argv[2][i]))
continue;
else
{
exit(1); // if not digit exit with 1
}
}
exit(0);
break;
}
}
}
if(argv[1][0] == '-') //check for -
{
i = 1;
if(argv[1][i++] == '0') //check for 0 if it is then exit 1
{
if(!atoi(argv[1]))
exit(1);
}
}
else if(argv[1][0] == '+') //check for +
{
i = 1;
if(argv[1][i++] == '0') //check for 0 if it is then exit 1
{
if(!atoi(argv[1]))
exit(1);
}
}
for( ;i< argv[1][i] ;i++) //check digits
{
if(isdigit( argv[1][i]))
continue;
else
{
exit(1);
}
}
exit(EXIT_SUCCESS); //if all things work properly exit with 0
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.