Check if string is palindrome in objective c

前端 未结 10 2333
执念已碎
执念已碎 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;
    }
    

提交回复
热议问题