Thread: String comparing in C

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    3

    String comparing in C

    I know Perl, and I'm so used to the operators for it.
    For example, in perl..

    Code:
    if($input =~ /help (.*)/)
    {
         $command = $1;
         if($command eq "echo")
         {
              #Example
         }
         else {
                 print "Command not found.\n";
          }
    }
    What is the C equivalent?
    Thanks,
    Austin B.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read up on strcmp.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you want the first line with any degree of utility, you'll need this
    http://www.pcre.org/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    "strcmp" compares two different strings

    Eg: strcmp(a,b); compares a and b, if they are equal returns a zero value, if less a value less than zero and if greater oh well u know
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    I know about strcmp, I just want something that will allow you to do something like this.
    Like an IRC bot:

    Person: !add Person
    Bot: Person is added to the Messaging List.
    Person2: !add Person2
    Bot: Person2 is added to the Messaging List.

    Make sense?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, but what does that have to do with string comparing? Do you mean "how do I read in a string"? What?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Or "How to write bot?"

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by petrock6 View Post
    I know about strcmp, I just want something that will allow you to do something like this.
    Like an IRC bot:

    Person: !add Person
    Bot: Person is added to the Messaging List.
    Person2: !add Person2
    Bot: Person2 is added to the Messaging List.

    Make sense?
    Absolutely not - it does not make one iota of sense. What in tarnation does that have to do with comparing strings in Perl versus C?
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    if(strstr(buffer, "!add") == buffer)
      /* do stuff */

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    3
    Finally, sounded like I was talking to a wall.
    Thank you, Master5001.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by petrock6 View Post
    I know Perl, and I'm so used to the operators for it.
    Oh, are you in for some fun. I think the nature of string ops in C were the motive for creating perl, but you'll have to check w/ Mr. Wall.

    Master5001 is being tricky here. You should look up strcmp as well.

    Code:
    if (strcmp(command, "echo")==0) dostuff();
    else whatever...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Comparing string contents
    By Slavakion in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2004, 12:14 PM