PDA

View Full Version : Um, I have a very strange question...




ausername
Apr 8, 2009, 01:47 AM
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:@"a"])
a++;
else if ([thisLetter isEqualToString:@"b"])
b++;
else if ([thisLetter isEqualToString:@"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.



gnasher729
Apr 8, 2009, 04:02 AM
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.

Sander
Apr 8, 2009, 04:04 AM
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!

DoFoT9
Apr 8, 2009, 04:30 AM
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!

sammich
Apr 8, 2009, 04:33 AM
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!

I started studying C++ to semester too! Would be enjoying it if my assignment would compile :D

DoFoT9
Apr 8, 2009, 04:34 AM
I started studying C++ to semester too! Would be enjoying it if my assignment would compile :D

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!

sammich
Apr 8, 2009, 04:40 AM
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!

[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.

DoFoT9
Apr 8, 2009, 04:57 AM
[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.

(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!

JoshDC
Apr 8, 2009, 05:19 AM
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:


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

ausername
Apr 8, 2009, 10:24 AM
That's great! Thank you!