Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Trying to make a caeser cipher program with a character array

$
0
0
Basically have to enter a message and a shift amount and then the encrypted message will display the new character array. For example if i input: hello...with a shift of two, the new message would be: jfnnq

I'm having trouble in the actual shifting part of it as i though you could just add the int shift to each character in the array, because of the ascii codes. Also i have to make sure capitals stay capitalized, punctuation stays the same, and spaces stay the same.

Here is my code so far:

#include <stdio.h>
#include <stdlib.h>



int main()
{
    int key = 0;
    int count = 1;
    int j = 0;
    char ch;
    char array[] = {0};
    
    printf("Enter message to be encrypted:  ");
    ch = getchar();
    
    for (j = 0; j <= 80; j++)
    {
        if(ch == '\n')
       break;
    
    else
    {
       ch = getchar();
       count++;
    }
      
    }
    
    printf("Enter shift amount (1-25):  ");
    scanf("%d", &key);
    
    printf("Encrypted message:  ");
    
    for(int k = 0; k < j ; k++)
    {
            
        if(array[k] >= 0 && array[k] <= 64)
        {
            printf("%c", array[k]);
        } 
        else if (array[k] == ' ')
        {
            printf(" ");     
        }
        else
        {
            printf("%c", array[k] + key);    
        }
         
      
    }
    printf("\n");
    system("PAUSE");
    return 0;
}




Any suggestions?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>