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

gonche1124

macrumors newbie
Original poster
Aug 6, 2009
27
0
Hello,

I'm kind of new to cocoa and programming. I'm trying to do build an arrary with the alphabet starting from a letter the user chooses, anyone knows how to do this??

Thanks in advanced
 
Code:
int main(int argc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSString *fullAlphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	NSRange positionOfC = [fullAlphabet rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"C"]];
	NSString *alphabetFromC = [fullAlphabet substringFromIndex:positionOfC.location];
	NSLog(@"The result is: %@",alphabetFromC);
	[pool drain];
	return 0;
}

The @"C" would be replaced with a string containing the entered character. There's other tomfoolery you could do with math on characters (ASCII code business.... pos = myChar - 'A', etc.), but this should work, and it's only a few lines

-Lee

Edit: You asked for the characters in an array. Not sure why, an NSString seems better, but you could toss this before the [pool drain]:
Code:
	unichar *alphaList = (unichar *)malloc([alphabetFromC length]*sizeof(unichar));
	[alphabetFromC getCharacters:alphaList];
	int x;
	for(x = 0;x < [alphabetFromC length];x++) {
	  NSLog(@"Position %d is %C",x,alphaList[x]);
	}
	free((void *)alphaList);
An NSArray can't hold unichars... so a C-style array it is.
 
thanks

Code:
int main(int argc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSString *fullAlphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	NSRange positionOfC = [fullAlphabet rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"C"]];
	NSString *alphabetFromC = [fullAlphabet substringFromIndex:positionOfC.location];
	NSLog(@"The result is: %@",alphabetFromC);
	[pool drain];
	return 0;
}

The @"C" would be replaced with a string containing the entered character. There's other tomfoolery you could do with math on characters (ASCII code business.... pos = myChar - 'A', etc.), but this should work, and it's only a few lines

-Lee

Edit: You asked for the characters in an array. Not sure why, an NSString seems better, but you could toss this before the [pool drain]:
Code:
	unichar *alphaList = (unichar *)malloc([alphabetFromC length]*sizeof(unichar));
	[alphabetFromC getCharacters:alphaList];
	int x;
	for(x = 0;x < [alphabetFromC length];x++) {
	  NSLog(@"Position %d is %C",x,alphaList[x]);
	}
	free((void *)alphaList);
An NSArray can't hold unichars... so a C-style array it is.


Tanks for the code, but when I try implmementing I get an error for the NSRange, it says : "incompatible types in initialization"....is NSRange a ocoa class? or is it Java, can u help me with it? thanks in advanced!
 
It's hard to say what might cause your error. Did you use this code verbatim? If not, please post your code so we can check it out. If you did use this exact code, I'd be surprised as it compiled and ran without issue for me.

Did you use an NSRange * instead of just NSRange? NSRange is a C structure defined in foundation, not a class.

-Lee
 
It's hard to say what might cause your error. Did you use this code verbatim? If not, please post your code so we can check it out. If you did use this exact code, I'd be surprised as it compiled and ran without issue for me.

Did you use an NSRange * instead of just NSRange? NSRange is a C structure defined in foundation, not a class.

-Lee

Thanks for the help.....I've managed to get the code working....I splited into several methods.....but in most of them I get the same warning: "....makes integer from pointer without a cast" and I don't know how to correct this.

Here is an example of the code:

-(NSUInteger *) obtenerPosicionDeLetra: (NSString *)letra
{
NSRange positionOfC = [fullAlphabet rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:letra]];
NSUInteger * numero = positionOfC.location;

return numero;
}

here "letra" is a letter I pass to find in which position it is in the full alphabet...
 
NSUInteger is not a class, it is typedef'd to an unsigned integer of the appropriate width for your platform. If you want to return a pointer to one of these, that's fine, but it makes more sense in this case to just return an NSUInteger, not an NSUInteger *. Also, NSRange's location element is an NSUInteger, not an NSUInteger * (and even if it were a pointer, it probably wouldn't be safe to just assign this pointer). NSRange is described on this page:
http://developer.apple.com/document...Foundation_DataTypes/Reference/reference.html

So you might try:
Code:
-(NSUInteger) obtenerPosicionDeLetra: (NSString *)letra
{
  NSRange positionOfC = [fullAlphabet rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:letra]];
  return positionOfC.location;
}

Also, when posting code use [CODE][/CODE] tags.

-Lee
 
NSUInteger is not a class, it is typedef'd to an unsigned integer of the appropriate width for your platform. If you want to return a pointer to one of these, that's fine, but it makes more sense in this case to just return an NSUInteger, not an NSUInteger *. Also, NSRange's location element is an NSUInteger, not an NSUInteger * (and even if it were a pointer, it probably wouldn't be safe to just assign this pointer). NSRange is described on this page:
http://developer.apple.com/document...Foundation_DataTypes/Reference/reference.html

So you might try:
Code:
-(NSUInteger) obtenerPosicionDeLetra: (NSString *)letra
{
  NSRange positionOfC = [fullAlphabet rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:letra]];
  return positionOfC.location;
}

Also, when posting code use [CODE][/CODE] tags.

-Lee

Thanks for the help....I've managed to clear some of the warnings and errors, but when I run it says it loading program into debugger and then frezzes!!!!! Do you know how to implement a method that triggers whenthe user chooses a different radio button?.....here is the code I'm working on:

Code:
#import "CodigoHitler.h"
#import "AppController.h"


@implementation CodigoHitler

-(id) init
{
	
	// inicializa el abcdario para utilizar
	[super init];
	fullAlphabet = @"abcdefghijklmnopqrstuvwxyz";
	alphabetCorrido =[[NSMutableArray alloc] init];
	
	return self;
}



// devuelve la posicion de una letra dentro de una palabra. 0 es la primera posicion
-(NSUInteger *) obtenerPosicionDeLetra: (NSString *)letra
{
	NSRange positionOfC = [fullAlphabet rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:letra]];
	return positionOfC.location;
}
	
//Crea un string con el abecedario corrido dependiendo de la letra de entrada
-(NSString *) implementarListasHitler:(NSUInteger)indice
{
	NSString *semiAlfabetoUno = [fullAlphabet substringFromIndex:indice];
	//NSLog(semiAlfabetoUno);
	NSString *semiAlfabetoDos =[fullAlphabet substringToIndex:indice];
	//NSLog(semiAlfabetoDos);
	NSString *alfabetoCorrido = [semiAlfabetoUno stringByAppendingString:semiAlfabetoDos];
	NSLog(alfabetoCorrido);
	
	return alfabetoCorrido;
	
}
	
//devuelve la longitud de una palabra o frase de entrada
-(NSUInteger *) longitudHitler: (NSString *)palabra
{
	return [palabra length];
}

//crea un arreglo con los codigos en abcdario corrido
-(void) arregloConCodigoHitler:(NSString *)palabraCodificadora
{	
	int i;
	for(i=0; i<[self longitudHitler:palabraCodificadora]; i++)
	{
		NSString * xx =[palabraCodificadora characterAtIndex:i];
		NSUInteger  *yy=[self obtenerPosicionDeLetra:xx];
		NSString * elemento = [self implementarListasHitler:yy];
		[alphabetCorrido addObject:elemento];
		NSLog(@"%d", elemento);
		
	}	
}
@end

Thanks for all the help Lee

-Andres
 
In my last post I suggested just dealing with NSUInteger values, not NSUInteger *. So...
Code:
#import "CodigoHitler.h"
#import "AppController.h"


@implementation CodigoHitler

-(id) init
{
	
	// inicializa el abcdario para utilizar
	[super init];
	fullAlphabet = @"abcdefghijklmnopqrstuvwxyz";
	alphabetCorrido =[[NSMutableArray alloc] init];	
	return self;
}



// devuelve la posicion de una letra dentro de una palabra. 0 es la primera posicion
-(NSUInteger) obtenerPosicionDeLetra: (unichar)letra
{
	NSString *letraString = [NSString stringWithFormat:@"%C",letra];
	NSRange positionOfC = [fullAlphabet rangeOfString:letraString];
	return positionOfC.location;
}
	
//Crea un string con el abecedario corrido dependiendo de la letra de entrada
-(NSString *) implementarListasHitler:(NSUInteger)indice
{
	NSString *semiAlfabetoUno = [fullAlphabet substringFromIndex:indice];
	//NSLog(semiAlfabetoUno);
	NSString *semiAlfabetoDos =[fullAlphabet substringToIndex:indice];
	//NSLog(semiAlfabetoDos);
	NSString *alfabetoCorrido = [semiAlfabetoUno stringByAppendingString:semiAlfabetoDos];
	NSLog(alfabetoCorrido);
	
	return alfabetoCorrido;
	
}
	
//devuelve la longitud de una palabra o frase de entrada
-(NSUInteger) longitudHitler: (NSString *)palabra
{
	return [palabra length];
}

//crea un arreglo con los codigos en abcdario corrido
-(void) arregloConCodigoHitler:(NSString *)palabraCodificadora
{	
	int i;
	for(i=0; i<[self longitudHitler:palabraCodificadora]; i++)
	{
		unichar xx =[palabraCodificadora characterAtIndex:i];
		NSUInteger yy=[self obtenerPosicionDeLetra:xx];
		NSString *elemento = [self implementarListasHitler:yy];
		[alphabetCorrido addObject:elemento];
		NSLog(@"%@", elemento);		
	}	
}
@end

Made some arguments agree, etc. At a few points an NSUInteger was going to be dereferenced as a pointer, a unichar was going to be dereferenced, etc. Not sure how to run this and test, but give this a try.

-Lee
 
In my last post I suggested just dealing with NSUInteger values, not NSUInteger *. So...
Code:
#import "CodigoHitler.h"
#import "AppController.h"


@implementation CodigoHitler

-(id) init
{
	
	// inicializa el abcdario para utilizar
	[super init];
	fullAlphabet = @"abcdefghijklmnopqrstuvwxyz";
	alphabetCorrido =[[NSMutableArray alloc] init];	
	return self;
}



// devuelve la posicion de una letra dentro de una palabra. 0 es la primera posicion
-(NSUInteger) obtenerPosicionDeLetra: (unichar)letra
{
	NSString *letraString = [NSString stringWithFormat:@"%C",letra];
	NSRange positionOfC = [fullAlphabet rangeOfString:letraString];
	return positionOfC.location;
}
	
//Crea un string con el abecedario corrido dependiendo de la letra de entrada
-(NSString *) implementarListasHitler:(NSUInteger)indice
{
	NSString *semiAlfabetoUno = [fullAlphabet substringFromIndex:indice];
	//NSLog(semiAlfabetoUno);
	NSString *semiAlfabetoDos =[fullAlphabet substringToIndex:indice];
	//NSLog(semiAlfabetoDos);
	NSString *alfabetoCorrido = [semiAlfabetoUno stringByAppendingString:semiAlfabetoDos];
	NSLog(alfabetoCorrido);
	
	return alfabetoCorrido;
	
}
	
//devuelve la longitud de una palabra o frase de entrada
-(NSUInteger) longitudHitler: (NSString *)palabra
{
	return [palabra length];
}

//crea un arreglo con los codigos en abcdario corrido
-(void) arregloConCodigoHitler:(NSString *)palabraCodificadora
{	
	int i;
	for(i=0; i<[self longitudHitler:palabraCodificadora]; i++)
	{
		unichar xx =[palabraCodificadora characterAtIndex:i];
		NSUInteger yy=[self obtenerPosicionDeLetra:xx];
		NSString *elemento = [self implementarListasHitler:yy];
		[alphabetCorrido addObject:elemento];
		NSLog(@"%@", elemento);		
	}	
}
@end

Made some arguments agree, etc. At a few points an NSUInteger was going to be dereferenced as a pointer, a unichar was going to be dereferenced, etc. Not sure how to run this and test, but give this a try.

-Lee

Thanks Lee, I will give a try when I get a break from the University and tell you about it.

-Andres
 
Hello,

I'm kind of new to cocoa and programming. I'm trying to do build an arrary with the alphabet starting from a letter the user chooses, anyone knows how to do this??

Thanks in advanced

Do you need the array, or just a list of characters in order? Maybe you could just store the ascii number when your array has to start and use char?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.