C中的程序返回两个字符串之间的类似字符的数量,而无需多次计数字母
我目前正在尝试在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数组<代码>发现未初始化,
因此函数
包含
使用非专业化阵列调用不确定的行为。另一个问题是,在
字母
密码[i]
中存在字符串Word Chosen 多次一次,尚未在数组
中写入。也似乎是,如果语句
您应该写这件事,
而不是为此注意,而不是您的手动书面函数
包含
您可以使用标准C String函数strchr
或memchr
。这是一个演示程序。
程序输出是
因为您可以看到函数定义不取决于
strlen
的值。也可以在不使用辅助阵列的情况下定义该函数。
给你。
The array
found
is not initializedSo the function
contains
using the uninitialized array invokes undefined behavior.Another problem is that inside the inner for loop
the variable
like
can be incremented several times when the letterpassword[i]
is present in the stringwordchosen
more than one time and is not yet written in the arrayfound
..Also it seems that instead of this if statement
you should write this one
Pay attention to that instead of your manually written function
contains
you could use standard C string functionstrchr
ormemchr
.Here is a demonstration program.
The program output is
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.