【问题标题】:I am trying to make a function that checks whether all characters in a string are unique, and returns 0 if this is so, or 1 if not all are unique我正在尝试创建一个函数来检查字符串中的所有字符是否都是唯一的,如果是这样,则返回 0,如果不是所有字符都是唯一的,则返回 1
【发布时间】:2022-01-20 06:26:01
【问题描述】:

我不确定我的代码有什么问题。当我更改字符串单词时,它似乎大多数时候都打印 0 。希望有任何帮助/cmets - 我的代码中的逻辑有问题吗?如果是这样,在哪里以及如何纠正?

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int check_unique_letters(string words);

int main (void)
{
    string word =  "ABCB" ;
    printf ("%i\n", check_unique_letters (word));
}


int check_unique_letters (string words)
{
    int j = 0;
    
    do
    {   int x = (int) (words [j]) ;
        int y = 0;
        for (int i=j + 1; i<strlen(words); i++)
        {
            if ((int)words[i] == x)
            {
                y += 1;
            }
            
            else
            {
                y+= 0;
            }
        }
        
        if (y>0)
        {
            return 1;
            break;
        }
        
        else
        {
            j = j+1;
        }
        
    }
    while (j < strlen (words));
}

【问题讨论】:

  • 如果外部do .. while 循环结束,那么你返回什么?
  • 不要在条件测试中使用strlen - 在这种情况下,它会将名义上的 O(n^2) 算法转换为 O(n^4)。获取一次长度并将其存储在变量中
  • @Alnitak 您的建议通常是合理的,但是在这里获取长度一次并将其存储在变量中会将应该是 O(1) 算法的算法转换为 O(n)。更好的方法是将strnlen(words, UCHAR_MAX+1) 存储在一个变量中(可能自己编写strnlen 的等价物,因为它不是标准C)。任何长于 UCHAR_MAX 的字符串都会在前 UCHAR_MAX+1 个字符中有重复。
  • @PaulHankin words 是输入字符串,而不是找到与未找到标志的数组。如果没有这样的数组,算法总是 O(n^2)
  • @Alnitak 这是此代码的 O(1) 版本:gist.github.com/paulhankin/363629b0eb731db3b60a1a40deaebfd2。使用 strlen 而不是 strnlen 的相同代码是 O(n)。

标签: c char unique cs50 c-strings


【解决方案1】:

该函数在 do-while 循环之外不返回任何内容,尽管它的返回类型不是 void。

也就是在do-while循环之后你需要放置语句

return 0;

注意,不需要像int这样使用类型的对象

int x = (int) (words [j]) ;

还有一个多余的代码没有这样的效果

        else
        {
            y+= 0;
        }

或者喜欢break语句

    if (y>0)
    {
        return 1;
        break;
    }

如果在所有符号都是唯一的情况下函数返回非零值,则逻辑上会更加一致。

同样调用函数strlen也是低效的。

我会这样写函数

#include <stdio.h>
#include <string.h>

int check_unique_letters( const char *s )
{
    if (*s)
    {
        while (s[1] != '\0' && strchr( s + 1, *s ) == NULL) ++s;
    }

    return *s == '\0' || s[1] == '\0';
}

int main( void )
{
    const char *s = "ABCB";

    printf( "All characters of the string \"%s\" are %s.\n",
            s, check_unique_letters( s ) ? "unique" : "not unique" );

    s = "ABCD";

    printf( "All characters of the string \"%s\" are %s.\n",
            s, check_unique_letters( s ) ? "unique" : "not unique" );
}

程序输出是

All characters of the string "ABCB" are not unique.
All characters of the string "ABCD" are unique.

【讨论】:

  • @Alnitak 函数 strcmp 返回三个值之一(负、零或正)。如果是问题中的函数,则它会回答“是”或“否”的问题。
【解决方案2】:

这里是您需要的快速实现。您可以快速比较可能出错的地方。 随意修改程序以满足您的需要。

你可以看到它正在运行here

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int check_unique_chars(char* str, int length)
{
// standard null checks
    if (length == 0 || str == NULL)
        return 1;
// The easiest way to do this is to, for each character
// compare every other character ahead of it. If there is a match, then return 1. Else return 0.
    for(int idx =0; idx < length; ++idx)
    {
        for(int idx1 =idx+1; idx1 < length; ++idx1)
        {
            if (str[idx] == str[idx1])
                return 1;
        }
    }
    return 0;
}

int main(void)
{
    char* s = "Hel0";
    char* s1 = "banana";
    printf("string has unique chars: %s\n", check_unique_chars(s, strlen(s)) == 0 ? "true" : "false");
    printf("string has unique chars: %s\n", check_unique_chars(s1, strlen(s1)) == 0 ? "true" : "false");
    return EXIT_SUCCESS;
}

我有理由确定这不是最有效的实现,但它的操作非常明显,您应该能够对其进行修改以满足您的需要。

【讨论】:

    猜你喜欢
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多