Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Checking arguments in argv

Posted on 2003-10-30
8
411 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 50 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 45

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

837 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