Find the characters with the least frequency in the string and remove them.

Source: Internet
Author: User

Find the characters with the least frequency in the string and remove them.

It seems that it was a written test question from a company that was written earlier. I can't quite remember it. It seems that I wrote a complete version when I came back that night, but now I remember to write a small note.

Alas, it's hard to find a job now. You have no relevant project experience or internship experience. You have been sentenced to death for recent graduates and non-prestigious graduates! Don't say you have a good foundation! Willing to suffer! Hard work! I love to work overtime! Nobody birds you! It cannot afford to hurt ~

Okay, let's talk about the question:

The idea is that the string is only in English. That's simple. We can check the ASCII table. We can see that the decimal range of lowercase letters is 97 to 122, and that of uppercase letters is 65 to 90. Then when I make statistics, the condition is that they correspond to the decimal number. The lower-case letter statistical value is put into an array, and the upper-case letter statistical number is put into an array, and then unified into an array.

The size of the array is 26. This simplifies the problem. It is assumed that only English letters are allowed.

I think the written code is more intuitive and easy to understand.

/* Program Function Description: find and remove the letters with the minimum frequency in the string; Author: NerohHwang; Date: 10/9/2013 Wednesday; */# include <iostream> # include <string. h> using namespace std; int main () {char * Str = "dhajkhfkablcjnakjyhrweuipqhydhjasbvfjhlksdabkljhslkmm"; // The following two arrays are lower-case and upper-case, finally, it is unified into an array int iArrSmall [26]; int iArrBig [26]; int iSum [26]; // statistics for (int I = 0; I <26; I ++) {iArrSmall [I] = iArrBig [I] = 0; // All Initialization is 0} for (int I = 0; I <strlen (Str ); I ++) {if (Str [I]> 96 & Str [I] <123) // if it is greater than 96, it is within the lowercase letter range; of course, cannot exceed the lower-case z range {iArrSmall [Str [I]-97] ++;} else if (Str [I] <= 90 & Str [I]> 64) // uppercase letter range {iArrBig [Str [I]-65] ++;} else {cout <"Invalid letter! "<Endl; system (" pause "); exit (1) ;}}for (int I = 0; I <26; ++ I) {iSum [I] = iArrBig [I] + iArrSmall [I];} // select the minimum statistical result. The statistical value must be greater than zero, that is, the letter must contain int iSmallestIndex = iSum [0]; for (int I = 1; I <26; ++ I) {if (iSmallestIndex> iSum [I] & iSum [I]> 0) {iSmallestIndex = I ;}} cout <"The smallest letter is" <char (iSmallestIndex + 97) <"or" <char (iSmallestIndex + 65) <endl; cout <"There are "<ISum [iSmallestIndex] <" of them "<endl; // update the data char strNew [256]; int index = 0; for (int I = 0; I <strlen (Str); I ++) {if (int (Str [I])! = (ISmallestIndex + 97) & int (Str [I])! = (ISmallestIndex + 65) {strNew [index] = Str [I]; index ++ ;}} strNew [index + 1] = '\ 0 '; cout <strlen (strNew) <endl; cout <strNew <endl; // re-assign a value to the address to overwrite the original data Str = strNew; cout <Str <endl; return 0 ;}

 

Related Article

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.