我正在尝试编写一个布尔函数,该功能检查字符串输入是否具有任何重复字符
我在main
下编写了此功能,我想检查字符串输入是否具有任何重复的字符,但是当我使用调试功能时,事实证明,当内部循环循环正确循环时,外循环仅循环一次。我不知道为什么外循环不应该像字符串的长度那样多次吗?由于某种原因不起作用,是的,它是CS50任务
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
bool only_letters(string s);
bool duplicts(string b);
int main(int argc, string argv[])
{
if(argc!=2){
printf("wrong input\n");
return 1;
}
int length=strlen(argv[1]);
if(length!=26){
printf("Key must contain 26 characters.\n");
return 1;
}
if(!only_letters(argv[1])){
printf("key must contain only alphapetic letters.\n");
return 1;
}
if(!duplicts(argv[1])){
printf("key must not contain same characters\n");
return 1;
}
}
// need a check input function if letters or not
bool only_letters(string s){
int length=strlen(s);
for(int i =0;i<length;i++){
if(!isalpha(s[i]))
return false;
}return true;
}
bool duplicts(string b) {
int length = strlen(b);
for (int i = 0; i < length; i++) {
for (int k = i + 1; k < length; k++) {
tolower(b[i]);
tolower(b[k]);
if (b[i] == b[k])
return false;
}
}
return true;
}
I wrote this function under main
I want to check if the string input has any duplicate characters but when I used the debug feature it turns out that the outer loop only loops once while the inner loop loops correctly. I don't know why the outer loop shouldn't iterate as many times as the length of the string ?,, im sorry i ran into another problem the program needs to be case insensitive to i tried integrating tolower in the bool function but it does not work for some reason ,yes its a cs50 task
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
bool only_letters(string s);
bool duplicts(string b);
int main(int argc, string argv[])
{
if(argc!=2){
printf("wrong input\n");
return 1;
}
int length=strlen(argv[1]);
if(length!=26){
printf("Key must contain 26 characters.\n");
return 1;
}
if(!only_letters(argv[1])){
printf("key must contain only alphapetic letters.\n");
return 1;
}
if(!duplicts(argv[1])){
printf("key must not contain same characters\n");
return 1;
}
}
// need a check input function if letters or not
bool only_letters(string s){
int length=strlen(s);
for(int i =0;i<length;i++){
if(!isalpha(s[i]))
return false;
}return true;
}
bool duplicts(string b) {
int length = strlen(b);
for (int i = 0; i < length; i++) {
for (int k = i + 1; k < length; k++) {
tolower(b[i]);
tolower(b[k]);
if (b[i] == b[k])
return false;
}
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过明智的凹痕约定将代码重新格式化使错误显而易见:
返回计数;
在外循环内,因此仅迭代一次。还有另一个问题:内部循环应以
k = i + 1
开始,否则您将始终在超过1个字节的字符串中找到重复项。一旦找到重复的字符,您的代码就可以轻松修改以返回
false
:您可以进一步简化以避免调用
strlen
并处理任意的大字符串(尽管非常效率低下):要修复对大字符串效率低下的二次复杂性,您可以使用一个数组:
Reformating your code with a sensible indentation convention makes the mistake obvious:
The
return count;
is inside the outer loop, hence it only iterates once.There is another problem: the inner loop should start at
k = i + 1
otherwise you will always find duplicates in strings of more than 1 byte.Your code can be easily modified to return
false
as soon as a duplicate character has been found:You can further simplified to avoid calling
strlen
and handle arbitrary large strings (albeit very inefficiently):To fix the quadratic complexity which is inefficient for large strings, you can use an array: