OC sorts the words in a string in descending order of occurrences (the number of times), after which the word appears only once, the word in the source string is underlined, and the resulting string is connected with a glide line

Source: Internet
Author: User
Tags sorts

/*
Sorts the words in a string in descending order of occurrences (the number of times), after which the word appears only once, the word in the source string is underlined, and the resulting string is connected with a glide line (10 points)
If incoming: @ "Good_good_study_good_study"
return: @ "Good_study"
If incoming: @ "I_love_i_hate_love_love"
return: @ "Love_i_hate"
*/

Method 1: Select sort

-(NSString *) sortstringbynumberofwordsfromstring: (NSString *) str
{
Nsarray *ary=[str componentsseparatedbystring:@ "_"];
Nsmutablearray *difary=[[nsmutablearray Alloc]init];
[Difary Addobject:ary[0]];
for (int i=0; i<ary.count; i++) {
BOOL Ret=no;
for (int m=0; m<difary.count; m++)
{
BOOL Ifret=[ary[i]isequaltostring:difary[m]];
if (ifret==yes) {ret=yes;break;}
}
if (ret==no) {[Difary addobject:ary[i];}
}
NSLog (@ "%@", difary);
int num=0;
int nun=0;
for (int m=0; m<difary.count; m++) {
for (int k=m; k<difary.count; k++) {
for (int n=0; n<ary.count; n++) {
if ([Difary[m]isequaltostring:ary[n]]==yes) {
num++;
}
if ([Difary[k]isequaltostring:ary[n]]==yes) {
nun++;
}
}
if (Num>nun)
{
NSString *temp=difary[m];
DIFARY[M]=DIFARY[K];
Difary[k]=temp;
}
num=0;
nun=0;
NSLog (@ "%@", difary);
}
}
NSString *arystr=[difary componentsjoinedbystring:@ "_"];
NSLog (@ "%@", arystr);
return arystr;
}

Method 2: Dictionary

-(NSString *) sortstringbynumberofwordsfromstrings: (NSString *) str
{
Nsmutabledictionary *dic=[[nsmutabledictionary alloc] init];
Nsarray *ary = [str componentsseparatedbystring:@ "_"];
nsmutablestring *mystr = [[Nsmutablestring alloc] init];
for (int i=0; i<ary.count; i++) {
Nsarray *ary1=[str Componentsseparatedbystring:ary[i]];
NSString *str1 = [Ary1 Componentsjoinedbystring:nil];
int k= (int) (str.length-str1.length)/[ary[i] length];
NSString *set = [NSString stringwithformat:@ "%d", K];
[DiC Setvalue:ary[i] forkey:set];
NSLog (@ "%@", str);
}
NSLog (@ "%@", DIC);
for (int j = (int) ary.count; j>0; j--) {
NSLog (@ "%@", [NSString stringwithformat:@ "%d", j]);
NSString *string = [dic objectforkey:[nsstring stringwithformat:@ "%d", j]];
NSLog (@ "%@", string);
if (String!=nil) {
[MyStr appendstring:@ "_"];
[MyStr appendstring:string];
}
}
[MyStr deletecharactersinrange:nsmakerange (0, 1)];
NSLog (@ "%@", mystr);
return mystr;
}

OC sorts the words in a string in descending order of occurrences (the number of times), after which the word appears only once, the word in the source string is underlined, and the resulting string is connected with a glide line

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.