Solved

Checking arguments in argv

Posted on 2003-10-30
8
400 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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:Kdo
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 80
Passing command line arguments in C 10 89
distributed computing in Python 4 122
What's the Difference Between a VI, the Command Prompt and a Shell 7 105
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now