【发布时间】:2016-06-07 19:40:22
【问题描述】:
我尝试了很多东西,但是当我选择 2 来分别计算大写和小写时,结果是垃圾,我不知道这有什么问题,而不管是小写还是大写仍然可以正常工作。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int i,h,k ,count[26] = {0}, nam[26] = {0};
char c[1000];
FILE *fptr;
fptr=fopen("tep1.txt","w");
if(fptr==NULL){
printf("Error!");
exit(1);
}
printf("a String please:\n");
gets(c);
fprintf(fptr,"%s",c);
fclose(fptr);
printf("discern between upper case and lower case? 0=no, 1=yes");
scanf("%d",&h);
if(h==0){
while (c[i] != '\0'){
if (c[i] >= 'a' && c[i] <= 'z')
count[c[i]-'a']++;
if (c[i]>='A' &&c[i]<='Z')
count[c[i]-'A']++;
i++;
}
for (i = 0; i < 26; i++){
if (count[i] != 0)
printf("%c %c appears %d times on file.\n",i+'a',i+'A',count[i]);
}
return 0;
}
if(h==1){
while (c[i]|c[k] != '\0') {
if (c[i] >= 'a' && c[i] <= 'z')
count[c[i]-'a']++;
i++;
if (c[k]>='A' &&c[k]<='Z')
nam[c[k]-'A']++;
k++;
}
for (i = 0; i < 26; i++){
if (count[i] != 0)
printf("%c %c appears %d times on file.\n",i+'a',count[i]);
}
for (k = 0; k < 26; k++){
if (nam[k] != 0)
printf("%c %c appears %d times on file.\n",k+'A',nam[k]);
}
return 0;
}
}
【问题讨论】:
-
也添加输出,以便我们澄清您的问题。
-
w 和(一些未知字符)在文件中出现 26856400 次
-
您的代码没有任何问题。 ideone.com/dOfdEh
-
int i,h,k:i和k需要初始化。printf("%c %c appears %d times on file.\n",i+'a',count[i]);和printf("%c %c appears %d times on file.\n",k+'A',nam[k]);参数不匹配。 -
感谢您的帮助Avinash,尝试选项1,选项0没有问题
标签: c algorithm uppercase lowercase