Thread: Finding a common suffix

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Finding a common suffix

    I am a new C programmer, so I imagine my errors are fairly obvious. I am trying to use this program to find a common suffix between two words, starting from the end (destination and procrastination would return stination, for example). My method of adding characters to the suffix array is apparently wrong.

    What is my error[s]?

    I believe line 31, in particular, has something wrong.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int
    main(void)
    {
      char word_one[80] = "";
      char word_two[80] = "";
      char suffix[80] = "";
      int shorter_length;  
    
      printf("Word #1: ");
      fgets(word_one, 80, stdin);
      printf("Word #2: ");
      fgets(word_two, 80, stdin);
    
      if(strlen(word_one) < strlen(word_two)) //if word one is shorter
      {
        shorter_length = strlen(word_one);
      }//if
      else //otherwise...
      {
        shorter_length = strlen(word_two);
      }//else
    
      for(int x = shorter_length; x > 0; x--) //process strings
      {
        if(word_one[x] == word_two[x]) 
        {
          printf("match found.  %c matches with %c", word_one[x], word_two[x]);
          suffix[x] = word_one[x];  
        }//if  
      }//for
    
      printf("Common suffix: %s", suffix);
    
      return 0;
    }//int main(void)

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Take a piece of paper and draw out step for step this loop, you will find your problem quickly.
    Code:
     for(int x = shorter_length; x > 0; x--) //process strings
      {
        if(word_one[x] == word_two[x])
        {
          printf("match found.  %c matches with %c", word_one[x], word_two[x]);
          suffix[x] = word_one[x]; 
        }//if 
      }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your algorithm needs tweaking. Words with different lengths can't start comparing letters, based on the length of either the shorter, or the longer word.

    You will need two variables - one for each word. Each should be set to the length of it's own word.

    Code:
    len1 = strlen(word1);
    len2 = strlen(word2);
    Now a while loop makes a pretty intuitive loop

    Code:
    while(len1 > 0 && len2 > 0 && word[len1] == word[len2]) {
       //rest of your code in here
       //decrement len1 and len2
    }
    Make sense?

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    I'd write a function that returns the length of the common suffix, zero if none:
    Code:
    size_t common_suffix(const char *const string1, const char *const string2)
    {
        const size_t  length1 = (string1 != NULL) ? strlen(string1) : 0;
        const size_t  length2 = (string2 != NULL) ? strlen(string2) : 0;
        const size_t  n = (length1 < length2) ? length1 : length2;
        size_t        i = 1; /* Offset before end of string, 1 .. n, inclusive. */
    
        /* As long as the i'th characters from the end match, increase i. */
        while (i <= n && string1[length1 - i] == string2[length2 - i])
            i++;
    
        /* The length of the common suffix is i - 1 characters. */
        return i - 1;
    }
    I used the ternary operator condition ? value-if-true : value-if-false to treat NULL pointers as if they were empty strings, and to save the smaller of the two string lengths into n.

    The index i is always between 1 and shorter string length, inclusive, so you can use string[ length - i ] to access the i'th character from the end of the string.

    Using your example strings, you can get the common suffix and its length using
    Code:
    static const char *const one = "destination";
    static const char *const two = "procrastination";
    size_t common;
    
    common = common_suffix(one, two);
    if (common > 0)
        printf("'%s' and '%s' have a %d-character common suffix, '%s'.\n", one, two, (int)common, one + common);
    else
        printf("'%s' and '%s' do not have a common suffix.\n", one, two);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding common numbers
    By BB89 in forum C Programming
    Replies: 14
    Last Post: 11-10-2009, 08:22 AM
  2. finding most common char in a string
    By scwizzo in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2007, 01:22 PM
  3. C program for finding highest common factor!!
    By visham in forum C Programming
    Replies: 11
    Last Post: 08-02-2007, 07:10 AM
  4. What does the suffix * mean?
    By flaran in forum C++ Programming
    Replies: 14
    Last Post: 10-18-2005, 09:09 PM
  5. Finding the most common char
    By Mikro in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 09:00 AM