首页 文章

C中的Anagram:我如何知道int数组的每个元素是否都设置为零?

提问于
浏览
0

这段代码部分有效,它认为某些字谜是字谜,而其他字眼不是 . 我认为错误发生在isZero中 . 真的很感激一些反馈 . 以下是每项功能的说明 .

Algorithm 读取第一个字符串,然后使用26个整数的数组来计算每个字母被看到的次数 . 读取第二个字符串,这次减少int数组中每个字母的计数 . 当且仅当int数组中的每个元素都为0时,字符串才是字符串 . 忽略任何非字母的字符 . 将大写字母视为与小写字母相同 . 有关这方面的帮助如下 .

main() 声明两个char数组和int数组,然后调用下面描述的函数来解决问题 .

initialize() 在读取第一个字符串之前清除所有char和int数组的内容 . (将char数组中的每个元素设置为空字符'\ 0' . )

getString() 从输入中提示并读取字符串 . 在执行此操作时,请调用标准库函数gets() .

setLetters() 循环第一个字符串中的每个字符,并更新int数组中该字母的计数 . 从这里调用以下标准库函数 . int isalpha&char tolower在setLetters()中,您需要将'a'...'z'范围内的小写字符转换为0..25范围内的索引 . 使用这个想法:int index;

index = (int) (ch – ‘a’);

checkLetters() 循环第二个字符串中的每个字符,并从int数组中该字母的计数中减去1 . 与setLetters()非常相似 .

isZero() 遍历int数组 . 当且仅当每个元素都为0时返回TRUE,否则返回FALSE .

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

#define MAX 26
#define TRUE 1
#define FALSE 0

void initialize (char string1[], char string2[], int count[]);
void getString (char string1[], char string2[]);
void setLetters (char string1[], int count[]);
void checkLetters (char string2[], int count[]);
int isZero (int count[]);

void main (void)
{
    char string1[MAX], string2[MAX];
    int count[MAX];

    while (TRUE)
    {
        initialize (string1, string2, count);
        getString (string1, string2);
        setLetters (string1, count);
        checkLetters (string2, count);

        //printf("\n");
        if (isZero(count))
        {
            printf("Anagram ");
        }
        else
        {
            printf("Not anagram ");
        }
        printf("\n");
    }
}

void initialize (char string1[], char string2[], int count[])
{
    int i;

    for (i = 0; i < MAX; i++)
    {
        string1[i] = '\0';
        string2[i] = '\0';
        count[i] = 0;
    }
}

void getString (char string1[], char string2[])
{
    printf("\n");

    printf("Enter string:  ");
    gets(string1);

    printf("Enter string:  ");
    gets(string2);
}

void setLetters (char string1[], int count[])
{
    int i, 
        index = 0;

    for (i = 0; i < MAX; i++)
    {
        if (isalpha(string1[i]))
        {
            string1[i] = tolower(string1[i]);
            index = (int) (string1[i] - 'a');
            count[index] = (count[index] + 1);
        }
    }
}

void checkLetters (char string2[], count[])
{
    int i, 
        index = 0;

    for (i = 0; i < MAX; i++)
    {
        if (isalpha(string2[i]))
        {
            string2[i] = tolower(string2[i]);
            index = (int) (string2[i] - 'a');
            count[index] = (count[index] - 1);
        }
    }
}

int isZero (int count[])
{
    int i;

    for (i = 0; i < MAX; i++)
    {
        if (count[i])
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
}

3 回答

  • 2

    您的isZero应如下所示 . 如果数组具有非零值,则返回 TRUE .

    int isZero (int count[])
    {
        int i;
    
        for (i = 0; i < MAX; i++)
        {   
            if (count[i])
            {   
                return FALSE;
            }   
        }   
        return TRUE;
    }
    
  • 1

    现在你的函数 isZero 将在遇到数字> 0 时立即 return True . 所以你可以像这样重写你的功能 -

    int isZero (int count[])
    {
        int i; 
        for(i=0;i<MAX;i++){
            if(count[i])
               return 1 ;  //as soon as number other than 0 is ecnountered function return 1
        } 
        return 0;      // return 0 if all elements are 0 
    }
    

    注意 1 - False0-True .

  • 1

    我检查过你是零,两个问题1.你想要返回的真是一切都是零,不是假的 . 2.检查完所有元素后,您将返回true .

    int isZero (int count[])
    {
    int i;
    
    for (i = 0; i < MAX; i++)
    {
        if (count[i])
        {
            return FALSE;
        }
    }
    return TRUE;
    }
    

相关问题