C中的程序返回两个字符串之间的类似字符的数量,而无需多次计数字母

发布于 2025-02-09 07:24:01 字数 1870 浏览 1 评论 0原文

我目前正在尝试在C中制作一个程序,以返回两个字符串之间的类似字符的数量,但没有多次计算字母。

检查两个字符串是否完全一样。

例如,使用“周”和“弱”,它应该返回3,因为字母'w','e'和'k'在两个词中都是。

两个字符串的大小仅为4。

我制作了“包含”功能,以检查我们是否尚未比较一个角色,例如使用“测试”和“ tast” Got 2“ T”,因此它的返回超出了预期。

目前,我的代码似乎可以正常工作,但我不明白为什么...

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

#define STRLEN 4

bool contains(char tab[], char c){
    for(int i=0; i<STRLEN; i++){
        if(tab[i] == c){
            return 1;
        }
    }
    return 0;
}

int likeness(char* password, char* wordchosen) {

    if(strlen(password)!=STRLEN && strlen(wordchosen)!=STRLEN) {
        return -1;
    }

    if(!strcmp(password, wordchosen)) {
        return 4;
    }

    int like = 0;
    char found[STRLEN];

    for(int i=0; i<STRLEN; i++) {
        for(int j=0; j<STRLEN; j++) {
            /*printf("%c : %c\n", password[i], wordchosen[j]);
            printf("\n");*/
            if(password[i] == wordchosen[j] && !(contains(found, wordchosen[j]))) {
                like++;
            }
        }
        found[i] = password[i];
    }

    return like;
}

int main() {
    printf("week and week = %d\n", likeness("week", "week")); // should be 4 (same word even if only 3 (w, e, k))
    printf("test and tast = %d\n", likeness("test", "tast")); // should be 2 (t, s)
    printf("week and weak = %d\n", likeness("week", "weak")); // should be 3 (w, e, k)
    printf("snet and sent = %d\n", likeness("snet", "sent")); // should be 4 (s, n, e, t)
    printf("gree and green = %d\n", likeness("gree", "gren")); // should be 3 (g, r, e)
    printf("mail and main = %d\n", likeness("mail", "main")); // should be 3 (m, a, i)
    printf("same = %d\n", likeness("same", "same")); // should be 4
    // opposite side in the likeness func. return different result...
    return 0;
}

I a currently trying to make a program in C to return the number of similar characters between two strings, but without counting a letter multiple times.

Checking if two strings are exactly the same too.

For example with "week" and "weak", it should return 3 because the letters 'w', 'e', and 'k' are in both words.

The size of the two strings are 4 only.

I made the "contains" function to check if we didn't already compared a character, for example with "test" and "tast" got 2 "t", so it was returning more than expected.

For now my code, seems to work but not correctly and I can't understand why...

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

#define STRLEN 4

bool contains(char tab[], char c){
    for(int i=0; i<STRLEN; i++){
        if(tab[i] == c){
            return 1;
        }
    }
    return 0;
}

int likeness(char* password, char* wordchosen) {

    if(strlen(password)!=STRLEN && strlen(wordchosen)!=STRLEN) {
        return -1;
    }

    if(!strcmp(password, wordchosen)) {
        return 4;
    }

    int like = 0;
    char found[STRLEN];

    for(int i=0; i<STRLEN; i++) {
        for(int j=0; j<STRLEN; j++) {
            /*printf("%c : %c\n", password[i], wordchosen[j]);
            printf("\n");*/
            if(password[i] == wordchosen[j] && !(contains(found, wordchosen[j]))) {
                like++;
            }
        }
        found[i] = password[i];
    }

    return like;
}

int main() {
    printf("week and week = %d\n", likeness("week", "week")); // should be 4 (same word even if only 3 (w, e, k))
    printf("test and tast = %d\n", likeness("test", "tast")); // should be 2 (t, s)
    printf("week and weak = %d\n", likeness("week", "weak")); // should be 3 (w, e, k)
    printf("snet and sent = %d\n", likeness("snet", "sent")); // should be 4 (s, n, e, t)
    printf("gree and green = %d\n", likeness("gree", "gren")); // should be 3 (g, r, e)
    printf("mail and main = %d\n", likeness("mail", "main")); // should be 3 (m, a, i)
    printf("same = %d\n", likeness("same", "same")); // should be 4
    // opposite side in the likeness func. return different result...
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2025-02-16 07:24:01

数组<代码>发现未初始化,

char found[STRLEN];

因此函数包含使用非专业化阵列调用不确定的行为。

另一个问题是,在

    for(int j=0; j<STRLEN; j++) {
        /*printf("%c : %c\n", password[i], wordchosen[j]);
        printf("\n");*/
        if(password[i] == wordchosen[j] && !(contains(found, wordchosen[j]))) {
            like++;
        }
    }

字母密码[i]中存在字符串Word Chosen 多次一次,尚未在数组中写入。

也似乎是,如果语句

if(strlen(password)!=STRLEN && strlen(wordchosen)!=STRLEN) {
    return -1;
}

您应该写这件事,

if(strlen(password)!=STRLEN || strlen(wordchosen)!=STRLEN) {
    return -1;
}

而不是为此注意,而不是您的手动书面函数包含您可以使用标准C String函数strchrmemchr

这是一个演示程序。

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

#define STRLEN 4

int likeness( const char *password, const char *wordchosen ) 
{
    if ( strlen( password ) != STRLEN || strlen( wordchosen ) != STRLEN ) 
    {
        return -1;
    }

    if ( strcmp( password, wordchosen ) == 0 ) 
    {
        return STRLEN;
    }

    int like = 0;
    char found[STRLEN + 1] = "";

    for ( ; *password; ++password )
    {
        if ( strchr( wordchosen, *password ) != NULL && 
             strchr( found, *password ) == NULL )
        {
            found[like++] = *password;
        }            
    }

    return like;
}

int main( void )
{
    printf("week and week = %d\n", likeness("week", "week")); // should be 4 (same word even if only 3 (w, e, k))
    printf("test and tast = %d\n", likeness("test", "tast")); // should be 2 (t, s)
    printf("week and weak = %d\n", likeness("week", "weak")); // should be 3 (w, e, k)
    printf("snet and sent = %d\n", likeness("snet", "sent")); // should be 4 (s, n, e, t)
    printf("gree and gren = %d\n", likeness("gree", "gren")); // should be 3 (g, r, e)
    printf("mail and main = %d\n", likeness("mail", "main")); // should be 3 (m, a, i)
    printf("same = %d\n", likeness("same", "same")); // should be 4
}

程序输出是

week and week = 4
test and tast = 2
week and weak = 3
snet and sent = 4
gree and gren = 3
mail and main = 3
same = 4

因为您可以看到函数定义不取决于strlen的值。

也可以在不使用辅助阵列的情况下定义该函数。

给你。

int likeness( const char *password, const char *wordchosen ) 
{
    if ( strlen( password ) != STRLEN || strlen( wordchosen ) != STRLEN ) 
    {
        return -1;
    }

    if ( strcmp( password, wordchosen ) == 0 ) 
    {
        return STRLEN;
    }

    int like = 0;

    for ( const char *p = password; *p; ++p )
    {
        if ( memchr( password, *p, p - password ) == NULL && 
             strchr( wordchosen, *p ) != NULL )
        {
            ++like;
        }            
    }

    return like;
}

The array found is not initialized

char found[STRLEN];

So the function contains using the uninitialized array invokes undefined behavior.

Another problem is that inside the inner for loop

    for(int j=0; j<STRLEN; j++) {
        /*printf("%c : %c\n", password[i], wordchosen[j]);
        printf("\n");*/
        if(password[i] == wordchosen[j] && !(contains(found, wordchosen[j]))) {
            like++;
        }
    }

the variable like can be incremented several times when the letter password[i] is present in the string wordchosen more than one time and is not yet written in the array found..

Also it seems that instead of this if statement

if(strlen(password)!=STRLEN && strlen(wordchosen)!=STRLEN) {
    return -1;
}

you should write this one

if(strlen(password)!=STRLEN || strlen(wordchosen)!=STRLEN) {
    return -1;
}

Pay attention to that instead of your manually written function contains you could use standard C string function strchr or memchr.

Here is a demonstration program.

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

#define STRLEN 4

int likeness( const char *password, const char *wordchosen ) 
{
    if ( strlen( password ) != STRLEN || strlen( wordchosen ) != STRLEN ) 
    {
        return -1;
    }

    if ( strcmp( password, wordchosen ) == 0 ) 
    {
        return STRLEN;
    }

    int like = 0;
    char found[STRLEN + 1] = "";

    for ( ; *password; ++password )
    {
        if ( strchr( wordchosen, *password ) != NULL && 
             strchr( found, *password ) == NULL )
        {
            found[like++] = *password;
        }            
    }

    return like;
}

int main( void )
{
    printf("week and week = %d\n", likeness("week", "week")); // should be 4 (same word even if only 3 (w, e, k))
    printf("test and tast = %d\n", likeness("test", "tast")); // should be 2 (t, s)
    printf("week and weak = %d\n", likeness("week", "weak")); // should be 3 (w, e, k)
    printf("snet and sent = %d\n", likeness("snet", "sent")); // should be 4 (s, n, e, t)
    printf("gree and gren = %d\n", likeness("gree", "gren")); // should be 3 (g, r, e)
    printf("mail and main = %d\n", likeness("mail", "main")); // should be 3 (m, a, i)
    printf("same = %d\n", likeness("same", "same")); // should be 4
}

The program output is

week and week = 4
test and tast = 2
week and weak = 3
snet and sent = 4
gree and gren = 3
mail and main = 3
same = 4

As you can see the function definition does not depend on the value of STRLEN.

The function can be defined also without using the auxiliary array.

Here you are.

int likeness( const char *password, const char *wordchosen ) 
{
    if ( strlen( password ) != STRLEN || strlen( wordchosen ) != STRLEN ) 
    {
        return -1;
    }

    if ( strcmp( password, wordchosen ) == 0 ) 
    {
        return STRLEN;
    }

    int like = 0;

    for ( const char *p = password; *p; ++p )
    {
        if ( memchr( password, *p, p - password ) == NULL && 
             strchr( wordchosen, *p ) != NULL )
        {
            ++like;
        }            
    }

    return like;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文