Solved

Checking arguments in argv

Posted on 2003-10-30
8
381 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

708 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

14 Experts available now in Live!

Get 1:1 Help Now