Home > Back-end >  [for] character statistics statistics, shed tears of what went wrong
[for] character statistics statistics, shed tears of what went wrong

Time:09-27

Line by line output characters in each sentence the most times and the number of occurrences of (if there are multiple characters at the same times, only a minimum of output ASCII characters),
#include
#include
#include
# define N 300
Int main ()
{
Int n={0} [n], Max, I;
Char a [N], ch, j;
While (gets) (a)!=NULL)
{
If (islower (a [I])!=0)
{
For (j='A'; J<='Z'; J++)
for(i=0; IIf (a==j [I])
N [j] + +;
}
Else if (isupper (a [I])!=0)
{
For (j='a'; J<='z'; J++)
for(i=0; IIf (a==j [I])
N [j] + +;
}
Ch='A', Max=n (ch);
For (j='A'; J<='z'; J++)
If (isalpha (j)!=0)
{
If (n [j] & gt; Max)
Max=n [j], ch=j;
Else if (n==max& [j]. & JMax=n [j], ch=j;
}
Printf (" % d % c \ n ", ch, Max).

}
return 0;
}
Input:
I am a student
A good programming problem
The ABCD ABCD ABCD ABCD
Output:
A 2
O 4
A 2
However:
A 2
A 2
A 2

CodePudding user response:

Where you function islower ()

CodePudding user response:

 # include 
#include
#include

//# define N 300
# define N 300
Int main ()
{
//int n={0} [n], Max, I, j;
//char a [N], ch.//, j;

Int n [52]={0}, I;
Int ch, Max;
Char a, [N].

While (the fgets (a, N, stdin)!=NULL)
{
i=0;
//puts (a);
While (a [I]) {
//putchar (a [I]);
If (isupper (a [I]))
N [a [I] - 'a'] + +;
If (islower (a [I]))
N [a [I] - 'a' + 26] + +;
i++;
}
Max=n [0];
For (I=1; i <52. I++)
If (Max & lt; N [I]) {
Max=n [I];
}
Printf (" Max=% d \ n ", Max).

For (I=0; i <52. I++)
If (Max==n [I]) {
If (I & gt;=26)
Ch='a' + (I - 26);
The else
Ch='A';
break;
}

Printf (" % d % c \ n ", ch, Max).

Memset (n, 0, sizeof (n));
# if 0
If (isalpha (j)!=0)
If (islower (a [I])!=0)
{
For (j='A'; J<='Z'; J++)
for(i=0; IIf (a==j [I])
N [j] + +;
}
Else if (isupper (a [I])!=0)
{
For (j='a'; J<='z'; J++)
for(i=0; IIf (a==j [I])
N [j] + +;
}
Ch='A', Max=n (ch);
For (j='A'; J<='z'; J++)
If (isalpha (j)!=0)
{
If (n [j] & gt; Max)
Max=n [j], ch=j;
Else if (n==max& [j]. & JMax=n [j], ch=j;
}
Printf (" % d % c \ n ", ch, Max).
# endif
}

return 0;
}

For your reference ~

CodePudding user response:

reference 1st floor Master Chicken response:
you function islower () where,

In the header file & lt; Ctype. H> The ~

CodePudding user response:

You that I don't have the initialization, the if and else if estimates have not entered, or enter a branch, I value also has always been the same
The problem is you make it complicated, actually can simply some
 # define N 300 
Int main ()
{
Int n [n], Max [n], I, CNT=0;
Char ch [N], a [N].
While (cnt{
If (gets) (a)==NULL) break;
Memset (n, 0, sizeof (n));
Ch/CNT=0;
Max (CNT)=0;
for (i=0; I{
If (isalpha (a [I]))
{//consider only English letters
the condition of theN [a] [I] + +;//the same character occurrences accumulative
If (n [a] [I] & gt; Max (CNT))
{//if there is a number greater than Max, update the Max and ch
Max (CNT)=n [a] [I];
Ch=[CNT] a [I];
}
Else if (n [a] [I]==Max/CNT)
{//if the occurrences and Max is the same, the update ch into smaller ASCII characters
If (ch (CNT) & gt; A [I])
Ch=[CNT] a [I];
}
}
}
cnt++;
}
for (i=0; I{
Printf (" % d % c \ n ", ch [I], Max [I]);
}
return 0;
}


  • Related