Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Checking arguments in argv

Posted on 2003-10-30
8
Medium Priority
?
451 Views
Last Modified: 2010-04-15
Hi,
I am trying to go through the argument vector and check whether each argument is either an integer or a string that was entered.  How would I be able to tell if It was a string or integer?  Thanks.
0
Comment
Question by:aomega
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9655971
Every argument u recieve would be in the form of char*

Then to distinguish if a particular arg is Numeric or not, u can may use atoi() function and depedning on the return, u can decide whether the arg is a number

0
 
LVL 1

Accepted Solution

by:
guitardude101 earned 150 total points
ID: 9655979
Hi,
   loop through all the chars in the argument and call isdigit to see if they are all '0' - '9'.
If the all are then the argument is an integer. You should also check for a '-' for negative numbers.


A function would look like this:


int isintvalue(char* str)
{
      if (*str == '-')
         str++;
     for ( ; *str; str++)
        if (! isdigit(*str))
             return 0;
     return 1;
}

NOTE: there are problems if you just call atoi and check for non 0 value.
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9656027
Check to see if it is an integer first.  If not, it is a string.
The strtol() function can handle this for you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Interperet arg as integer or string.
 * If the returned *asStr is non-NULL, it is the argument as a string.
 * If the returned *asStr is NULL, then asInt holds the integer value of the arg.
 */
void intOrStr(char * arg, char ** asStr, long * asInt)
{
  char *endp = NULL;

  if (arg) {
    /* first attempt to parse the arg as an integer */
    *asInt = strtol(arg, &endp, 0);

    /* if there was no integer, then (endp == arg) */
    if ((endp == arg) || (*endp)) {
      *asInt = 0;
      *asStr = arg;
    } else
      *asStr = NULL;
  }
}

int main (int argc, char ** argv)
{
  char *asStr;
  long asInt;

  int i;
  for (i = 0; i < argc; i++) {
    intOrStr(argv[i], &asStr, &asInt);
    if (asStr)
      printf("argv[%d] is a string: %s\n", i, asStr);
    else
      printf("argv[%d] is an integer: %ld\n", i, asInt);
  }
  return 0;
}

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:guitardude101
ID: 9656068
I just want people to know atoi("100J") returns 100 even though "100J" is not a number
0
 
LVL 6

Expert Comment

by:Ajar
ID: 9657356
#include<ctype.h>
int main(int argc,char * argv[])
{
      char * t;
      int i,is_numeric;
      for(i = 1; i < argc; i++)
      {
            t = argv[i];
            is_numeric = 0;
            while(*t!='\0') {if (isdigit(*t)) is_numeric = 1 ; else {is_numeric = 0;break;} }
                printf("ARGUMENT NUMBER %d :: '%s' is  %s",i,argv[i],is_numeric?"NUMERIC":"ALPHA NUMERIC");       
      }
}
0
 
LVL 6

Expert Comment

by:Ajar
ID: 9657366
sorry !!
Plz  replace
>>while(*t!='\0') {if (isdigit(*t)) is_numeric = 1 ; else {is_numeric = 0;break;} }
with
 
>>while(*t!='\0') {if (isdigit(*t)) is_numeric = 1 ; else {is_numeric = 0;break;} t++;}
         
 
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9657535

Hi aomega,

I like guitardude101's answer.  It also covers the case where the string represents a negative number.  For whatever it's worth, here's another variation:

int isintvalue(char* str)
{
      if (*str == '-' || *str == '+')
         str++;
      while (isdigit(*str))
        str++;
      return *str == '\0';
}

Though none of the examples in this thread handle hex constants.  ;)
Kent
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9659086
> I just want people to know atoi("100J") returns 100 even though "100J" is not a number
> [guitardude101's answer] covers the case where the string represents a negative number.
> Though none of the examples in this thread handle hex constants.

That is why I used strtol().  The if (*endp) test checks for the "100J" case.
strtol() handles leading +/- and whitespace.  strtol() with a radix of 0 (third param)
will even understand numbers in octal or hex.  Set the radix to "10" if you don't
want that behavior.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question