Check if string is palindrome in objective c

前端 未结 10 2217
执念已碎
执念已碎 2021-01-07 02:15

I\'m trying to check if a string is palindrome or not using objective c. I\'m new to programming without any experience in other programming languages so bear with me please

相关标签:
10条回答
  • 2021-01-07 02:47
    @import Foundation;
    
    BOOL isPalindrome(NSString * str)
    {
        if (!str || str.length == 0) return NO;
        if (str.length == 1) return YES;
        for(unsigned i = 0; i < str.length / 2; ++i)
            if ([str characterAtIndex:i] != [str characterAtIndex:str.length - i - 1]) return NO;
        return YES;
    }
    
    int main() {
        @autoreleasepool {
            NSLog(@"%s", isPalindrome(@"applelppa") ? "YES" : "NO");
        } return 0;
    }
    
    0 讨论(0)
  • 2021-01-07 02:50
    var str: NSString = "123321"
    var length = str.length
    var isPalindrome = true
    
    for index in 0...length/2{
        if(str.characterAtIndex(index) != str.characterAtIndex(length-1 - index)){
            print("\(index )not palindrome")
            isPalindrome = false
            break
        }
    }
    
    print("is palindrome: \(isPalindrome)")
    
    0 讨论(0)
  • 2021-01-07 02:51
    -(BOOL)checkPalindromeNumber:(int)number{
        int originalNumber,reversedNumber = 0,remainder;
        originalNumber=number;
        while (number!=0) {
            remainder=number%10;
            reversedNumber=(reversedNumber*10)+remainder;
            number=number/10;
        }
    
        if (reversedNumber==originalNumber) {
            NSLog(@"%d is Palindrome Number",originalNumber);
    
            return YES;
        }
        else{
            NSLog(@"%d is Not Palindrome Number",originalNumber);
            return NO;
    
        }
    }
    
    0 讨论(0)
  • 2021-01-07 02:53

    As it seems there's no answer yet that handles composed character sequences correctly I'm adding my two cents:

    NSString *testString = @"\u00E0 a\u0300"; // "à à"
    
    NSMutableArray *logicalCharacters = [NSMutableArray array];
    [testString enumerateSubstringsInRange:(NSRange){0, [testString length]}
                                   options:NSStringEnumerationByComposedCharacterSequences
                                usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
    {
        [logicalCharacters addObject:substring];
    }];
    
    NSUInteger count = [logicalCharacters count];
    BOOL isPalindrome = YES;
    for (NSUInteger idx = 0; idx < count / 2; ++idx) {
        NSString *a = logicalCharacters[idx];
        NSString *b = logicalCharacters[count - idx - 1];
        if ([a localizedCaseInsensitiveCompare:b] != NSOrderedSame) {
            isPalindrome = NO;
            break;
        }
    }
    
    NSLog(@"isPalindrome: %d", isPalindrome);
    

    This splits the string into an array of logical characters (elements of a string that a normal user would call a "character").

    0 讨论(0)
提交回复
热议问题