Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ausername

macrumors newbie
Original poster
Feb 28, 2009
25
0
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.
 
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.
 
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!
 
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.
 
[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!
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.