String comparing in C

This is a discussion on String comparing in C within the C Programming forums, part of the General Programming Boards category; I know Perl, and I'm so used to the operators for it. For example, in perl.. Code: if($input =~ /help ...

  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,185
    Read up on strcmp.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,185
    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
    Katy, Texas
    Posts
    2,309
    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?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21