Thread: Program doesnt work properly.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    Program doesnt work properly.

    Hello guys!

    I wrote this program that compares two lines and says if they are equal or not. But there is a twist: A star can replace any character and even any Number of characters (like whole words).

    My program nearly works. It returns everything correctly apart from when there is a mistake AFTER the star.

    So:

    Hello World
    Hello World
    = Match.

    Hello World
    H*o World
    = Match.

    Hello World
    *World
    = Match

    But
    Hello World
    Hello W*sfd
    Shows it matches, but it shouldnt!

    Any help, tips, advice is appreciated.

    Code:
    int     match(char *s1, char *s2)
    {
      int   s1length;
      int   s2length;
      int   is_match;
      int   y;
      int   x;
      int   i;
    
      s1length = strlen(s1);
      s2length = strlen(s2);
      is_match = 0;
      y = 0;
      while (y < s1length)
        {
          i = y;
          if (s2[y] == '*')
            {
              while (s1[y] != s2[i + 1])
                {
                  if (s1[y] == '\0')
                    {
                      is_match = 1;
                      break;
                    }
                  y++;
                }
              break;
            }
          if (s2[s2length - 1] == '*')
            {
              is_match = 1;
              break;
              }
          if (s1[y] == s2[y])
            {
              is_match = 1;
              y++;
              continue;
            }
          else
            {
              is_match = 0;
              break;
            }
        }
      return (is_match);
    }
    
    int     main()
    {
      int   x;
      char array1[] = "Hello World";
      char array2[] = "Hello W*ef";
      x = match(array1, array2);
      printf("%d", x);
      return (0);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    while (s1[y] != s2[i + 1])
    {
      if (s1[y] == '\0')
      {
        is_match = 1;
        break;
      }
      y++;
    }
    In your example, s2[i + 1] would be 's', but there's no 's' in s1. You'll hit the end of the string, and your logic says that this is a match. Since you're matching strings exactly with the exception of the wildcard, if s1[y] is '\0', s2[i + 1] should be '\0' as well for there to be a legitimate match in this case.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Didnt solve my problem :/

    It still considers anything after a wildcard a Match.
    So if it is like this:
    Hello World
    Hdllo W*orld

    It works properly and shows that it is Not a match. But

    Hello World
    Hello W*sef

    Shows it matches. So after a wildcard it just shows everything as match.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Hey.

    I solved my problem, so thank you anyway.

    But I ran into a different one... I cant count the number of matches possible :/

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Didnt solve my problem :/
    And we can't help anymore because your code is different now, but you didn't post it.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Why does this program not work on my laptop?!
    By Eddie K in forum C Programming
    Replies: 1
    Last Post: 03-11-2006, 04:34 PM
  4. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 09:58 AM
  5. how do i get this program to work, anyone, anyone?
    By correlcj in forum C Programming
    Replies: 5
    Last Post: 07-04-2002, 10:28 PM