【问题标题】:C: print the longest common prefixC:打印最长公共前缀
【发布时间】:2022-01-11 04:23:45
【问题描述】:

这里是初学者,试图弄清楚如何在 C 中查找和打印最长的公共前缀。

我在这里有一个程序的基地。

#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 () {
        }
    }
}

最初的想法是在函数中使用for 循环和if 语句,但我不确定如何。

【问题讨论】:

  • 我想你希望“findprefix”函数返回一个int,最长公共前缀的长度。
  • @MarkLavin size_t 会更合适。
  • 如果输入可以达到100字节,为什么found[10]只有10字节?
  • 调用后的字符串你关心吗?那么它们应该是const char *malloc。或许更好的方法是将char的数量返回相同;可以传递给printf“%.*s”吗?

标签: c c-strings string-comparison prefix function-definition


【解决方案1】:

此声明

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

是多余的,没有意义。

函数findprefix 也应该返回公共前缀的长度。

函数的声明和定义方式如下

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

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

    return n;
}

在 main 你可以写

size_t n = findprefix( str1, str2 );

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

这是一个演示程序。

#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;
}

程序输出是

"Hello "

使用函数的返回值,您还可以动态分配一个数组或声明一个可变长度数组,如果需要,您可以在其中复制前缀。

【讨论】:

    【解决方案2】:

    你有一个很好的基础,除了你应该定义prefix的长度为100的病理情况。

    在函数中,您应该使用从0 开始的索引i 进行迭代,比较来自str1str2 的字符在偏移量i 处,并在它们不同或其中一个为空时停止字节(值为 0 的 char),否则将字节复制到 found 数组中的相同偏移量 i

    循环之后。您将在 found 中在您停止迭代的偏移处存储一个空字节。

    最后,你会返回给调用者。

    这是一个例子:

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-11
      • 1970-01-01
      • 2022-11-22
      • 2018-09-30
      • 2013-04-14
      • 2012-02-01
      • 2020-07-05
      • 2020-02-10
      • 2021-10-12
      相关资源
      最近更新 更多