10 Replies - 436 Views - Last Post: 10 February 2013 - 02:48 PM Rate Topic: -----

#1 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 09:31 AM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to make a caeser cipher program with a character array

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 10:29 AM

You can include <ctype.h> and use isalnum() to test whether a char is a alphanumeric type. Those are the only kind of char you want to "shift". You can also test this yourself, of course.

Quote

Syntax:
int isalnum(int c);

Declared in:
<ctype.h>

Description:
The isalnum function tests for any character for which isalpha or isdigit is true.

Returns:
A nonzero value if the character is alpha-numeric.


if(isalnum(mycharArray[i]))
//go ahead and shift it.


The only tricky part to this is shifting a char that takes it beyond the value of Z or z (depending on it's case). Then you have to use a bit of math to figure out how far it still needs to be shifted. Say you're doing a shift of 10 and the letter is a W - you'll use up 10-3 to take the W to Z, and now you want to shift it to A + 7 (not A + 10).
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 11:02 AM

It would be easier to make two arrays one

array with lowercase letters and one with capitals

two test conditions if the letter is 65-90 its a capital

if its 97-122 its lowercase

the rest remain unchanged...

Why do it this way well if you word or message is Zebra then adding 2 to Z will be

the character '\' you need a condition that says if the letter is over then it resets to the first letter in the array...

something like

 if(array1[k] >= 97 && array1[k] <= 122)
        {
           word = (array1[k]-97) + key;
           if (word >26)
           {word = word-26;}

           printf("%c", array2[word]);
        } 




On a side note your word hello would be 'jgnnq' not as you put as e +2 does not = f.

Its quite an easy program.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#4 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 11:24 AM

View Postsnoopy11, on 09 February 2013 - 11:02 AM, said:

It would be easier to make two arrays one

array with lowercase letters and one with capitals

two test conditions if the letter is 65-90 its a capital

if its 97-122 its lowercase

the rest remain unchanged...

Why do it this way well if you word or message is Zebra then adding 2 to Z will be

the character '\' you need a condition that says if the letter is over then it resets to the first letter in the array...

something like

 if(array1[k] >= 97 && array1[k] <= 122)
        {
           word = (array1[k]-97) + key;
           if (word >26)
           {word = word-26;}

           printf("%c", array2[word]);
        } 




On a side note your word hello would be 'jgnnq' not as you put as e +2 does not = f.

Its quite an easy program.

Snoopy.


i think i'm only supposed to use one array as specified by my instructor.

I tried a different approach to "wrap" around the letters but i'm still kind of confused because i feel like this should work. Here is my code now:

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

int main()
{
    
    int key;
    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();
        }
      
    }
    
    printf("Enter shift amount (1-25):  ");
    scanf("%d", &key);
    
    printf("Encrypted message:  ");
    
    for(int k = 0; k < j ; k++)
    {
            
        if(array[k] >= 65 && array[k] <= 90)
        {
            if(((array[k] - 'A') + key) % 26 > 0)
            {
                array[k] = ((array[k] - 'A') + key) % 26 + 'A';           
            }
            else
            {
                array[k] = (array[k] + key);
            }
        } 
        
        else if (array[k] >= 97 && array[k] <= 122)
        {
            if(((array[k] - 'a') + key) % 26 > 0)
            {
                array[k] = ((array[k] - 'a') + key) % 26 + 'a';              
            }     
            else
            {
                array[k] = (array[k] + key);    
            }
        }
        
        else
        {
              array[k] = array[k]; 
        }
         
    }
    
    for (int h = 0; h < j; h++)
    {
        printf("%c", array[h]);    
    }
    
    printf("\n");
    system("PAUSE");
    return 0;
    
}


Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 11:36 AM

Well,

If you can only use one array, then

you would again have two test conditions one between 97-122

the other between 65 and 90

then you would subtract for instance 97 from the 1 array and add key
if the value is over 26 again reset then add the value to 97.

word = (array1[k]-97) + key;
if (word >26)
{word = word-26;}
printf("%c", word+97);



do the same for uppercase

Best Wishes

Snoopy.

This post has been edited by snoopy11: 09 February 2013 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 11:46 AM

View Postsnoopy11, on 09 February 2013 - 11:36 AM, said:

Well,

If you can only use one array, then

you would again have two test conditions one between 97-122

the other between 65 and 90

then you would subtract for instance 97 from the 1 array and add key
if the value is over 26 again reset then add the value to 97.

word = (array1[k]-97) + key;
if (word >26)
{word = word-26;}
printf("%c", word+97);



do the same for uppercase

Best Wishes

Snoopy.


Ok so i changed my if statements to what you were saying but it still won't print out anything. Sorry if it something obvious but i really can't see what the problem is

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

int main()
{
    
    int key;
    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();
        }
      
    }
    
    printf("Enter shift amount (1-25):  ");
    scanf("%d", &key);
    
    
    
    for(int k = 0; k < j ; k++)
    {
            
        if(array[k] >= 65 && array[k] <= 90)
        {
            if(((array[k] - 65) + key) > 26)
            {
                array[k] = ((array[k] - 65) + key) % 26 + 65;           
            }
            else
            {
                array[k] = (array[k] + key);
            }
        } 
        
        else if (array[k] >= 97 && array[k] <= 122)
        {
            if(((array[k] - 97) + key) > 26)
            {
                array[k] = ((array[k] - 97) + key) % 26 + 97;              
            }     
            else
            {
                array[k] = (array[k] + key);    
            }
        }
        
        else
        {
              array[k] = array[k]; 
        }
         
    }
    
    printf("Encrypted message:  ");
    
    for (int h = 0; h < j; h++)
    {
        printf("%c", array[h]);    
    }
    
    printf("\n");
    system("PAUSE");
    return 0;
    


Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 11:58 AM

Your over confusing the issue its much simpler than you think

You have a loop from 0 to length of sentence k

you have two test conditions one for 97- 122 the other 65-90 else just print it out.

if it is between 97-122 introduce a int variable lets call it word

word = (array1[k]-97) + key;
or
word = (array1[k]-65) + key;

now if word is over 26 then reset it to word=word-26 so you get a wrap around.

you dont need the modulo operator % for this.

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#8 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 08:49 PM

View Postsnoopy11, on 09 February 2013 - 11:58 AM, said:

Your over confusing the issue its much simpler than you think

You have a loop from 0 to length of sentence k

you have two test conditions one for 97- 122 the other 65-90 else just print it out.

if it is between 97-122 introduce a int variable lets call it word

word = (array1[k]-97) + key;
or
word = (array1[k]-65) + key;

now if word is over 26 then reset it to word=word-26 so you get a wrap around.

you dont need the modulo operator % for this.

Snoopy.


changed it but it still isn't printing anything out

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

int main()
{
    
    int key;
    int j = 0;
    int word = 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();
        }
      
    }
    
    printf("Enter shift amount (1-25):  ");
    scanf("%d", &key);
    
    
    printf("Encrypted message:  ");
    for(int k = 0; k < j ; k++)
    {
            
        if(array[k] >= 65 && array[k] <= 90)
        {
            word = (array[k] - 65) + key;
            
            if (word > 26)
               word = word - 26;
               
            printf("%c", word + 65);
        } 
        
        else if (array[k] >= 97 && array[k] <= 122)
        {
            word = (array[k] - 97) + key;
            
            if (word > 26)
               word = word - 26;
               
            printf("%c", word + 97);
        }
        
        else
        {
              word = array[k]; 
              printf("%c", word);
        }
         
    }
    
    
    
    
    
    printf("\n");
    system("PAUSE");
    return 0;
    
}



Was This Post Helpful? 0
  • +
  • -

#9 adgjlsfhk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 08-February 13

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 10:35 PM

I just finished making one for unrelated reasons. I won't give you the code for mine, but it used 1 shift variable, one loop variable, the text was a string, and it used a for loop and if statements to go through the text one at a time. This may help
A-Z=0x40-0x6a
a-z=0x60-0x7a
the subtraction was a good idea, subtract by 0x1a

hope this helps
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Trying to make a caeser cipher program with a character array

Posted 09 February 2013 - 11:35 PM

Hi,

Well dont you think you need to initialize your array properly

char array[100] = {0}; would do it

also this

else
{
ch = getchar();

}

dont you think you should add ch to the array for every ch = getchar();

Right now your array is not initialized and you dont add anything to it.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 10-August 12

Re: Trying to make a caeser cipher program with a character array

Posted 10 February 2013 - 02:48 PM

View Postsnoopy11, on 09 February 2013 - 11:35 PM, said:

Hi,

Well dont you think you need to initialize your array properly

char array[100] = {0}; would do it

also this

else
{
ch = getchar();

}

dont you think you should add ch to the array for every ch = getchar();

Right now your array is not initialized and you dont add anything to it.

Snoopy.


wow that was really dumb of me, thanks so much for the help though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1