Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 8, 2009, 01:47 AM   #1
ausername
macrumors newbie
 
Join Date: Feb 2009
Um, I have a very strange question...

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.
ausername is offline   0 Reply With Quote
Old Apr 8, 2009, 04:02 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
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.
gnasher729 is offline   0 Reply With Quote
Old Apr 8, 2009, 04:04 AM   #3
Sander
macrumors 6502
 
Join Date: Apr 2008
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!
Sander is offline   0 Reply With Quote
Old Apr 8, 2009, 04:30 AM   #4
DoFoT9
macrumors P6
 
DoFoT9's Avatar
 
Join Date: Jun 2007
Location: Singapore
Send a message via AIM to DoFoT9 Send a message via MSN to DoFoT9 Send a message via Skype™ to DoFoT9
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!
__________________
Official MacRumors IRC @ irc.krono.net #macrumors (Or http://kewlirc.net:9090/)
2012 2.5GHz Mac Mini Dual - 16GB RAM
Win8 PC - i5-3570k - 16GB RAM - SSD
DoFoT9 is offline   0 Reply With Quote
Old Apr 8, 2009, 04:33 AM   #5
sammich
macrumors 601
 
sammich's Avatar
 
Join Date: Sep 2006
Location: Sarcasmville.
Quote:
Originally Posted by DoFoT9 View Post
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
__________________
Official MR IRC
Look at me, I'm own a bunch of things and everyone must know about every single one.
sammich is offline   0 Reply With Quote
Old Apr 8, 2009, 04:34 AM   #6
DoFoT9
macrumors P6
 
DoFoT9's Avatar
 
Join Date: Jun 2007
Location: Singapore
Send a message via AIM to DoFoT9 Send a message via MSN to DoFoT9 Send a message via Skype™ to DoFoT9
Quote:
Originally Posted by sammich View Post
I started studying C++ to semester too! Would be enjoying it if my assignment would compile
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!
__________________
Official MacRumors IRC @ irc.krono.net #macrumors (Or http://kewlirc.net:9090/)
2012 2.5GHz Mac Mini Dual - 16GB RAM
Win8 PC - i5-3570k - 16GB RAM - SSD
DoFoT9 is offline   0 Reply With Quote
Old Apr 8, 2009, 04:40 AM   #7
sammich
macrumors 601
 
sammich's Avatar
 
Join Date: Sep 2006
Location: Sarcasmville.
Quote:
Originally Posted by DoFoT9 View Post
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 )

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.
__________________
Official MR IRC
Look at me, I'm own a bunch of things and everyone must know about every single one.
sammich is offline   0 Reply With Quote
Old Apr 8, 2009, 04:57 AM   #8
DoFoT9
macrumors P6
 
DoFoT9's Avatar
 
Join Date: Jun 2007
Location: Singapore
Send a message via AIM to DoFoT9 Send a message via MSN to DoFoT9 Send a message via Skype™ to DoFoT9
Quote:
Originally Posted by sammich View Post
[hikack] (last one i promise )

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!
__________________
Official MacRumors IRC @ irc.krono.net #macrumors (Or http://kewlirc.net:9090/)
2012 2.5GHz Mac Mini Dual - 16GB RAM
Win8 PC - i5-3570k - 16GB RAM - SSD
DoFoT9 is offline   0 Reply With Quote
Old Apr 8, 2009, 05:19 AM   #9
JoshDC
macrumors regular
 
Join Date: Apr 2009
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
JoshDC is offline   0 Reply With Quote
Old Apr 8, 2009, 10:24 AM   #10
ausername
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
That's great! Thank you!
ausername is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
strange hard drive question rdsii64 Mac Pro 3 Oct 8, 2013 01:47 PM
strange RAID question rdsii64 MacBook Pro 10 Aug 23, 2013 05:05 AM
Strange Question - Adding a Strap? iRabbit iPhone Accessories 2 Sep 22, 2012 01:12 AM
Strange bit rate question. rdsii64 Digital Video 1 Sep 5, 2012 07:10 PM
iPhoto question, very strange Please help, thanks so much saltwaterdream Mac Applications and Mac App Store 0 May 31, 2012 10:51 PM

Forum Jump

All times are GMT -5. The time now is 06:38 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC