简体   繁体   中英

I need to find common prefix between two strings in C++

My strncpy function is not working, shows argument of type "cons char" is in compatible with parameter type "char" And when I call out the prefix function in the main function it says i must have a pointer to function type

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

void prefix(const char s1[], const char s2[], char prefix[]);  

    int main()
    {
        char s1[30];
        char s2[30];
        char prefix[30];

        cout << "Enter two sentences to store in two different strings" << endl;
        cin.getline(s1, 30);
        cin.getline(s2, 30);

        prefix(s1, s2, prefix);

    }

    void prefix(const char a[], const char b[], char prefix[]) 
    {
        int size;
        if (strlen(a) < strlen(b))
        {
            size = strlen(a);
        }
        else if (strlen(a) > strlen(b))
        {
            size = strlen(b);
        }
        else
        {
            size = strlen(a);
        }


            for (int i = 0; i < size; i++) 
            {
                if (a[i] != b[i]) 
                {
                    strncpy(a, b, size);
                }
            }
    }

Not sure on your exact error, but it is probably like "error C2064: term does not evaluate to a function taking 3 arguments" or "error: 'prefix' cannot be used as a function".

The issue here is you declared a local variable with the name prefix , so it will take precedence over the global function prefix . Some types of variable may be callable (eg function pointers, std::function , etc.).

The best solution for that is generally to rename your local, but you can explicitly tell it to use the global scope if desired: ::prefix(s1, s2, prefix); .


There are further errors within the prefix function itself however, as strncpy(a, b, size); tries to copy to a "const" string, which is not allowed, presumably you meant to copy to the prefix string instead, and probably end the loop there.

However, for C++ it would also generally be better to use the std::string type. You can use std::getline(std::cin, my_std_string) to read lines, and prefix = my_std_string.substr(0, i) would be a way to copy part of a string.

For starters this declaration in main

char prefix[30];

hides the function with the same name declared in the global name space.

Either rename the function or the variable or use a qualified name for the function.

This loop

        for (int i = 0; i < size; i++) 
        {
            if (a[i] != b[i]) 
            {
                strncpy(a, b, size);
            }
        }

does not make sense and in this call

strncpy(a, b, size);

you are trying to change the constant array pointed to by the pointer a .

And there are many redundant calls of the function strlen .

The function can be declared and defined the following way as it is shown in the demonstrative program below.

#include <iostream>

char * common_prefix( const char s1[], const char s2[], char prefix[] )
{
    char *p = prefix;

    for ( ; *s1 != '\0' && *s1 == *s2; ++s1, ++s2 )
    {
        *p++ = *s1;
    }

    *p = '\0';

    return prefix;
}

int main() 
{
    const size_t N = 30;

    char s1[N];
    char s2[N];
    char prefix[N];

    std::cout << "Enter two sentences to store in two different strings" << '\n';
    std::cin.getline( s1, N );
    std::cin.getline( s2, N );

    std::cout << "The common prefix is \"" << common_prefix( s1, s2, prefix ) 
              << "\"\n";

    return 0;
}

Its output might look like

Enter two sentences to store in two different strings
Hello C#
Hello C++
The common prefix is "Hello C"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM