/*
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