Solved

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

Posted on 2011-02-25
15
354 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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 article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 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

12 Experts available now in Live!

Get 1:1 Help Now