Baidu C ++

Source: Internet
Author: User

The questions are:Http://post.baidu.com/F? Kz= 70481398
Count the number of letters:
ProgramEnter a string (up to 100 characters in length), all lowercase letters.
Count the number of occurrences of lower-case letters and use the required graph.
Test data:

Input:
Sadjhasdhqwpopeepomcxnnbladkjkfjasjas

output:
@< BR >< br> @@@@@@
a B c d e f g h I j k l m n o p q r s t u v w x y z
note:
the number of lowercase letters in a string cannot exceed 20.
programming language : C/C ++.

_________________________________________________
The question refers to the number of each letter and the number of @ output above @. I use an array (array int count [26]) with a length of 26 to save the number of each letter, then, a 26*20 array (the number of each letter cannot exceed 20) is used to record the final output result, the initialization in this array is a null character. Every time a letter is encountered, it is assigned a value of @. Finally, you only need to output the array from the highest row down to get the result, however, many of the preceding columns may be empty characters. to output data from the first row containing at least one non-empty character (I .e. @), I used a variable (INT max_count) to record the location of this row. The procedure is as follows:

# I nclude < Iostream >  
Using Namespace STD;

# DefineCount 20

Void Output ( Char * P)
{
Char Chars [ 26 ] [Count];
For ( Int I = 0 ; I < 26 ; I ++ )
For ( Int J = 0 ; J < Count; j ++ )
Chars [I] [J] = ' ' ;

IntCount [26];
For(IntI=0; I<26; I++)
Count [I]=0;

Int Max_count = 0 ;
Int Index = 0 ;
Int TMP = 0 ;
While ( * P ! = ' \ 0 ' )
{
Index = * P - ' A ' ;
TMP = ++ Count [Index];
If (Max_count < TMP)
Max_count = TMP;
Chars [Index] [TMP - 1 ] = ' @ ' ;
P ++ ;
}

For ( Int I = Max_count - 1 ; I > = 0 ; I -- )
{
For ( Int J = 0 ; J < 26 ; J ++ )
Cout < Chars [J] [I];
Cout < Endl;
}
For ( Char I = ' A ' ; I <= ' Z ' ; I ++ )
Cout < I;
}

IntMain ()
{
Char *Str="Sadjhasdhqwpopeepomcxnnbladkjkfjasjas",*P=STR;
Output (P );
Getchar ();
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.