-1

im having a bit of trouble with my code and im in need of suggestions.This may seem simple to you guys so im sorry for that.I need to compare the string v to string s and see how many times a combination of either aa,ee,ea,ae matches the string v and count the results.

What i have so far is this :

int main (int argc, const char * argv[])

{
    NSString *s = @"aebeee";
    NSString *v =@"ae";
    NSInteger length = v.length -1 ;
    BOOL isGrup = FALSE;


    for ( int i= 0 ; i<length; i++) {
        if([s characterAtIndex:i] == [v characterAtIndex:0 ]){
            isGrup = TRUE;
        }

        if(isGrup ==1)
        {
        NSLog(@" is equal");
        }else{
         NSLog(@" is not equal");   
        }
    }

    return(0);
}

I know i should delete the code after isGrup = TRUE; but i only did that to test and see if the results match so far.As you see it works but this only works for one char, how do i make it go to the next one and count my results?

user2384735
  • 29
  • 1
  • 4
  • Isn't [that](http://stackoverflow.com/questions/2166809/number-of-occurrences-of-a-substring-in-an-nsstring) what you want? :) and btw we use `YES` and `NO` instead of `TRUE` and `FALSE` in Objective-C :) – HAS Aug 02 '13 at 11:02
  • Thanks for pointing that out:) no the code as it is only compares "a" with "a" and tells me the result.I want it to compare the rest of the letters also. – user2384735 Aug 02 '13 at 11:57
  • Well, that's exactly what [Matthew does in his answer](http://stackoverflow.com/a/2166919/1489885). He even uses a loop! In the end `count` is the number of occurrences ;) – HAS Aug 02 '13 at 12:42

2 Answers2

0

what is NSString? could you replace it with char?

rigortek
  • 25
  • 5
  • Yeah but i need to use NSString :The NSString class declares the programmatic interface for an object that manages immutable strings. An immutable string is a text string that is defined when it is created and subsequently cannot be changed. NSString is implemented to represent an array of Unicode characters, in other words, a text string. – user2384735 Aug 02 '13 at 10:39
0

A way of thinking it :
Use rangeOfString to get the first occurrence of your string.
Then, rangeOfString:options:range starting at the NSLocation (from previous range) and again and again.

Larme
  • 24,190
  • 6
  • 51
  • 81
  • There has to be a simpler way to do it, i though of that and it wont look good if i do it like that. – user2384735 Aug 02 '13 at 10:39
  • 1
    http://stackoverflow.com/questions/2166809/number-of-occurrences-of-a-substring-in-an-nsstring Response of gwdp... – Larme Aug 02 '13 at 10:42
  • They dont use loops i also need to use loops sorry forgot to mention that. – user2384735 Aug 02 '13 at 11:16
  • 1
    Why do you need to use loops? Is it a homework assignment or what? Actually, I'm pretty sure they use loops (inside Apple's implementation of the used methods) ;) – HAS Aug 02 '13 at 11:18
  • Yes you can call it that i guess.I can only use integer/string and for loops. – user2384735 Aug 02 '13 at 11:27