caesar cypher

This is a discussion on caesar cypher within the C Programming forums, part of the General Programming Boards category; I'm trying to create a program that first takes in a number that determines the amount of letters the second ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    38

    caesar cypher

    I'm trying to create a program that first takes in a number that determines the amount of letters the second input (sentence) will go up. This is what I have so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void){
    
        int  i=0,a=1;
        char sent[100];
        while(true){
                scanf("%d",&a);
                if(a == 0){
                     break;
                     }
                     
                gets(sent);
                
                while(sent[i]){
                if(sent[i] != " "){
                      sent[i] += a;
                        }
                      }
                printf("%s",sent);
                           
                }
    
        getchar();
        return 0;
        }
    I get an error at the line: if(sent[i] != " "){

    How come?

    Also, even with that working, how come this program doesn't end up working ?

    Thank you .

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sent[i] is a char, not a string. You're trying to match a char with a string, and that won't work.

    Perhaps you meant to use ' ', with only single quotation marks?

    You need to declare true, and hopefully initialize it, also. When your shift of letters, takes one letter past Z (or z), you have no logic to handle bringing it around to A or a.

    You'll need that.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    better change your loop to be

    Code:
    while(scanf("%d",&a) == 1)
    {
    instead of
    Code:
    while(true){
                scanf("%d",&a);
    2. Do not use gets - read FAQ
    3. you should initialize i and increment it for each string - so your i loop better to write as
    Code:
    for(i=0;s[1];i++)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    Thanks for your replys. I've updated the program and now have partial success.

    Code:
    int main(void){
    
        int  i=0,a;
        char sent[100];
        while(scanf("%d",&a) != '0'){
                    
                fgets(sent,sizeof(sent),stdin);
                
                for(i=0;sent[i];i++){
                    if(sent[i] == ' ')
                      sent[i] = ' ';
                    else if((sent[i] + a) > 'Z')
                      sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
                    else
                      sent[i] += a;
                      }
                printf("%s\n",sent);
                           
                }
        
        return 0;
        }
    I have a few problems.

    When I put in a 0, the program should quit the loop and end, but in the output it just seems to skip a line and continue working.

    When I put in a number, it automatically interprets it and then waits for the next input (which is the only thing that should be interpreted..

    The encoding part works though. The program assumes only capital letters will be used.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf doesn't return the character scanned. It returns the number of successfully scanned codes. In your case, you want it to return 1, since you're scanning in one thing, anything else would mean that something went wrong.

    You should instead be checking to see if a is zero.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    Ok. So I added this:

    Code:
        while(scanf("%d",&a) && a != 0){
    Like always, It still encodes the number itself which I don't get how to stop because it should ask for the next input.

    For Example.. I put in 5 and hit enter and I get * on the next line.
    Now this is where I usually put in the actual sentence to encode which it normally did, but now with the new condition added to the while() loop, when I put in the new sentence and hit enter the program exits.

    But it actually also exits when it finds a 0, but doesn't work now.

    Any idea?

    Here's the full updated code:
    Code:
    int main(void){
    
        int  i,a;
        char sent[100];
        while(scanf("%d",&a) && a != 0){
                    
                fgets(sent,sizeof(sent),stdin);
                
                for(i=0;sent[i];i++){
                    if(sent[i] == ' ')
                      sent[i] = ' ';
                    else if((sent[i] + a) > 'Z')
                      sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
                    else
                      sent[i] += a;
                      }
                printf("%s\n",sent);
                           
                }
        
        return 0;
        }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use fgets to control your input, and look at the first character in the buffer to see if it's a zero?
    Code:
    while( fgets( sent, sizeof sent, stdin ) )
    {
        if ( sent[ 0 ] == '0' )
            break;
        ...otherwise do stuff...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    But for the problem the input must be: first line- the number to increment and the second line the sentence. So I don't see how the number could also be in the sent array.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have to send it in the array. You could, but you don't have to. Just pull it out, then overwrite the array with the line.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    I don't know if this is what you meant, but now the 0 terminates the program and the encoding the number has been fixed, but the actual interpreting of the sentence has totally screwed up. I'm assuming it has something to do with a being a character instead of an integer like it was before.. or something like that.. any idea? lol

    Code:
    int main(void){
    
        int  i,a;
        char sent[100];
        while(fgets(sent,sizeof(sent),stdin)){
          a = sent[0];
          if(a == '0'){
               break;
               }
          else{
               fgets(sent,sizeof(sent),stdin);
                
                for(i=0;sent[i];i++){
                    if(sent[i] == ' ')
                      sent[i] = ' ';
                    else if((sent[i] + a) > 'Z')
                      sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
                    else
                      sent[i] += a;
                      }
                printf("%s\n",sent);
                           
                }
        }
        return 0;
        }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A few idea's for your program.

    Code:
    /* A simple Caesar shift */
    
    #include <stdio.h>
    
    int main(void){
       char again, gar;
       int  i=0, a;
       char sent[100];
        
       do {
          
          printf("\n What shall our offset be for the shift? Enter an integer: [1-25]");
          scanf("%d",&a);
          gar = getchar();
                    
          fgets(sent,sizeof(sent) - 2,stdin);
          
          for(i=0;sent[i];i++) {
             if(sent[i] == ' ')
                continue;
             else if((sent[i] + a) > 'Z')
                sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
             else
                sent[i] += a;
          }   
          printf("%s\n",sent);
          printf("\n Encode again [y/n]? ");
          scanf(" %c", &again);  
          gar = getchar();
       }while(again == 'y' || again == 'Y');
    
       printf("\n\n\t\t\t      press enter when ready \n");
    
       gar = getchar(); gar++;
       return 0;
    }

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there any particular reason you've got a - 2 on your fgets line? It will always make sure there's room for a null character.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    Aha, it converts 'a' into its ascii format. Lets see if I can fix this..

    BTW, I don't understand what the gar = getchar() is for
    Last edited by me77; 04-02-2009 at 08:33 PM.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    Problem solved. I converted the character a to int and now it works great....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
    
        int  i,a;
        char sent[100];
        
        while(fgets(sent,sizeof(sent),stdin)){
          a = atoi(sent);
          if(a == 0){
               break;
               }
          else{
    
               fgets(sent,sizeof(sent),stdin);
                
                for(i=0;sent[i];i++){
                    if(sent[i] == ' ')
                      sent[i] = ' ';
                    else if((sent[i] + a) > 'Z')
                      sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
                    else
                      sent[i] += a;
                      }
                printf("%s\n",sent); 
                           
                }
        }
        return 0;
        }
    well except for one thing....

    at the end of the encoded sentence it ads on the encoded number... for example when i put in

    5
    THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

    I get

    YMJ VZNHP GWTBS KTC OZRUX TAJW YMJ QFED ITL*

    the * is extra.. it's encoding the number... or maybe the enter... or maybe the null character? I'm not sure.

    Other then that.. it works now.

    BTW.. adak has the same problem in his version.
    Last edited by me77; 04-02-2009 at 08:47 PM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's the newline character probably. Try something like isalpha, which is in ctype.h, so that you don't mung up punctuation.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Caesar Cipher
    By dldsob in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2009, 07:06 PM
  2. Another Caesar Cipher
    By Swordsman in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 09:56 AM
  3. Caesar Cypher
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-29-2007, 01:21 PM
  4. Help with Caesar cipher
    By jcmichman in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2005, 11:50 AM
  5. My little Caesar cipher emulator
    By dead_cell in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 01:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21