Um, I have a very strange question...

Discussion in 'Mac Programming' started by ausername, Apr 7, 2009.

  1. macrumors newbie

    Joined:
    Feb 28, 2009
    Messages:
    25
    #1
    Okay, not sure if this can be done but, what I want to do is... say that I have 3 int variables named a, b, and c... Now say that I have an NSString containing some letters, and have a loop run through the string letter by letter, and each time the letter 'a' is found, it would increment the variable a by 1, and the same for b and c. But something like this would require an if tree like this:

    NSString *myString = @"abjbcaubuaedhoca"; // some random letters
    NSString *thisLetter;
    int a = 0;
    int b = 0;
    int c = 0;

    for (int i = 0; i < [myString length]; i++) {
    thisLetter = [this_word substringWithRange:NSMakeRange(i, 1)]; // gets each letter.

    if ([thisLetter isEqualToString:mad:"a"])
    a++;
    else if ([thisLetter isEqualToString:mad:"b"])
    b++;
    else if ([thisLetter isEqualToString:mad:"c"])
    c++;


    }

    But what I want to do is remove the if tree... Something like this is what I want:

    NSString *myString = @"abjbcaubuaedhoca"; // some random letters
    NSString *thisLetter;
    int a = 0;
    int b = 0;
    int c = 0;

    for (int i = 0; i < [myString length]; i++) {
    thisLetter = [this_word substringWithRange:NSMakeRange(i, 1)]; // get each letter.

    //something here... considering the contents of "thisLetter" is the name of the integer I want to increment, I was hoping that there would be some way to refer to a variable with a variable, as in "thisLetter" takes the place of the variable name, if that makes sense (i.e if the contents of "thisLetter" were "a" then, thisLetter++ would increment the value of "a" by 1).

    }

    If this can't be done, then I would also like to know if there is anything faster than an if tree... I was told the switch() can't be used on NSStrings.
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    Messages:
    14,249
    #2
    Look for the header files to find a function that will return a UTF8 string. That will be an array of characters, delimited by a 0 at the end. And then it's just plain C code.

    Note that we are talking about Unicode, so the assumption that getting one element from a string is a "character" is generally flawed except for the simplest cases. There is about a million different characters that could be inside the string.
     
  3. macrumors 6502

    Joined:
    Apr 24, 2008
    Messages:
    471
    #3
    This sounds a bit like a homework assignment, so you'll probably not get a full working answer.

    I can offer you a hint though. You can keep a "histogram" of all the letters you are interested in, set the values to zero, then iterate over your string and ++ the value of the histogram bin corresponding to the particular letter.

    Good luck!
     
  4. macrumors P6

    DoFoT9

    Joined:
    Jun 11, 2007
    Messages:
    17,445
    Location:
    Singapore
    #4
    started studying C++ this semester at uni.. i can read it and follow most of it but a few things are like WOW...

    goodluck with it haha!
     
  5. macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Messages:
    4,151
    Location:
    Sarcasmville.
    #5
    I started studying C++ to semester too! Would be enjoying it if my assignment would compile :D
     
  6. macrumors P6

    DoFoT9

    Joined:
    Jun 11, 2007
    Messages:
    17,445
    Location:
    Singapore
    #6
    oh isnt that just the worst!!!

    anybody got any good C++ programs that work on macs?? they only give us crappy PC ones :|

    we get our assignment 2morrow boo!! goodluck with it!
     
  7. macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Messages:
    4,151
    Location:
    Sarcasmville.
    #7
    [hikack] (last one i promise :D)

    I just use textmate (rather expensive, got mine from machiest bundle), but I think textwrangler will do the job for you, or you could always sink to eclipse? If you're just programming command line type programs, just open up a good text editor like above, and have a terminal window sitting nearby to compile.
     
  8. macrumors P6

    DoFoT9

    Joined:
    Jun 11, 2007
    Messages:
    17,445
    Location:
    Singapore
    #8
    (last one from me too)

    hhmm yea i currently use Smultron for it, didnt think of using the command line.. i might have to. tried installing QT Creator, but couldnt get the libraries working! ahwell. have fun!
     
  9. macrumors regular

    Joined:
    Apr 8, 2009
    Messages:
    115
    #9
    Have a look at NSCountedSet, I think it pretty much does what you want. The only problem I can see is that it will keep a track of all the letter in the string, not just the one you're interested in:

    Code:
    	NSCountedSet *newSet = [NSCountedSet set];
    	
    	for (int i = 0; i < [myString length]; i++) {
    		thisLetter = [myString substringWithRange:NSMakeRange(i, 1)]; // gets each letter.
    		
    		[newSet addObject:thisLetter];
    	}
    	
    	int countOfA = [newSet countForObject:@"a"]; // The number of times a occurs
    
     
  10. macrumors newbie

    Joined:
    Feb 28, 2009
    Messages:
    25
    #10
    That's great! Thank you!
     

Share This Page