我需要在C ++中找到两个字符串之间的公共前缀

问题描述 投票:0回答:2

我的strncpy函数不起作用,显示类型为“ cons char”的参数与参数类型“ char”兼容当我在主函数中调出前缀函数时,它说我必须有一个指向函数类型的指针

#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);
                }
            }
    }
c++ algorithm for-loop substring c-strings
2个回答
1
投票

不确定您的确切错误,但可能类似于“错误C2064:术语未评估为带有3个参数的函数”或“错误:'前缀'不能用作函数”。


0
投票

对于初学者,此声明位于主要位置

© www.soinside.com 2019 - 2024. All rights reserved.