向程序中添加scanf和EOF #包括 #包括 int main() { 字符串[100]; int c=0,计数[26]={0}; int accum=0; printf(“输入字符串”); 获取(字符串); while(字符串[c]!='\0') { 如果(string[c]>='a'&&string[c]='a'&&string[c]

向程序中添加scanf和EOF #包括 #包括 int main() { 字符串[100]; int c=0,计数[26]={0}; int accum=0; printf(“输入字符串”); 获取(字符串); while(字符串[c]!='\0') { 如果(string[c]>='a'&&string[c]='a'&&string[c],c,scanf,eof,C,Scanf,Eof,添加printf并在while循环中获取语句 #include <stdio.h> #include <string.h> int main() { char string[100]; int c = 0, count[26] = {0}; int accum = 0; printf("Enter a string\n"); gets(string); while ( string[c] != '\0' ) {

添加printf并在while循环中获取语句

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

int main()
{
   char string[100];
   int c = 0, count[26] = {0};
   int accum = 0;

   printf("Enter a string\n");
   gets(string);

   while ( string[c] != '\0' )
   {

      if ( string[c] >= 'a' && string[c] <= 'z' ){
         count[string[c]-'a']++;
         accum++;
      }

      else if (string[c] >= 'A' && string[c] <= 'Z'){
          count[string[c]-'A']++;
          accum++;
      }
      c++;
   }

   for ( c = 0 ; c < 26 ; c++ )
   {
      if( count[c] != 0 )
          printf( "%c %f\n", c+'a', ((double)count[c])/accum);
   }

   return 0;
}

给你。我将每个循环的变量归零。我将一个空白条目(只需按Enter键)视为EOF。但是如果你想要所有字符串的统计信息,请不要将
while
循环开始时的值归零

printf("Enter a string\n");
gets(string);

while ( string[c] != '\0' )
{
     ...//remaining code inside while loop

     printf("Enter a string\n");
     gets(string);

}
... //for loop code and return 0;
#包括
#包括
#包括
int main()
{
字符串[100];
int c=0,计数[26]={0};
int accum=0;
做{
c=0;
累计=0;
memset(count,sizeof count,0);
获取(字符串);
if(strlen(string)<1)
中断;//终止
while(字符串[c]!='\0'){

如果(string[c]>='a'&&string[c]='a'&&string[c]使用输入的
scanf()

scanf()
出现EOF条件或IO错误时返回EOF

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


int main()
{
    char string[100];
    int c = 0, count[26] = {0};
    int accum = 0;

    do {
        c = 0;
        accum = 0;
        memset (count, sizeof count, 0);

        gets(string);
        if (strlen (string) < 1)
            break;      // terminate

        while ( string[c] != '\0' ) {
            if ( string[c] >= 'a' && string[c] <= 'z' ) {
                count[string[c]-'a']++;
                accum++;
                }

            else if (string[c] >= 'A' && string[c] <= 'Z') {
                count[string[c]-'A']++;
                accum++;
                }
            c++;
            }

        for ( c = 0 ; c < 26 ; c++ ) {
            if( count[c] != 0 )
                printf( "%c %f\n", c+'a', ((double)count[c])/accum);
            }
        }
    while (1);

    return 0;
}
//printf(“输入字符串”);
char ch;
而(scanf(“%c”,&ch)==1{

如果(ch>='a'&&ch),你的意思是你想要的行为类似于在EOF(控制台)之前反复运行的情况吗,但只需要运行一次程序?基本上,程序是使用scanf输入字符串直到EOF,然后计算所有输入字符串的字母频率。永远不要使用
获取
。这很危险,已从POSIX和C标准中退出。改为使用
fgets
。此行:((双)计数[C])/accum)将一个较小的数除以总的数。即,结果将小于1。但是,此代码使用整数除法,因此当除数大于被除数时,结果将始终为0。如何使用scanf进行此操作?是否不需要添加EOF?更改将转到scanf(“%s”,字符串);好的,EOF部分呢?@userDEV
 // printf("Enter a string\n");
 char ch;
 while (scanf("%c", &ch) == 1) {  
   if ( ch >= 'a' && ch <= 'z' ){
     count[ch-'a']++;
     accum++;
   }
   ...
 }