用于字谜的C编码(如何忽略句点)

时间:2015-04-20 02:52:34

标签: c

这是我到目前为止编码字谜的代码。一切似乎是正确的,除了某些原因,虽然它不计数撇号它确实计算期间,使一些东西应该是字谜,而不是。我该如何解决这个问题?

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

#define MAX 25 
#define LETTERS 26 

void initialize(int * letters, char * string1, char * string2); 
void setletters (char string1[], int letters[]);
void checkletters (char string2[], int letters[]);
char *getstring(char s[]);  
int isZero(int letters[]); 


int main(void)
{ 
    int letters[LETTERS]; 
    char string1[MAX]; 
    char string2[MAX]; 
    while(1){
        initialize(letters, string1, string2); 
        getstring(string1); 
        setletters(string1, letters); 
        getstring(string2); 
        checkletters (string2, letters);  


        if (isZero(letters)) 
            printf("Anagram \n \n");
        else
            printf("Not an Anagram \n \n");
    }
    return 0;
}


void initialize(int * letters, char * string1, char * string2)     
{
    int count = 0; 
    for(; count < LETTERS;  count++)
    {
        letters[count]= 0;
    }

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

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

    return; 
}



char * getstring(char string1[])
{ 
    char * errorcheck; 
    printf("Enter line:  "); 
    errorcheck = gets(string1);
    if (errorcheck)
        return string1; 
    return NULL;            
}

void setletters (char string1[], int letters[]) 
{
    int count,
    numletter = 0;

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

    return;
}

void checkletters (char string2[], int letters[]) 
{
    int count,
    numletter = 0;

    for(count = 0;(count < MAX) ; count++)
    {    
        if(isalpha(string2[count])) 
        {
            string2[numletter] = tolower(string2[numletter]);
            numletter = (string2[count] - 'a');
            letters[numletter] = (letters[numletter] - 1);
        }        
    }
    return;
}


int isZero(int letters[]) 
{ 
    int numletter = 0;

    for(; numletter < LETTERS; numletter ++)
    {
        if (letters[numletter])
            return 0; 
    }
    return 1;
}

1 个答案:

答案 0 :(得分:1)

setletterscheckletters中,您都有一个像:

的块
if(isalpha(string1[count])) 
{
    string1[numletter] = tolower(string1[numletter]); // <==
    numletter =(int) (string1[count] - 'a');
    letters[numletter] = (letters[numletter] + 1);
}

numletter应该是来自a的字母的偏移量,但您使用它来索引string1。您打算在这两个地方使用count