I want this program to say the length of the longest common prefix.

This is a discussion on I want this program to say the length of the longest common prefix. within the C Programming forums, part of the General Programming Boards category; I wrote this program to find the longest common prefix but for some reason it is failing some of the ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    38

    I want this program to say the length of the longest common prefix.

    I wrote this program to find the longest common prefix but for some reason it is failing some of the tests I am running on it. Any ideas why?
    Code:
    int longestCommonPrefix(const char *a, const char *b){
    	int size,sizea, sizeb;
    	int com=0;
    	int i=0;
    	sizea=strlen(a);
    	sizeb=strlen(b);
    	if(sizea==NULL||sizeb==NULL){//probably wrong
    		return com;}
    	if(sizea==0||sizeb==0){
    		return com;}
    	if(sizea>=sizeb){
    		size=sizea;}
    	if(sizea<sizeb){
    		size=sizeb;}
    	for(i=0; i<size; i++){
    		if(a[i] != b[i]){
    			return com;
    			break;}
    		com++;}
    	return com;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Perhaps if you posted some examples of "some tests which fail", we might be able to tell you.

    Nobody is going to spend hours trying to replicate (possibly without success) what you already know.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    Thank you. I forgot to say that, I am slightly new to asking for help with programming.
    ok "foo", "bar" does not come back right and "", "bar" and "balls", "" those are the only ones that do not work. I tried to fix the issue of "" with this part of the code
    Code:
     
    Code:
    if(sizea==NULL||sizeb==NULL){//probably wrong return com;} if(sizea==0||sizeb==0){ return com;}

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    What does strlen return? Does it make sense to compare it to NULL? It will probably work, depending on how NULL is defined, but it's not standard. Maybe you really want to check a and b to see if they are NULL before you use them, because you're doing the same thing here

    Code:
    if(sizea==0||sizeb==0){
            return com;}
    I'd suggest learning to use your debugger and step through the code to see what's happening so that you actually understand it.

  5. #5
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    When I comment out the bizarre NULL comparison, it compiles with no warnings, or errors, and functions correctly for foo/bar = 0 , and bar /balls = 2. Here's the full code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int longestCommonPrefix(const char *a, const char *b);
    
    int main(void)
    {
    int c = longestCommonPrefix("bar","balls");
    printf("longest prefix: - %i",c);
    return 0;
    }
    
    int longestCommonPrefix(const char *a, const char *b)
    {
        int size,sizea, sizeb;
        int com=0;
        int i=0;
        sizea=strlen(a);
        sizeb=strlen(b);
     //   if(sizea==NULL||sizeb==NULL){//probably wrong
     //       return com;}
        if(sizea==0||sizeb==0){
            return com;}
        if(sizea>=sizeb){
            size=sizea;}
        if(sizea<sizeb){
            size=sizeb;}
        for(i=0; i<size; i++){
            if(a[i] != b[i]){
                return com;
                break;}
            com++;}
        return com;
    }

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    Ok I have made those changes but the program using my code to check to see if it is correct still says it is wrong for those cases only. And it still does not accept the "" one to be zero. I think it wants it to be returning null instead of 0. I will check, but still the foo and bar should be correct.

  7. #7
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    What can I say, it compiles with no warnings and functions as intended. I am able to put in a string with nothing, "" and it returns 0. I am using gcc. I am guessing you are on some windows based IDE. Have you tried just copying and pasting and compiling my working version? Good luck.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    I am using GCC too. Yes I have tried using your code too. I think it a problem with the grade checker. Mine was working the same too. I will check and then keep you updated.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    Ok it was a graders error. It is now fixed. Thanks for all the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. longest common substrings ( I can't implement algoritm)
    By paranoidgnu in forum C Programming
    Replies: 3
    Last Post: 05-14-2011, 10:32 AM
  2. longest prefix
    By Marksman in forum C Programming
    Replies: 1
    Last Post: 03-14-2009, 11:19 PM
  3. longest common substring problem
    By Enchanter88 in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2007, 11:02 AM
  4. longest common substring
    By TechHigh in forum C Programming
    Replies: 6
    Last Post: 01-05-2007, 02:18 AM
  5. Longest Common Subsequence
    By stimpyzu in forum Tech Board
    Replies: 4
    Last Post: 04-04-2005, 03:18 PM

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