@implementation LWTest
/*
1.找出数组中单词最多的元素(15分)
数组中的元素都是字符串,字符串中的单词以空格隔开,返回数组中单词最多的元素的索引(注:单词个数都不一样)
传入:@[@"hello world", @"Oh my God Jone", @"What the fuck"]
返回:1
*/
- (NSUInteger)indexOfMostWordOfStringInArray:(NSArray *)arr {
NSUInteger maxCount = 0 ;
NSUInteger index = 0;
for (int i = 0; i < arr.count; i++) {
NSArray *ary1 = [arr[i] componentsSeparatedByString:@" "];
[(NSMutableArray *)ary1 removeObject:@""];//将数组中的每一个元素按“ ”切割,放到数组中,数组的长度就是单词的个数
NSUInteger count = [ary1 count];
if (count > maxCount) {
maxCount = count;
index = i;
}
}
return index;
}
/**
2.(15分)已知我的邮箱的用户名只能由数字字母下滑线组成,域名为@wwwphone.com,判断一个字符串是否是千锋邮箱,是,返回1,不是返回0。
mail@wwwphone.com 是
$mail@wwwphone.com 不是
mali@wwwphone.comp 不是
*/
- (BOOL)is1000PhoneMailString:(NSString *)str {
NSArray *ary = [str componentsSeparatedByString:@"@"];
NSMutableString *str1 = [NSMutableString stringWithString:ary[1]];
[str1 insertString:@"@" atIndex:0];
int flag = 0;
if (![str1 isEqualToString:@"@1000Phone.com"]) {
return 0;
}else{
NSUInteger len = [ary[0] length];
for (int i = 0; i<len; i++) {
flag = 0;
unichar ch1 = [ary[0] characterAtIndex:i];
if (ch1 != '_' && !(ch1 >= '0' && ch1 <= '9') && !((ch1 >= 'a' && ch1 <= 'z') || (ch1 >= 'A' && ch1 <= 'Z')) ) {
flag = 1;
break;
}
}
if (flag == 1) {
return 0;
}else{
return 1;
}
}
}
/*
3.计算两个字符串所表示数字的和(15分)
传入:@"123" @"459"
返回:@"582"
*/
- (NSString *)sumOfNumber:(NSString *)string1 andNumber:(NSString *)string2 {
int value1 = [string1 intValue];
int value2 = [string2 intValue];
return [NSString stringWithFormat:@"%d",value1+value2];
}
/*
4.路径解析 15分
取得路径path最后的部分
比如
传入:/home/xuanmac/
返回:xuanmac
传入:/home/xuanmac/1.txt
返回:1.txt
*/
- (NSString *)lastPathComponent:(NSString *)path {
NSArray *ary = [path componentsSeparatedByString:@"/"];//以'/'切割,放到数组中
NSUInteger count = [ary count];
NSString *str0 = [ary objectAtIndex:count-1];
if ([str0 compare:@""] == NSOrderedSame) {//判断数组最后一个元素是不是空,也就是判断原字符串中最后一个字符是不是‘/’
return ary[count-2];//如果是,那么就返回数组中的第二个元素
}else{
return ary[count-1];//如果不是,就返回最后一个元素
}
}
/*
5.将字符串按照单词逆序 (15分)
输入一段字符串,已知字符串只由字母和空格构成,将字符串按照单词逆序
传入@"welcome to qianfeng" 返回 @"qianfeng to welcome"
*/
- (NSString *)reverseWordsInString:(NSString *)str {
NSArray *ary = [str componentsSeparatedByString:@" "];
NSMutableArray *ary1 = [NSMutableArray arrayWithArray:ary];
NSMutableArray *ary2 = [NSMutableArray new];
[ary1 removeObject:@""];
NSUInteger count = [ary1 count];
NSString *str1 = [NSString new];
for (int i = 0; i < count; i++) {
[ary2 addObject:ary1[count-1-i]];
str1 = [ary2 componentsJoinedByString:@" "];
}
return str1;
}
/*
6.根据输入的内容打印(15分)
//比如 [obj printString:@"12345"];
1 1
2 22
3 3 3
44 4
5 5
*/
- (void)printString:(NSString *)string {
NSUInteger len = [string length];
NSMutableString *str = [NSMutableString new];
int i;
for ( i = 0; i < len; i++) {
for (int j = 0; j <len; j++) {
if (j == 0) {
//printf("%c",[string characterAtIndex:i]);
[str appendFormat:@"%c",[string characterAtIndex:i]];
}else if(i + j == len-1 && j <len-1){
//printf("%c",[string characterAtIndex:i]);
[str appendFormat:@"%c",[string characterAtIndex:i]];
}else if(j == len-1){
//printf("%c",[string characterAtIndex:i]);
[str appendFormat:@"%c",[string characterAtIndex:i]];
}else{
//printf(" ");
[str appendFormat:@" "];
}
}
[str appendFormat:@"\n"];
}
NSLog(@"\n%@",str);
}
/*
7.将字符串中单词按照出现次数(次数都不一样)降序排序,排序之后单词只出现一次,源字符串中单词用下划线连接,生成字符串也用下滑线连接(10分)
如传入:@"good_good_study_good_study"
返回:@"good_study"
如传入:@"I_love_I_hate_love_love"
返回:@"love_I_hate"
*/
- (NSString *)sortStringByNumberOfWordsFromString:(NSString *)str {
NSMutableArray *ary = (NSMutableArray *)[str componentsSeparatedByString:@"_"];
[ary removeObject:@""];
NSUInteger count = ary.count;
NSMutableArray *ary1 = [NSMutableArray new];
NSMutableArray *ary2 = [NSMutableArray new];
NSMutableString *str1 = [NSMutableString new];
NSMutableString *str2 = [NSMutableString new];
int Count = 1;
for (int i = 0; i < count-1; i++) {
Count = 1;
if ([ary[i] isEqualToString:@""]) {
continue;
}
for (int j = i+1; j < count; j++) {
if ([ary[i] isEqualToString:ary[j]]) {
Count++;
ary[j] = @"";//字符串赋值方式
}
}
[str1 appendFormat:@"%d ",Count];
[str2 appendFormat:@"%@ ",ary[i]];
}
ary1 =(NSMutableArray *) [str1 componentsSeparatedByString:@" "];
[ary1 removeObject:@""];
ary2 =(NSMutableArray *) [str2 componentsSeparatedByString:@" "];
[ary2 removeObject:@""];
NSUInteger count1 = ary1.count;
for (int i = 0; i < count1-1; i++) {
for (int j = 0; j < count1-1-i; j++) {
if ([ary1[j] compare:ary1[j+1]] == NSOrderedAscending) {
[ary1 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
[ary2 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
NSString *str3 = [ary2 componentsJoinedByString:@"_"];
return str3;
}
@end
/*
1.找出数组中单词最多的元素(15分)
数组中的元素都是字符串,字符串中的单词以空格隔开,返回数组中单词最多的元素的索引(注:单词个数都不一样)
传入:@[@"hello world", @"Oh my God Jone", @"What the fuck"]
返回:1
*/
- (NSUInteger)indexOfMostWordOfStringInArray:(NSArray *)arr {
NSUInteger maxCount = 0 ;
NSUInteger index = 0;
for (int i = 0; i < arr.count; i++) {
NSArray *ary1 = [arr[i] componentsSeparatedByString:@" "];
[(NSMutableArray *)ary1 removeObject:@""];//将数组中的每一个元素按“ ”切割,放到数组中,数组的长度就是单词的个数
NSUInteger count = [ary1 count];
if (count > maxCount) {
maxCount = count;
index = i;
}
}
return index;
}
/**
2.(15分)已知我的邮箱的用户名只能由数字字母下滑线组成,域名为@wwwphone.com,判断一个字符串是否是千锋邮箱,是,返回1,不是返回0。
mail@wwwphone.com 是
$mail@wwwphone.com 不是
mali@wwwphone.comp 不是
*/
- (BOOL)is1000PhoneMailString:(NSString *)str {
NSArray *ary = [str componentsSeparatedByString:@"@"];
NSMutableString *str1 = [NSMutableString stringWithString:ary[1]];
[str1 insertString:@"@" atIndex:0];
int flag = 0;
if (![str1 isEqualToString:@"@1000Phone.com"]) {
return 0;
}else{
NSUInteger len = [ary[0] length];
for (int i = 0; i<len; i++) {
flag = 0;
unichar ch1 = [ary[0] characterAtIndex:i];
if (ch1 != '_' && !(ch1 >= '0' && ch1 <= '9') && !((ch1 >= 'a' && ch1 <= 'z') || (ch1 >= 'A' && ch1 <= 'Z')) ) {
flag = 1;
break;
}
}
if (flag == 1) {
return 0;
}else{
return 1;
}
}
}
/*
3.计算两个字符串所表示数字的和(15分)
传入:@"123" @"459"
返回:@"582"
*/
- (NSString *)sumOfNumber:(NSString *)string1 andNumber:(NSString *)string2 {
int value1 = [string1 intValue];
int value2 = [string2 intValue];
return [NSString stringWithFormat:@"%d",value1+value2];
}
/*
4.路径解析 15分
取得路径path最后的部分
比如
传入:/home/xuanmac/
返回:xuanmac
传入:/home/xuanmac/1.txt
返回:1.txt
*/
- (NSString *)lastPathComponent:(NSString *)path {
NSArray *ary = [path componentsSeparatedByString:@"/"];//以'/'切割,放到数组中
NSUInteger count = [ary count];
NSString *str0 = [ary objectAtIndex:count-1];
if ([str0 compare:@""] == NSOrderedSame) {//判断数组最后一个元素是不是空,也就是判断原字符串中最后一个字符是不是‘/’
return ary[count-2];//如果是,那么就返回数组中的第二个元素
}else{
return ary[count-1];//如果不是,就返回最后一个元素
}
}
/*
5.将字符串按照单词逆序 (15分)
输入一段字符串,已知字符串只由字母和空格构成,将字符串按照单词逆序
传入@"welcome to qianfeng" 返回 @"qianfeng to welcome"
*/
- (NSString *)reverseWordsInString:(NSString *)str {
NSArray *ary = [str componentsSeparatedByString:@" "];
NSMutableArray *ary1 = [NSMutableArray arrayWithArray:ary];
NSMutableArray *ary2 = [NSMutableArray new];
[ary1 removeObject:@""];
NSUInteger count = [ary1 count];
NSString *str1 = [NSString new];
for (int i = 0; i < count; i++) {
[ary2 addObject:ary1[count-1-i]];
str1 = [ary2 componentsJoinedByString:@" "];
}
return str1;
}
/*
6.根据输入的内容打印(15分)
//比如 [obj printString:@"12345"];
1 1
2 22
3 3 3
44 4
5 5
*/
- (void)printString:(NSString *)string {
NSUInteger len = [string length];
NSMutableString *str = [NSMutableString new];
int i;
for ( i = 0; i < len; i++) {
for (int j = 0; j <len; j++) {
if (j == 0) {
//printf("%c",[string characterAtIndex:i]);
[str appendFormat:@"%c",[string characterAtIndex:i]];
}else if(i + j == len-1 && j <len-1){
//printf("%c",[string characterAtIndex:i]);
[str appendFormat:@"%c",[string characterAtIndex:i]];
}else if(j == len-1){
//printf("%c",[string characterAtIndex:i]);
[str appendFormat:@"%c",[string characterAtIndex:i]];
}else{
//printf(" ");
[str appendFormat:@" "];
}
}
[str appendFormat:@"\n"];
}
NSLog(@"\n%@",str);
}
/*
7.将字符串中单词按照出现次数(次数都不一样)降序排序,排序之后单词只出现一次,源字符串中单词用下划线连接,生成字符串也用下滑线连接(10分)
如传入:@"good_good_study_good_study"
返回:@"good_study"
如传入:@"I_love_I_hate_love_love"
返回:@"love_I_hate"
*/
- (NSString *)sortStringByNumberOfWordsFromString:(NSString *)str {
NSMutableArray *ary = (NSMutableArray *)[str componentsSeparatedByString:@"_"];
[ary removeObject:@""];
NSUInteger count = ary.count;
NSMutableArray *ary1 = [NSMutableArray new];
NSMutableArray *ary2 = [NSMutableArray new];
NSMutableString *str1 = [NSMutableString new];
NSMutableString *str2 = [NSMutableString new];
int Count = 1;
for (int i = 0; i < count-1; i++) {
Count = 1;
if ([ary[i] isEqualToString:@""]) {
continue;
}
for (int j = i+1; j < count; j++) {
if ([ary[i] isEqualToString:ary[j]]) {
Count++;
ary[j] = @"";//字符串赋值方式
}
}
[str1 appendFormat:@"%d ",Count];
[str2 appendFormat:@"%@ ",ary[i]];
}
ary1 =(NSMutableArray *) [str1 componentsSeparatedByString:@" "];
[ary1 removeObject:@""];
ary2 =(NSMutableArray *) [str2 componentsSeparatedByString:@" "];
[ary2 removeObject:@""];
NSUInteger count1 = ary1.count;
for (int i = 0; i < count1-1; i++) {
for (int j = 0; j < count1-1-i; j++) {
if ([ary1[j] compare:ary1[j+1]] == NSOrderedAscending) {
[ary1 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
[ary2 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
NSString *str3 = [ary2 componentsJoinedByString:@"_"];
return str3;
}
@end