@implementation LWTest
/*
1、计算字符串中所有整数的和(15分)
传入:@"1abc23def456feg7h"
返回:@"487"
*/
- (NSString *)sumOfIntegerInString:(NSString *)str {
NSMutableArray *ary = [NSMutableArray new];
NSMutableString *str1 = [NSMutableString stringWithString:str];
for (int i = 0; i < [str1 length]; i ++ ) {
unichar ch = [str1 characterAtIndex:i];
if (!(ch >= '0' && ch <= '9')) {//将字符串中不是数字的字符全都换为“ ”
[str1 replaceCharactersInRange:NSMakeRange(i, 1) withString:@" "];
}
}
//根据“ ”切割字符串,留在数组中的都是数字部分
ary =(NSMutableArray *) [str1 componentsSeparatedByString:@" "];
[ary removeObject:@""];
//NSLog(@"%@",ary);
int sum = 0;
for (int k = 0; k < ary.count; k++) {
sum = sum + [ary[k] intValue];//将数字进行处理后相加
}
return [NSString stringWithFormat:@"%d",sum];
}
/*
2、将一个字符串中的字符按照ASCII码从小到大的顺序排序(15分)
传入:@"Lifeislikeridingabicycletokeepyourblanceyoumustkeepmoving"
返回:@"Laabbcccdeeeeeeeefggiiiiiiikkklllmmnnnoooopprrssttuuuvyyy"
*/
- (NSString *)sortString:(NSString *)str {
NSMutableArray *ary = [NSMutableArray new];
NSMutableString *str1 = [NSMutableString new];
for (int i = 0; i < [str length]; i++) {
[str1 appendFormat:@"%c ",[str characterAtIndex:i]];//将字符串重新组合,变成由空格相隔的字符串
}
ary = (NSMutableArray *)[str1 componentsSeparatedByString:@" "];//将字符串切割存放到数组中
[ary removeObject:@""];//删除数组中的空元素
for (int j = 0; j < ary.count-1; j++) {
for (int k = 0; k < ary.count-j-1; k++) {
if ([ary[k] compare:ary[k+1]] == NSOrderedDescending) {
[ary exchangeObjectAtIndex:k withObjectAtIndex:k+1];//用冒泡排序对数组进行排序
}
}
}
return [ary componentsJoinedByString:@""];
}
/*
3、传入一个数组,返回数组中第二长元素。
已知数组中的对象都是字符串,且任意两元素长度不等。(15分)
如传入:@[@"12",@"234",@"3",@"4231"],返回:@"234"
*/
- (NSString *)secondLongItem:(NSArray *)ary {
//假设第一第二打元素是前两个中的某一个
NSUInteger maxIndex = [ary[0] length]>[ary[1] length]?0:1;
NSUInteger secondIndex = [ary[0] length]>[ary[1] length]?1:0;
for (int i = 2 ; i < ary.count; i++) {
if ([ary[i] length] > [ary[maxIndex] length]) {//如果大于最大的,就把最大的赋值给第二大的
secondIndex = maxIndex;
maxIndex = i;
}else if ([ary[i] length] > [ary[secondIndex] length]){
maxIndex = i;
}
}
return ary[secondIndex];
}
/*
4、给定一个字符串,按单词分开,单词是全大写或全小写,
转换时首字母大写,其余全部转换为小写,并用空格隔开(15分)
传入:@"whereTHEREisAwillTHEREisAway"
返回:@"Where there is a will there is a way"
*/
- (NSString *)printByWord:(NSString *)str {
NSMutableString *str1 = [NSMutableString new];
//将字符串按单词放入一个新的字符串中
for (int i = 0; i < [str length]-1; i++) {
if ((([str characterAtIndex:i] >= 'a'&&[str characterAtIndex:i]<= 'z') && ([str characterAtIndex:i+1]>= 'A'&&[str characterAtIndex:i+1]<='Z'))||(([str characterAtIndex:i+1] >= 'a'&&[str characterAtIndex:i+1]<= 'z')&&([str characterAtIndex:i]>= 'A'&&[str characterAtIndex:i]<='Z'))) {
[str1 appendFormat:@"%c ",[str characterAtIndex:i]];
}else{
[str1 appendFormat:@"%c",[str characterAtIndex:i]];
}
}
[str1 appendFormat:@"%c",[str characterAtIndex:[str length]-1]];//将最后一个字符加到字符串中
//NSLog(@"%@",str1);
NSMutableArray *ary =(NSMutableArray *) [str1 componentsSeparatedByString:@" "];//将字符串切割放到数组中
//NSLog(@"%@",ary);
NSMutableArray *ary1 = [NSMutableArray new];
//判断第一个单词是大写还是小写,如果是小写,首字母变成大写,
//如果是大写,首字母不变,其余变成小写
[ ary1 addObject:[ary[0] capitalizedString]];//capitalizedString将单词首字母 变大写,其余小写
for (int i = 1; i < ary.count; i++) {
if ([ary[i] characterAtIndex:0] >= 'A' && [ary[i] characterAtIndex:0] <= 'Z') {
[ary1 addObject:[ary[i] lowercaseString]];
}else{
[ary1 addObject:ary[i]];
}
}
return [ary1 componentsJoinedByString:@" "];
}
/*
5.解压缩字符串(15分)
传入:@"a10b3Cd5e"
返回:@"aaaaaaaaaabbbCddddde"
提示:次数可能超过10
*/
- (NSString *)decompressString:(NSString *)str {
//思路是将字母存放在一个数组中,把原字符串中的字母所在的位置换成“ ”,之后对原字符串按“ ”切割,得到其中的数字部分单独存放到数组中
NSMutableString *str1 = [NSMutableString stringWithString:str];
NSMutableArray *ary = [NSMutableArray new];
NSMutableString *str2 = [NSMutableString new];
NSMutableString *str3 = [NSMutableString new];
for (int i = 0; i < [str1 length]; i++) {
if (([str1 characterAtIndex:i] >= 'a' && [str1 characterAtIndex:i] <= 'z') || ([str1 characterAtIndex:i] >= 'A' && [str1 characterAtIndex:i] <= 'Z')) {
[str2 appendFormat:@"%c",[str1 characterAtIndex:i]];
[str1 replaceCharactersInRange:NSMakeRange(i, 1) withString:@" "];
}
}
ary = (NSMutableArray *)[str1 componentsSeparatedByString:@" "];
for (int k = 1; k < ary.count; k++) {
if ([ary[k] isEqualToString:@""]) {//数组中的空元素是由于原字符串中字符连续,在换成空格的时候,有相邻空格,所以切割的时候会出现空元素,这时候只需要在数组的空位置部分补上1即可
[ary replaceObjectAtIndex:k withObject:[NSString stringWithFormat:@"%d",1]];
}
}
[ary removeObject:@"" inRange:NSMakeRange(0, 1)];//这一步是处理数组第一个元素
for (int j = 0; j < ary.count; j++) {
unichar ch = [str2 characterAtIndex:j];
for (int k = 0; k < [ary[j] intValue]; k++) {
[str3 appendFormat:@"%c",ch];//这是解压缩部分,数组中的元素和新字符串中的元素是一一对应的,数组中的元素代表的是字符串中字符出现的次数
}
}
return (NSString *)str3;
}
/*
6、根据传入字符串,打印对应图形(15分)
如传入字符串@"ABCDEFG",打印
A
BAB
CB BC
DC CD
ED DE
FE EF
GF FG
G G
图形类似两个^叠摞
*/
- (void)printGraph:(NSString *)str {
//打印图形的小窍门就是将C语言写出来的每一行应该输出地东西加到一个可变字符串中
NSMutableString *str1 = [NSMutableString new];
NSUInteger len = [str length];
for (int i = 0 ; i < len+1 ; i++) {
for (int j = 0; j < 2*len-1 ; j++) {
if (i + j == len-1 || (j - i == len - 1)) {
[str1 appendFormat:@"%c",[str characterAtIndex:i]];
}else if(i >= 1 && ((j + i == len) || (j - i == len - 2))){
[str1 appendFormat:@"%c",[str characterAtIndex:i-1]];
}else{
[str1 appendFormat:@" "];
}
}
[str1 appendFormat:@"\n"];
}
NSLog(@"\n%@",str1);
}
/*
7.已知字符串中的单词间由单个空格隔开,实现函数,返回字符串s中出现次数最多的单词与次数组成的字符串,(10分)。
传入:@"drink your drink don't drink others drink"
返回:@"drink4"
*/
- (NSString *)mostWordInString:(NSString *)s {
//只需要用两个量记录最大值出现的次数和最大值对应的单词的下标就可以了
NSMutableArray *ary = (NSMutableArray *)[s componentsSeparatedByString:@" "];
[ary removeObject:@""];
NSUInteger maxCount = 1;
NSUInteger maxIndex = 0;
NSUInteger count = 1;
for (int i = 0; i < ary.count; i++) {
count = 1;
if ([ary[i] isEqualToString:@""]) {
continue;
}
for (int j = i+1; j < ary.count; j++) {
if ([ary[i] isEqualToString:ary[j]]) {
count++;
[ary replaceObjectAtIndex:j withObject:@" "];
}
}
if (count > maxCount) {
maxCount = count;
maxIndex = i;
}
}
NSString *str = [NSString stringWithFormat:@"%@%lu",ary[(int)maxIndex],maxCount];
return str;
}
@end
/*
1、计算字符串中所有整数的和(15分)
传入:@"1abc23def456feg7h"
返回:@"487"
*/
- (NSString *)sumOfIntegerInString:(NSString *)str {
NSMutableArray *ary = [NSMutableArray new];
NSMutableString *str1 = [NSMutableString stringWithString:str];
for (int i = 0; i < [str1 length]; i ++ ) {
unichar ch = [str1 characterAtIndex:i];
if (!(ch >= '0' && ch <= '9')) {//将字符串中不是数字的字符全都换为“ ”
[str1 replaceCharactersInRange:NSMakeRange(i, 1) withString:@" "];
}
}
//根据“ ”切割字符串,留在数组中的都是数字部分
ary =(NSMutableArray *) [str1 componentsSeparatedByString:@" "];
[ary removeObject:@""];
//NSLog(@"%@",ary);
int sum = 0;
for (int k = 0; k < ary.count; k++) {
sum = sum + [ary[k] intValue];//将数字进行处理后相加
}
return [NSString stringWithFormat:@"%d",sum];
}
/*
2、将一个字符串中的字符按照ASCII码从小到大的顺序排序(15分)
传入:@"Lifeislikeridingabicycletokeepyourblanceyoumustkeepmoving"
返回:@"Laabbcccdeeeeeeeefggiiiiiiikkklllmmnnnoooopprrssttuuuvyyy"
*/
- (NSString *)sortString:(NSString *)str {
NSMutableArray *ary = [NSMutableArray new];
NSMutableString *str1 = [NSMutableString new];
for (int i = 0; i < [str length]; i++) {
[str1 appendFormat:@"%c ",[str characterAtIndex:i]];//将字符串重新组合,变成由空格相隔的字符串
}
ary = (NSMutableArray *)[str1 componentsSeparatedByString:@" "];//将字符串切割存放到数组中
[ary removeObject:@""];//删除数组中的空元素
for (int j = 0; j < ary.count-1; j++) {
for (int k = 0; k < ary.count-j-1; k++) {
if ([ary[k] compare:ary[k+1]] == NSOrderedDescending) {
[ary exchangeObjectAtIndex:k withObjectAtIndex:k+1];//用冒泡排序对数组进行排序
}
}
}
return [ary componentsJoinedByString:@""];
}
/*
3、传入一个数组,返回数组中第二长元素。
已知数组中的对象都是字符串,且任意两元素长度不等。(15分)
如传入:@[@"12",@"234",@"3",@"4231"],返回:@"234"
*/
- (NSString *)secondLongItem:(NSArray *)ary {
//假设第一第二打元素是前两个中的某一个
NSUInteger maxIndex = [ary[0] length]>[ary[1] length]?0:1;
NSUInteger secondIndex = [ary[0] length]>[ary[1] length]?1:0;
for (int i = 2 ; i < ary.count; i++) {
if ([ary[i] length] > [ary[maxIndex] length]) {//如果大于最大的,就把最大的赋值给第二大的
secondIndex = maxIndex;
maxIndex = i;
}else if ([ary[i] length] > [ary[secondIndex] length]){
maxIndex = i;
}
}
return ary[secondIndex];
}
/*
4、给定一个字符串,按单词分开,单词是全大写或全小写,
转换时首字母大写,其余全部转换为小写,并用空格隔开(15分)
传入:@"whereTHEREisAwillTHEREisAway"
返回:@"Where there is a will there is a way"
*/
- (NSString *)printByWord:(NSString *)str {
NSMutableString *str1 = [NSMutableString new];
//将字符串按单词放入一个新的字符串中
for (int i = 0; i < [str length]-1; i++) {
if ((([str characterAtIndex:i] >= 'a'&&[str characterAtIndex:i]<= 'z') && ([str characterAtIndex:i+1]>= 'A'&&[str characterAtIndex:i+1]<='Z'))||(([str characterAtIndex:i+1] >= 'a'&&[str characterAtIndex:i+1]<= 'z')&&([str characterAtIndex:i]>= 'A'&&[str characterAtIndex:i]<='Z'))) {
[str1 appendFormat:@"%c ",[str characterAtIndex:i]];
}else{
[str1 appendFormat:@"%c",[str characterAtIndex:i]];
}
}
[str1 appendFormat:@"%c",[str characterAtIndex:[str length]-1]];//将最后一个字符加到字符串中
//NSLog(@"%@",str1);
NSMutableArray *ary =(NSMutableArray *) [str1 componentsSeparatedByString:@" "];//将字符串切割放到数组中
//NSLog(@"%@",ary);
NSMutableArray *ary1 = [NSMutableArray new];
//判断第一个单词是大写还是小写,如果是小写,首字母变成大写,
//如果是大写,首字母不变,其余变成小写
[ ary1 addObject:[ary[0] capitalizedString]];//capitalizedString将单词首字母 变大写,其余小写
for (int i = 1; i < ary.count; i++) {
if ([ary[i] characterAtIndex:0] >= 'A' && [ary[i] characterAtIndex:0] <= 'Z') {
[ary1 addObject:[ary[i] lowercaseString]];
}else{
[ary1 addObject:ary[i]];
}
}
return [ary1 componentsJoinedByString:@" "];
}
/*
5.解压缩字符串(15分)
传入:@"a10b3Cd5e"
返回:@"aaaaaaaaaabbbCddddde"
提示:次数可能超过10
*/
- (NSString *)decompressString:(NSString *)str {
//思路是将字母存放在一个数组中,把原字符串中的字母所在的位置换成“ ”,之后对原字符串按“ ”切割,得到其中的数字部分单独存放到数组中
NSMutableString *str1 = [NSMutableString stringWithString:str];
NSMutableArray *ary = [NSMutableArray new];
NSMutableString *str2 = [NSMutableString new];
NSMutableString *str3 = [NSMutableString new];
for (int i = 0; i < [str1 length]; i++) {
if (([str1 characterAtIndex:i] >= 'a' && [str1 characterAtIndex:i] <= 'z') || ([str1 characterAtIndex:i] >= 'A' && [str1 characterAtIndex:i] <= 'Z')) {
[str2 appendFormat:@"%c",[str1 characterAtIndex:i]];
[str1 replaceCharactersInRange:NSMakeRange(i, 1) withString:@" "];
}
}
ary = (NSMutableArray *)[str1 componentsSeparatedByString:@" "];
for (int k = 1; k < ary.count; k++) {
if ([ary[k] isEqualToString:@""]) {//数组中的空元素是由于原字符串中字符连续,在换成空格的时候,有相邻空格,所以切割的时候会出现空元素,这时候只需要在数组的空位置部分补上1即可
[ary replaceObjectAtIndex:k withObject:[NSString stringWithFormat:@"%d",1]];
}
}
[ary removeObject:@"" inRange:NSMakeRange(0, 1)];//这一步是处理数组第一个元素
for (int j = 0; j < ary.count; j++) {
unichar ch = [str2 characterAtIndex:j];
for (int k = 0; k < [ary[j] intValue]; k++) {
[str3 appendFormat:@"%c",ch];//这是解压缩部分,数组中的元素和新字符串中的元素是一一对应的,数组中的元素代表的是字符串中字符出现的次数
}
}
return (NSString *)str3;
}
/*
6、根据传入字符串,打印对应图形(15分)
如传入字符串@"ABCDEFG",打印
A
BAB
CB BC
DC CD
ED DE
FE EF
GF FG
G G
图形类似两个^叠摞
*/
- (void)printGraph:(NSString *)str {
//打印图形的小窍门就是将C语言写出来的每一行应该输出地东西加到一个可变字符串中
NSMutableString *str1 = [NSMutableString new];
NSUInteger len = [str length];
for (int i = 0 ; i < len+1 ; i++) {
for (int j = 0; j < 2*len-1 ; j++) {
if (i + j == len-1 || (j - i == len - 1)) {
[str1 appendFormat:@"%c",[str characterAtIndex:i]];
}else if(i >= 1 && ((j + i == len) || (j - i == len - 2))){
[str1 appendFormat:@"%c",[str characterAtIndex:i-1]];
}else{
[str1 appendFormat:@" "];
}
}
[str1 appendFormat:@"\n"];
}
NSLog(@"\n%@",str1);
}
/*
7.已知字符串中的单词间由单个空格隔开,实现函数,返回字符串s中出现次数最多的单词与次数组成的字符串,(10分)。
传入:@"drink your drink don't drink others drink"
返回:@"drink4"
*/
- (NSString *)mostWordInString:(NSString *)s {
//只需要用两个量记录最大值出现的次数和最大值对应的单词的下标就可以了
NSMutableArray *ary = (NSMutableArray *)[s componentsSeparatedByString:@" "];
[ary removeObject:@""];
NSUInteger maxCount = 1;
NSUInteger maxIndex = 0;
NSUInteger count = 1;
for (int i = 0; i < ary.count; i++) {
count = 1;
if ([ary[i] isEqualToString:@""]) {
continue;
}
for (int j = i+1; j < ary.count; j++) {
if ([ary[i] isEqualToString:ary[j]]) {
count++;
[ary replaceObjectAtIndex:j withObject:@" "];
}
}
if (count > maxCount) {
maxCount = count;
maxIndex = i;
}
}
NSString *str = [NSString stringWithFormat:@"%@%lu",ary[(int)maxIndex],maxCount];
return str;
}
@end