Thread: Simple Prefix Programming giving weird results

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Simple Prefix Programming giving weird results

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int smaller_value(int length_word1,int length_word2);
    int main (void)
    {
    
    
    char word1[256];
    char word2[256];
    char prefix[256];
    
    
    printf("Enter two words: ");
    
    
    scanf("%s%s",word1,word2);
    
    
    
    
    int length_word1 = strlen(word1);
    int length_word2 = strlen(word2);
    int j = smaller_value(length_word1,length_word2);
    int i;
    
    
    
    
    for (i=0;i<j;i++)
    {
        if (word1[i] == word2[i])
            prefix[i]= word2[i];
    }
    
    
    int length_prefix = strlen(prefix);
    
    
    if(length_prefix == 0)
    
    
        printf("%s and %s have no common prefix.",word1,word2);
    else
        printf("The longest common prefix of %s and %s is %s.", word1, word2, prefix);
    
    
    printf("\n");
    
    
    return 0;
    }
    
    
    
    
    
    
    int smaller_value (int length_word1, int length_word2)
    {
    if (length_word1 > length_word2)
        return length_word2;
    
    
    if (length_word1 < length_word2)
        return length_word1;
    
    
    if (length_word1 == length_word2)
    
    
        return length_word1;
    }

    Objective:
    User enters two words, and I find the common prefix, or where the characters from both arrays are the exact same.
    Then i print the matching characters into another array

    My smaller_value function serves to only compare the characters from the smaller word, since the smaller word would finish comparing first.



    So this works:
    Enter two words: economy ecology
    The longest common prefix of economy and ecology is eco.


    However this is the weird output for a different run.
    Enter two words: The ehe
    The longest common prefix of economy and ecology is $he.



    What is the dollar sign doing here?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're not copying just the common prefix, you're copying only characters that match between the same position in word1 and word2. Furthermore, you're not null terminating prefix when you're done. Write out some examples on paper, and work through your algorithm by hand. You'll see the mistake. Then, work out the correct way to do it on paper, and turn that into code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POW() func giving weird output
    By workisnotfun in forum C Programming
    Replies: 3
    Last Post: 10-20-2012, 01:41 PM
  2. Getting weird results with isalpha
    By itsthemac in forum C Programming
    Replies: 7
    Last Post: 04-20-2011, 09:17 PM
  3. structure giving weird output
    By bluetxxth in forum C Programming
    Replies: 7
    Last Post: 02-14-2010, 11:44 PM
  4. sprintf being weird and giving me a segfult.
    By redruby147 in forum C Programming
    Replies: 10
    Last Post: 02-07-2010, 03:09 AM
  5. Dot3 bumpmapping: weird results
    By psychopath in forum Game Programming
    Replies: 12
    Last Post: 02-09-2006, 01:40 PM