Kallum Smith
Kallum Smith

Reputation: 33

C: print the longest common prefix

Beginner programmer here, trying to figure out how to find and print the longest common prefix in C.

I have a base here for the program.

#include <stdio.h>

void findprefix(char *str1, char *str2, char *found);

int main(void) {
    char str1[100];
    char str2[100];
    char found[10] = { '\0' }; 
    
    printf("\nGive string 1: ");
    scanf("%99s", str1);
    printf("\nGive string 2: ");
    scanf("%99s", str2);
    
    //Print prefix
    findprefix(str1, str2, found); 
    printf("%s", found);
    
    return 0;
}

//Function to find the longest common prefix
void findprefix(char *str1, char *str2, char *found) {
    
    int i, j;
    
    for () {
        if () {
        }
    }
}

The initial idea is to use a for loop and an if statement in the function but I'm not sure how.

Upvotes: 2

Views: 3108

Answers (2)

chqrlie
chqrlie

Reputation: 144810

You have a good base, except you should define prefix with a length of 100 for pathological cases.

In the function, you should iterate with an index i starting at 0, comparing the characters from str1 and str2 at offset i and stop when they differ or when either one is a null byte (a char with the value 0), otherwise copying the byte to the found array at the same offset i.

After the loop. you would store a null byte in found at the offset where you stopped the iteration.

Finally, you would return to the caller.

Here is an example:

#include <stdio.h>

//Function to extract the longest common prefix
int findprefix(const char *str1, const char *str2, char *found) {
    int i;
    
    for (i = 0; str1[i] == str2[i] && str1[i] != '\0'; i++) {
        found[i] = str1[i];
    }
    found[i] = '\0';
    return i;
}

int main(void) {
    char str1[100];
    char str2[100];
    char prefix[100]; 
    
    printf("\nGive string 1: ");
    if (scanf("%99s", str1) != 1)
        return 1;
    printf("\nGive string 2: ");
    if (scanf("%99s", str2) != 1)
        return 1;
    
    //Print prefix
    findprefix(str1, str2, prefix); 
    printf("%s\n", prefix);
    
    return 0;
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

This declaration

char found[10] = { '\0' }; 

is redundant and does not make a sense.

Also the function findprefix should return the length of the common prefix.

The function should be declared and defined the following way

size_t findprefix( const char *str1, const char *str2 )
{
    size_t n = 0;

    for ( ; *str1 && *str1 == *str2; ++str1, ++str2 )
    {
        ++n;
    }

    return n;
}

And in main you can write

size_t n = findprefix( str1, str2 );

if ( n != 0 ) printf( "%.*s\n", ( int )n, str1 );

Here is a demonstration progarn.

#include <stdio.h>

size_t findprefix( const char *str1, const char *str2 )
{
    size_t n = 0;

    for ( ; *str1 && *str1 == *str2; ++str1, ++str2 )
    {
        ++n;
    }

    return n;
}

int main( void ) 
{
    const char *str1 = "Hello Word!";
    const char *str2 = "Hello Kallum Smith";
    
    size_t n = findprefix( str1, str2 );

    if ( n != 0 ) printf( "\"%.*s\"\n", ( int )n, str1 );
    
    return 0;
}

The program output is

"Hello "

Using the return value of the function you also can dynamically allocate an array or declare a variable length array where you can copy the prefix if it is required.

Upvotes: 1

Related Questions