0
제 할당의 질문은 모든 문자의 수를 배열에 저장해야한다는 것입니다. 소문자와 대문자는 동일하게 취급됩니다. 내가 사용한 전략은 모든 문자를 대문자로 변환 한 다음 계산하는 것이 었습니다. 그러나 출력을 인쇄하려고 할 때 정확한 출력을 제공하지 못합니다.배열에 파일의 문자 수를 저장하십시오.
예를 들어, 내가 읽은 파일을했다 :
ADDadd
을하지만 출력은 각 문자 나에게 어떤 미친 숫자를했다.
여기 내 코드는 (좀 지저분)입니다 :
void loadText(char story[])
{
FILE *input;
int letter = 0;
int other = 0;
int totalcharacter;
char words;
input = fopen(story, "r");
while(fscanf(input, "%c", &words) != EOF)
{
if ((words >= 'A') && (words <= 'z'))
{
letter++;
}
else
{
other++;
}
}
totalcharacter = letter + other;
printf("Letters: %d\n", letter);
printf("Characters: %d\n", totalcharacter);
int typeofletter[26];
int i = 0;
while(fscanf(input, "%c", &words) != EOF)
{
if ((words >= 'a') && (words <= 'z'))
{
words = toupper(words);
}
if((words >= 'A') && (words<='Z'))
{
typeofletter[words - 'A']++;
}
}
for(i = 0; i < 26; i++)
{
if(typeofletter[i] != 0)
{
printf("%c occurs %d times \n", i + 'A', typeofletter[i]);
}
}
}
문자를 불러오는 것은 꽤 혼란 스럽습니다. – zubergu
'typeofletter' 배열을 0으로 초기화 해보십시오. 'int typeofletter [26] = {0}' – GoldRoger
실제로 파일을 열 었는지 알 수 없으므로 fopen의 반환을 확인하지 마십시오. 둘째, fscanf가 두 번째 루프에서 읽기를 계속 수행한다고 생각합니까? – Asthor