0

私のアプリケーションでは、単語全体ではなく単語を比較する必要があります。単語内の同じ場所にある場合、文字を認識してほしい。すべての単語の最大長は 6 です。

両方の単語がラベルに表示されます。

ラベル1 & ラベル2

たとえば、label1 の単語が「ボタン」の場合、それを 6 つの文字列に分割します。

string1: B
string2: u
string3: t
string4: t 
string5: o
string6: n

そして、私のlabel2は「レンガ」で、6つに分割されています。

string7: B
string8: r
string9: i
string10: c 
string11: k
 string12: s

これで、文字列 1:string7 などを比較できます。

このようにして、単語内のすべての文字を比較できますよね? 私の質問は、これは正しい方法ですか?それが正しい場合、コードはどのように見えるでしょうか?

誰かが私の意味を理解し、これを行う方法を知っていることを願っています! ありがとうございました

4

1 に答える 1

0

私はこのようなことをします:

- (void)findEqualsIn:(NSString *)string1 and:(NSString *)string2 {
    for (int i = 0; i < [string1 length] && i < [string2 length]; i++) {
        if ([string1 characterAtIndex:i] == [string2 characterAtIndex:i]) {
            NSLog(@"%c is at index %i of both strings", [string1 characterAtIndex:i], i);
        }
    }
}

あなたがそれで何をしたいのか、どのように情報を返したいのかわかりません(おそらく、一致するすべてのインデックスを持つ NSArray ですか?)

編集

- (void)findEqualsIn:(NSString *)string1 and:(NSString *)string2 {
    NSMutableArray *string1chars = [[NSMutableArray alloc] init];
    NSMutableArray *string2chars = [[NSMutableArray alloc] init];

    //filling the string1chars array
    for (int i = 0; i < [string1 length]; i++) {
        [string1chars addObject:[NSString stringWithFormat:@"%c", [string1 characterAtIndex:i]]];
    }

    //filling the string2chars array
    for (int i = 0; i < [string2 length]; i++) {
        [string2chars addObject:[NSString stringWithFormat:@"%c", [string2 characterAtIndex:i]]];
    }

    //checking if they have some letters in common on the same spot
    for (int i = 0; i < [string1chars count] && i < [string2chars count]; i++) {
        if ([[string1chars objectAtIndex:i] isEqualToString:[string2chars objectAtIndex:i]]) {
            //change the color of the character at index i to green
        } else {
            //change the color of the character at index i to the standard color
        }
    }
}
于 2012-04-23T10:27:31.097 に答える