Home > Back-end >  Statistical string in each number, number of symbols, in which each digital output
Statistical string in each number, number of symbols, in which each digital output

Time:04-26

#include
using namespace std;
Int main ()
{
Int sp=0, the lett=0, oth=0;
Int shu [10]={0};
Char [80] s={0};
Cout<" The input string: "& lt; Gets (s);
Cout<" The output string: "& lt; for(int i=0; i<80; I++)
CoutCoutCout<" The statistical result to: "& lt; for(int j=0; j!='\ n' & amp; & J<80; J++)
{
If (s) [j]==' ')
Sp++;
Else if (s) [j] & gt;='0' & amp; & S [j] <='9')
Shu [s] [j] + +;
Else if (s) [j] & gt;='a' & amp; & S [j] <='z' | | s [j] & gt;='A' & amp; & S [j] <='Z')
Lett++;
The else
Oth++;
}
For (int k=0; K<10; K++)
CoutCout<" \ n Spaces: "& lt; return 0;
}
Other characters in the output is always wrong, digital output abnormalities, abnormal Spaces sometimes,
For bosses to help analyze which wrong

CodePudding user response:

Modified as follows, for your reference:
 # include 
using namespace std;
Int main ()
{
Int sp=0, the lett=0, oth=0;
Int shu [10]={0};
Char [128] s={0};
Cout<" The input string: "& lt; Gets (s);
Cout<" The output string: "& lt; for(int i=0; S [I]!='\ 0'; I++)//for (int I=0; i<80; I++)
CoutCoutCout<" The statistical result to: "& lt; for(int j=0; S [j].='\ 0'; J++)//for (int j=0; j!='\ n' & amp; & J<80; J++)
{
If (s) [j]==' ')
Sp++;
Else if (s) [j] & gt;='0' & amp; & S [j] <='9')
Shu [[j] - '0' s] + +;//shu [s] [j] + +;
Else if ((s [j] & gt;='a' & amp; & S [j] <='z') | | (s [j] & gt;='A' & amp; & S [j] <='Z'))
Lett++;
The else
Oth++;
}
For (int k=0; K<10; K++)
CoutCout<" \ n Spaces: "& lt;
return 0;
}

CodePudding user response:


Int shu [10]={0};
Char [80] s={0};
Must be numeric characters statistics is problematic
Shu [s] [j] + +;
Change
Shu [[j] - '0' s] + +;
Ok,
Such other should also by the way,
Because of the original cause cross-border access, let others messing,

CodePudding user response:

Why will cross shu [[j] - '0' s] + + - what is the meaning of '0'

CodePudding user response:

Shu [10]={0} defines the 0-9 digital storage arrays, shu [10] the subscript is 0-9, when the code statistics shu [s] [j] + +, assuming that the s [j]='0', '0' character in the ASCII value is 48, this sentence is shu [48] + +, subscript bounds, characters' 0... 'and Numbers 0... In the computer is different, so to get the correct value should be this: shu [s [I] - '0'], characters' 1 'in the ASCII value of 49, when s [I]=' 1 ', shu [[j] - '0' s]=shu [49-48]=shu [1], and so on,

CodePudding user response:

Got it thank you,,,
  • Related