1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ HELP!!!! How do you wrap an array?

Discussion in 'C, C++, C#' started by Designerdude13, Nov 1, 2012.

  1. Designerdude13

    Designerdude13 Power Member

    Joined:
    Nov 22, 2010
    Messages:
    645
    Likes Received:
    188
    Gender:
    Male
    Ok, So I'm trying to wrap the alphabet so it starts back at A when it's shifted by a value thats goes past the 26 letters. I tried putting the alphabet in array and wrapping, but I'm stuck. Please help!

    Code:
    char shiftChar(char c, int shift) {    
    
    
        if(!isalpha(c)) {
            
            return c;
            
        }
        
        if(isalpha(c)) {
            
            if(islower(c)) {
                
                c = toupper(c);
                
            }
            
                            
         
            
            c = ((c % 26) - 13);
          
            int a[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
            
            shift = (26 % shift);
            int x = 0;
            
            x = c + shift;
            
            c = a[x];
            
            
        
                   
            
            return static_cast<char> (c);
                
            
        }
        
    }
     
  2. SEOWhizz

    SEOWhizz Power Member

    Joined:
    Oct 22, 2011
    Messages:
    606
    Likes Received:
    431
    Location:
    Lat: 38N 43' 11.298" Long: 27W 12' 7.733"
    Code:
    char caesarShift( signed char shift, char c )
      {
      if (isalpha( c ))  // only shift alphabetic characters
        {
        c += shift;
        if (!isalpha( c ))  // The result must be a letter, so wrap around if needed
          c -= 26;          // (for example 'Z'+3 --> 'C'; 'Y'+3 --> 'B'; etc)
        }
      else  // everything else remains unchanged
        return c;
      }
    More info:
    Code:
    [URL]http://www.cplusplus.com/forum/beginner/2040/[/URL]
    [URL]http://www.cplusplus.com/forum/beginner/75615/[/URL]

    Caesar ciphers are common homework questions. :)
     
    • Thanks Thanks x 1
    Last edited: Nov 1, 2012
  3. Designerdude13

    Designerdude13 Power Member

    Joined:
    Nov 22, 2010
    Messages:
    645
    Likes Received:
    188
    Gender:
    Male
    Thanks so much, but I can't get it to work
     
  4. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    Thread moved to the proper place :) Yes, we have a C section! :eek:
     
  5. Designerdude13

    Designerdude13 Power Member

    Joined:
    Nov 22, 2010
    Messages:
    645
    Likes Received:
    188
    Gender:
    Male
    WOah....i didn't even know that lol! Cool. and thanks!
     
  6. Designerdude13

    Designerdude13 Power Member

    Joined:
    Nov 22, 2010
    Messages:
    645
    Likes Received:
    188
    Gender:
    Male
    Okay I'm using a while loop now --- and when the loop gets to Z - it skips a letter :(

    Code:
    char shiftChar(char c, int shift) {    
        int z = c + shift;
        int x = c;
        int i = c;
        
    	while (x < z) {
            
            i--;
            x++;
            
            if(i == 65) {
                
                i = 90;
                
            }
            
        }
        
        return static_cast<char> (i + 1);
    }
     
  7. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    There are better ways to implement a shift than to use an array, but to answer your original question here is a way to do it in C (it should be pretty trivial to convert to C++):


    Code:
    #include <stdio.h>
    
    /*prototypes*/
    char shift( char, short );
    
    int main( int argc, char* argv[] )
    {
    	printf( "%c\n", shift( 'A', 3 ) );
    	printf( "%c\n", shift( 'A', 29 ) );
    	printf( "%c\n", shift( 'A', -26 ) );
    	printf( "%c\n", shift( 'Z', 3 ) );
    	printf( "%c\n", shift( 'Z', 29 ) );
    	printf( "%c\n", shift( 'D', -3 ) );
    
    	return 0;
    }
    
    
    char shift( char arg, short amount )
    {
    	char index[26]	= { 0 };
    	int i		= 0;
    
    	/*populate the array*/
    	for( i = 0; i < 26; i++ )
    		index[i] = (char)( 65 + i );
    	
    	/*convert the character to an index*/
    	i = ( (int)arg - 65 ) + amount;
    
    	/*check bounds*/
    	while( i > 26 )
    		i -= 26;
    
    	while( i < 0 )
    		i += 26;
    
    	return index[i];
    }
    
     
    Last edited: Nov 4, 2012
  8. matessim

    matessim Junior Member

    Joined:
    Nov 22, 2008
    Messages:
    164
    Likes Received:
    72
    Occupation:
    Being funny and kind to puppies
    Location:
    UT 2003
    you should really use the modulus operation instead of those whiles
     
  9. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    You also shouldn't use an array. But I was watching football and doing someone else's homework for them, life goes on.