alphabetic array

Discussion in 'Mac Programming' started by gonche1124, Aug 6, 2009.

  1. gonche1124 macrumors newbie

    Joined:
    Aug 6, 2009
    #1
    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
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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.
     
  3. gonche1124 thread starter macrumors newbie

    Joined:
    Aug 6, 2009
    #3
    thanks


    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!
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. gonche1124 thread starter macrumors newbie

    Joined:
    Aug 6, 2009
    #5
    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...
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. gonche1124 thread starter macrumors newbie

    Joined:
    Aug 6, 2009
    #7
    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
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    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
     
  9. gonche1124 thread starter macrumors newbie

    Joined:
    Aug 6, 2009
    #9
    Thanks Lee, I will give a try when I get a break from the University and tell you about it.

    -Andres
     
  10. gonche1124 thread starter macrumors newbie

    Joined:
    Aug 6, 2009
    #10
    Thanks

    Thanks Lee, I've tried it now and it works great, I will now move on to the next phase!!!!!

    Andres
     
  11. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #11
    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?
     

Share This Page