奇怪的可可虫?

发布于 2024-09-25 02:06:28 字数 1362 浏览 2 评论 0原文

嘿伙计们,下面是我用于学校作业的一段代码。 每当我输入一个带有 O 的单词(大写 o)时,它都会失败! 每当这个程序中有一个或多个大写 O 时,它就会返回 false 并记录 : 句子不是回文。

对于那些不知道什么是回文的人来说,回文是一个从左到右、倒着读的单词。 (例如哈哈、皮划艇、复兴者等) 我在尝试检查有史以来发现的“最古老的”回文时发现了这个错误:SATOR AREPO TENET OPERA ROTAS。

当我将所有大写 o 更改为小写 o 时,它起作用并返回 true。 让我清楚地说明一下,在这段代码中,所有大写 O 的句子/单词都返回 false。单个大写 o 就足以使该程序失败。

-(BOOL)testForPalindrome:(NSString *)s position:(NSInteger)pos {
    NSString *string = s;
    NSInteger position = pos;
    NSInteger stringLength = [string length];
    NSString *charOne = [string substringFromIndex:position];
    charOne = [charOne substringToIndex:1];

    NSString *charTwo = [string substringFromIndex:(stringLength - 1 - position)];
    charTwo = [charTwo substringToIndex:1];
    if(position > (stringLength / 2)) {
        NSString *printableString = [NSString stringWithFormat:@"De following word or sentence is a palindrome: \n\n%@", string];
        NSLog(@"%@ is a palindrome.", string);
        [textField setStringValue:printableString];
        return YES;
    }
    if(charOne != charTwo) {
        NSLog(@"%@, %@", charOne, charTwo);
        NSLog(@"%i", position);
        NSLog(@"%@ is not a palindrome.", string);
        return NO;
    }
    return [self testForPalindrome:string position:position+1]; 
}

那么,这是 Cocoa 中的一些奇怪的错误吗? 或者我错过了什么?

Hey folks, beneath is a piece of code i used for a school assignment.
Whenever I enter a word, with an O in it (which is a capital o), it fails!
Whenever there is one or more capital O's in this program, it returns false and logs : sentence not a palindrome.

A palindrome, for the people that dont know what a palindrome is, is a word that is the same read left from right, and backwards. (e.g. lol, kayak, reviver etc)
I found this bug when trying to check the 'oldest' palindrome ever found: SATOR AREPO TENET OPERA ROTAS.

When I change all the capital o's to lowercase o's, it works, and returns true.
Let me state clearly, with this piece of code ALL sentences/words with capital O's return false. A single capital o is enough to fail this program.

-(BOOL)testForPalindrome:(NSString *)s position:(NSInteger)pos {
    NSString *string = s;
    NSInteger position = pos;
    NSInteger stringLength = [string length];
    NSString *charOne = [string substringFromIndex:position];
    charOne = [charOne substringToIndex:1];

    NSString *charTwo = [string substringFromIndex:(stringLength - 1 - position)];
    charTwo = [charTwo substringToIndex:1];
    if(position > (stringLength / 2)) {
        NSString *printableString = [NSString stringWithFormat:@"De following word or sentence is a palindrome: \n\n%@", string];
        NSLog(@"%@ is a palindrome.", string);
        [textField setStringValue:printableString];
        return YES;
    }
    if(charOne != charTwo) {
        NSLog(@"%@, %@", charOne, charTwo);
        NSLog(@"%i", position);
        NSLog(@"%@ is not a palindrome.", string);
        return NO;
    }
    return [self testForPalindrome:string position:position+1]; 
}

So, is this some weird bug in Cocoa?
Or am I missing something?

  • B

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-10-02 02:06:29

这当然不是 Cocoa 中的错误,正如您内心深处可能知道的那样。

您的比较方法导致了这个“Cocoa 中的错误”,您正在比较 charOne 和 charTwo 的地址。相反,您应该将字符串的内容与 isEqualToString 消息进行比较。

使用:

if(![charOne isEqualToString:charTwo]) {

而不是:

if(charOne != charTwo) {

编辑:在测试项目中对其进行测试,并可以确认这是问题所在。

This of course is not a bug in Cocoa, as you probably knew deep down inside.

Your compare method is causing this 'bug in Cocoa', you're comparing the addresses of charOne and charTwo. Instead you should compare the contents of the string with the isEqualToString message.

Use:

if(![charOne isEqualToString:charTwo]) {

Instead of:

if(charOne != charTwo) {

Edit: tested it in a test project and can confirm this is the problem.

猛虎独行 2024-10-02 02:06:29

不要使用 charOne != charTwo

而是使用 NSString 比较方法。

if ([charOne caseInsensitiveCompare:charTwo] != NSOrderedSame)

它也可能与 本地化(但我对此表示怀疑)。

Don't use charOne != charTwo

Instead use one of the NSString Compare Methods.

if ([charOne caseInsensitiveCompare:charTwo] != NSOrderedSame)

It may also have to do with localization (but I doubt it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文