Thread: How can I compare strings?

  1. #1
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    How can I compare strings?

    Whats an easy way to compare arg1 with arg2
    and if arg1 only contains one letter and it that matches arg2's first letter
    then it returns 1 and if arg1 contains more than one character
    that all match arg 2 it returns one otherwise it returns zero?

    Code:
    int is_abbrev(const char arg1, const char arg2)
    {
      if (!*arg1)
        return (0);
    
      for (; *arg1 && *arg2; arg1++, arg2++)
        if (LOWER(*arg1) != LOWER(*arg2))
          return (0);
    
      if (!*arg1)
        return (1);
      else
        return (0);
    }

    I basically just want acopy of the above that returns one if arg1 only has one character and that character matches arg2's first character. thanks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think you're looking for strncmp, where n is the length of arg1: C Guide--2.14 string.h
    Last edited by anduril462; 11-15-2010 at 03:09 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by errigour
    Whats an easy way to compare arg1 with arg2
    and if arg1 only contains one letter and it that matches arg2's first letter
    then it returns 1 and if arg1 contains more than one character
    that all match arg 2 it returns one otherwise it returns zero?
    I am not too clear about this. Are you saying:
    Return 1 if and only if the string arg1 has exactly one character which is equal to the first character of the string arg2, or the string arg1 is equal to the string arg2; otherwise return 0. Use case-insensitive comparison.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    arg

    Iw wanr it return one if the first string is even equal to the seconds second after one character agr1++;;

    Im comparing strings and if arg one is even equal to the first erregualar sring arg2 I want it to return one but onley if arg one has no arguments after ih,

    Code:
    int is_abbrev(const char *arg1, const char *arg2)
    {
      if (!*arg1)
        return (0);
    
      for (; *arg1 && *arg2; arg1++, arg2++)
        if (LOWER(*arg1) != LOWER(*arg2))
          return (0);
    
      if (!*arg1)
        return (1);
      else
        return (0);
    }
    I want the above code to return one even if the strings match at the first character and arg one has no other character than the first character.
    Last edited by errigour; 11-15-2010 at 11:47 PM. Reason: its good to detect fivsable feractle characters and compare characters after the second character but if character

  5. #5
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Code:
    int is_abbrev(const char *arg1, const char *arg2)
    {
      if (!*arg1)
        return (0);
    
      for (; *arg1 && *arg2; arg1++, arg2++)
        if (LOWER(*arg1) != LOWER(*arg2))
          return (0);
    
      if (!*arg1)
        return (1);
      else
        return (0);
    }
    I want the above code to return one even if the strings match at the first character and arg one has no other character than the first character.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So if I understand correctly, if the entire string in arg1, whether it's 1 character or several characters, is the same as the beginning of arg2, then return 1. Otherwise return 0.

    If that's the case, then you should use the strncmp function as I described, with one very minor modification. strncmp is going to return zero if they match up to the n characters, so you need to adjust your is_abbrev function accordingly.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by errigour
    Iw wanr it return one if the first string is even equal to the seconds second after one character agr1++;;

    Im comparing strings and if arg one is even equal to the first erregualar sring arg2 I want it to return one but onley if arg one has no arguments after ih,

    (...)
    I want the above code to return one even if the strings match at the first character and arg one has no other character than the first character.
    A few examples might help, and in fact can be used to write automated tests. For example, what are the expected return values of these function calls?
    Code:
    is_abbrev("a", "abc");
    is_abbrev("ab", "abc");
    is_abbrev("abc", "abc");
    is_abbrev("abcd", "abc");
    is_abbrev("A", "abc");
    is_abbrev("a", "Abc");
    is_abbrev("ABC", "abc");
    is_abbrev("abc", "ABC");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    Expected values for woowoo.

    directions[]]= { "north",
    "east",
    "south",
    "west",
    "up",
    "down" };

    I just want to be able to open n and all i need is for the is_abbrev function to return 1 if the value n is passed to agument. same for n e w s u d. But the is_abbrev function +
    + plusseses I think before its first comparison. but c progrmmaing mus be understood to modify that function.


    Code:
    int find_door(struct char_data *ch, const char *type, char *dir, const char *cmdname)
    {
      int door;
      char *directions[] = {"north", "east", "south" , "west" , "up", "down"};
    
      if (*dir) {			/* a direction was specified */
        if ((door = search_block(dir, dirs, FALSE)) == -1) {	/* Partial Match */
          send_to_char(ch, "That's not a direction.\r\n");
          return (-1);
        }
        if (EXIT(ch, door)) {	/* Braces added according to indent. -gg */
          if (EXIT(ch, door)->keyword) {
    	if (isname(type, EXIT(ch, door)->keyword))
    	  return (door);
    	else {
    	  send_to_char(ch, "I see no %s there.\r\n", type);
    	  return (-1);
            }
          } else
    	return (door);
        } else {
          send_to_char(ch, "I really don't see how you can %s anything there.\r\n", cmdname);
          return (-1);
        }
      } else {			/* try to locate the keyword */
        if (!*type) {
          send_to_char(ch, "What is it you want to %s?\r\n", cmdname);
          return (-1);
        }
        for (door = 0; door < NUM_OF_DIRS; door++)
          if (EXIT(ch, door))
    	if (EXIT(ch, door)->keyword)
    	  if (isname(type, EXIT(ch, door)->keyword) || is_abbrev(type, directions[door]))
    	    return (door);
    
        send_to_char(ch, "There doesn't seem to be %s %s here.\r\n", AN(type), type);
        return (-1);
      }
    }
    
    
    int is_abbrev(const char *arg1, const char *arg2)
    {
      if (!*arg1)
        return (0);
    
      for (; *arg1 && *arg2; arg1++, arg2++)
        if (LOWER(*arg1) != LOWER(*arg2))
          return (0);
    
      if (!*arg1)
        return (1);
      else
        return (0);
    }
    I just need a function simular to is_abbrev that will return one if n e s w u or d is passed to it.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    errigour, answer my question in post #7. It is pointless to keep posting code that does not do what you want, and then asking for help to fix it, when those who can help you are not entirely sure of what exactly the code is supposed to do in the first place.

    Quote Originally Posted by errigour
    I just need a function simular to is_abbrev that will return one if n e s w u or d is passed to it.
    So you don't need help on is_abbrev, but on something else? What exactly does this other function do? You need to describe it clearly, hopefully with several examples, not just dump some code on us.
    Last edited by laserlight; 11-16-2010 at 03:12 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    No i need help on is_abbrev

    Or mayby a new function that will do what is_abbrev isn't. The other function parses argument. I just need something simular to is_abbrev that recognizes only one character as an abbreviation of argument two. If only one character was used. I just put the other function there to show how is_abbrev is being used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding a character in an array of strings
    By ewoods in forum C Programming
    Replies: 3
    Last Post: 08-12-2009, 03:37 PM
  2. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  4. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 05:03 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 09:17 PM