Link to home
Start Free TrialLog in
Avatar of HelpMeMaggi
HelpMeMaggiFlag for United States of America

asked on

How do I compare a command line string with another string?

Im trying to compare a command line string with a string in code but i keep getting errors. I have #include <strings.h>     what is typed in the command line is ./funny big or small either. I need to compare it so the programs runs a different function depending if they put in ./funny big or ./funny small How do i go about this?
Avatar of jimyX
jimyX

Can you provide the code you are using please?
Avatar of HelpMeMaggi

ASKER

thats the thing, i dont have one. Im using if statements.
I have
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = 'small';
char big[] = 'big';

smaller = strcmp(argv[1],'small');
bigger = strcmp(argv[1], 'bigger');

if (smaller = 0)
      {
            printf("small party isn't it?\n");
        return 0;
     }
else if(bigger = 0)
     {
            printf("Big party!!!\n");
           return 0;
      }
}

this is what i have.
String in C is enclosed by using double quotes:
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = "small";
char big[] = "big";

smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "bigger");

if (smaller = 0)
      {
            printf("small party isn't it?\n");
        return 0;
     }
else if(bigger = 0)
     {
            printf("Big party!!!\n");
           return 0;
      }
}

Open in new window

smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "bigger");

if (smaller == 0)
      {
            printf("small party isn't it?\n");
        return 0;
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
           return 0;
      }
}
So the only problem i had was my ", does this mean that if i were to put in ./funny small the output will be small party isn't it? ?
I can't test it at the moment.
Your argument has to be in a form that matches what you are comparing in the code:

So you should enter:
small or bigger to match in the if statement.
you also need use the equality operator rather than the assignment operator in your condition
Beside the equal as ozo said.

I am a pascal coder ;-)
Nice catch ozo :-)
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = "small";
char big[] = "big";

smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "bigger");

if (smaller == 0)
      {
            printf("small party isn't it?\n");
        return 0;
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
           return 0;
      }
}
so it would be like this? and yeah that was a typo by me, i just made this code up fast. Well how i have it.
Yes, also check what is typed in the command line to be ./funny small or ./funny bigger.
Just moved "return 0;" at the end to save one line of code ;-)
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = "small";
char big[] = "big";

smaller = strcmp(argv[1],"small");
bigger = strcmp(argv[1], "bigger");

if (smaller == 0)
      {
            printf("small party isn't it?\n");
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
      }
 return 0;
}

Open in new window

But hang on, what are you using char small and big for?
I think you mean to write:
main(int argc, char *argv[])
{
int smaller;
int bigger;
char small[] = "small";
char big[] = "big";

smaller = strcmp(argv[1],small);
bigger = strcmp(argv[1], big);

if (smaller == 0)
      {
            printf("small party isn't it?\n");
     }
else if(bigger == 0)
     {
            printf("Big party!!!\n");
      }
 return 0;
}

Open in new window

And hence the command line becomes: ./funny small or ./funny big as you stated in your question, and most probably that was your error.
smaller = strcmp(argv[1],small);
bigger = strcmp(argv[1], big);

shouldn't there be " " on small and big? or should they be without " " when being compared?
ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not complete code but i worked with what i have. thank you!