如何在C中的字符串中检查重复的字符 [英] How to check for repeated characters within a string in c

查看:86
本文介绍了如何在C中的字符串中检查重复的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,该程序检查命令行参数字符串中的重复字符.假定该字符串仅包含26个字符,并且所有字符都必须按字母顺序.但是,字符串中不能有任何重复的字符,每个字母字符只能出现一次.我想出了程序的前两个部分,但我想不出如何检查重复的字符.我真的可以使用一些帮助以及说明.

I'm trying to create a program that checks for repeated characters within the command line argument's string. The string is suppose to contain only 26 characters, and all characters have to be alphabetical. However, there cannot be any repeated characters within the string, each alphabetical character must appear only once. I figured out the first two sections of the program but I cant figure out how to check for repeated characters. I could really use some help as well as explanations.

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

int main (int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    else
    {
        int len = strlen(argv[1]);
        if (len != 26)
        {
            printf("Key must contain 26 characters.\n");
            return 1;
        }
        else
        {
            for (int i = 0; i < len; i++)
            {
                if (!isalpha(argv[1][i]))
                {
                    printf("Usage: ./substitution key\n");
                    return 1;
                }
            }
        }
    }
}

推荐答案

以下是解决方案的基础:

Here are the basics of a solution:

启动时,初始化标志表: char Seen [UCHAR_MAX + 1] = {0}; .在此数组中,当且仅当已经看到字符 c 时, Seen [c] 将为true(非零).(要获取 UCHAR_MAX #include< limits.h> .)

When starting, initialize a table of flags: char Seen[UCHAR_MAX+1] = {0};. In this array, Seen[c] will be true (non-zero) if and only if character c has been seen already. (To get UCHAR_MAX, #include <limits.h>.)

在处理每个字符时,将其复制到 unsigned char : unsigned char c = argv [1] [i]; .然后将其转换为大写: c = toupper(c); .然后测试它是否已经被看到:

When processing each character, copy it to an unsigned char: unsigned char c = argv[1][i];. Then convert it to uppercase: c = toupper(c);. Then test whether it has been seen already:

if (Seen[c])
    Report error...

如果它是新的,请记住它已经被看到: Seen [c] = 1; .

If it is new, remember that it has been seen: Seen[c] = 1;.

这就是必需的.

注意:

  • 如果已知A到Z在字符集中是连续的(就像在ASCII中一样),则可以将 char Seen [UCHAR_MAX + 1] 简化为 char Seen ['Z'-'A'+ 1] 并使用 Seen [c-'A'] 进行索引.(实际上,一个较弱的条件就足够了:A是最低的大写字符,Z是最大的.)
  • 不要试图使用 unsigned char c = toupper(argv [1] [i]),因为 toupper 是为 unsigned char 定义的.code>值和 char 值可能超出范围(负).
  • 在一个深奥的C实现中,其中 char 的宽度与 int 一样宽,但都不存在, UCHAR_MAX + 1 将评估为零.但是,如果发生这种情况,编译器应发出警告.
  • If it is known that A to Z are contiguous in the character set, as they are in ASCII, then char Seen[UCHAR_MAX+1] can be reduced to char Seen['Z'-'A'+1] and indexed using Seen[c-'A']. (Actually, a weaker condition suffices: A is the lowest uppercase character in value and Z is the greatest.)
  • Do not be tempted to use unsigned char c = toupper(argv[1][i]), because toupper is defined for unsigned char values, and a char value may be out of bounds (negative).
  • In an esoteric C implementation where a char is as wide as an int, none of which are known to exist, UCHAR_MAX+1 would evaluate to zero. However, the compiler should warn if this happens.

这篇关于如何在C中的字符串中检查重复的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆