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

Designerdude13

Power Member
Joined
Nov 22, 2010
Messages
648
Reaction score
195
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);
            
        
    }
    
}
 
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. :)
 
Last edited:
Thanks so much, but I can't get it to work
 
Thread moved to the proper place :) Yes, we have a C section! :o
 
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);
}
 
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:
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];
}

you should really use the modulus operation instead of those whiles
 
You also shouldn't use an array. But I was watching football and doing someone else's homework for them, life goes on.
 
Back
Top