Solved

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

Posted on 2011-02-25
15
358 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 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