?
Solved

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

Posted on 2011-02-25
15
Medium Priority
?
365 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:HelpMeMaggi
[X]
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
  • 8
  • 5
  • 2
15 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 34986019
Can you provide the code you are using please?
0
 

Author Comment

by:HelpMeMaggi
ID: 34986073
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.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986091
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

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 84

Expert Comment

by:ozo
ID: 34986097
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;
      }
}
0
 

Author Comment

by:HelpMeMaggi
ID: 34986099
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.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986109
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 34986111
you also need use the equality operator rather than the assignment operator in your condition
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986116
Beside the equal as ozo said.

I am a pascal coder ;-)
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986121
Nice catch ozo :-)
0
 

Author Comment

by:HelpMeMaggi
ID: 34986134
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.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986148
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

0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986156
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.
0
 

Author Comment

by:HelpMeMaggi
ID: 34986159
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?
0
 
LVL 24

Accepted Solution

by:
jimyX earned 2000 total points
ID: 34986188
This what brought the confusion in the first place. You compare them with the array you initialized as follows:

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

char big[] = "big";
bigger = strcmp(argv[1], big);

Either that or you can use direct string in quotation, but then there is no need to use the arrays, so it becomes:
main(int argc, char *argv[])
{
int smaller;
int bigger;

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


In command line you write the same string you are comparing against otherwise you get no matching.
0
 

Author Closing Comment

by:HelpMeMaggi
ID: 34986223
Not complete code but i worked with what i have. thank you!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

771 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