Caesar Cipher help!!

This is a discussion on Caesar Cipher help!! within the C Programming forums, part of the General Programming Boards category; Hi i am having problem with strings using for loop. When i enter input abc, strlen shows that string length ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Caesar Cipher help!!

    Hi i am having problem with strings using for loop. When i enter input abc, strlen shows that string length is 3 but when i enter abc d string length still shows 3 and i think loop stop when it sees space.

    here is my program
    Code:
    /* Caesar Cipher 
       Darshan
     */
    
    #include <stdio.h>
    #include <string.h>
    
    int main(){
        
        // Message to be encrypted
        char input[80];
        
        // shift
        int key =0;
    
        int i;
        
        // Enter shift amount
        printf("Enter shift amount (1-25): ");
        scanf("%d", &key);
        
        // enter message
        printf("Enter message to be encrypted: ");
        scanf("%s", &input);
        
    
        for(i=0; input[i] != '\0';i++)
        {
            // check  if letter is captial or lower case
            if (input[i]>='A' && input[i]<='Z')
            {
                input[i] = (input[i] - 'A' + key) % 26 + 'A';
            }
                else 
                    if (input[i]>='a' && input[i]<='z')
                    {
                        input[i] = (input[i] - 'a' + key) % 26 + 'a';
                    } 
    
        }
        
        printf("\nEncrypted message: %s", input);
        printf("\nstrlen(%lu)", strlen(input));
        
        getchar();
        getchar();
        return 0;   
        
    }
    Thanks for your help

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Which is just one of the reasons scanf() is not well suited at all for user input.

    fgets() is preferred, but try this scanf() format out, and see what you think.

    Code:
          scanf("%[^\n]s",input);
          
          printf("%s  %d", input, strlen(input));

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    that didn't work for me but is this right if i do this
    Code:
       if (input[i] == ' ')
            {
                input [i] = ' ';
            }
    inside for loop? b/c its still not working.

    if i let
    Code:
    char input[80] = abc xyz;
    without asking user to enter any input it works fine but it doesn't work when i ask user to enter string.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by darshan10 View Post
    that didn't work for me but is this right if i do this
    Code:
       if (input[i] == ' ')
            {
                input [i] = ' ';
            }
    That doesn't do anything. It sets the current square to the current square's value. So it really doesn't do anything.


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

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by darshan10 View Post
    Hi i am having problem with strings using for loop. When i enter input abc, strlen shows that string length is 3 but when i enter abc d string length still shows 3 and i think loop stop when it sees space.

    here is my program
    Code:
    /* Caesar Cipher 
       Darshan
     */
    
    #include <stdio.h>
    #include <string.h>
    
    int main(){
        
        // Message to be encrypted
        char input[80];
        
        // shift
        int key =0;
    
        int i;
        
        // Enter shift amount
        printf("Enter shift amount (1-25): ");
        scanf("%d", &key);
        
        // enter message
        printf("Enter message to be encrypted: ");
        scanf("%s", &input);
        
    
        for(i=0; input[i] != '\0';i++)
        {
            // check  if letter is captial or lower case
            if (input[i]>='A' && input[i]<='Z')
            {
                input[i] = (input[i] - 'A' + key) % 26 + 'A';
            }
                else 
                    if (input[i]>='a' && input[i]<='z')
                    {
                        input[i] = (input[i] - 'a' + key) % 26 + 'a';
                    } 
    
        }
        
        printf("\nEncrypted message: %s", input);
        printf("\nstrlen(%lu)", strlen(input));
        
        getchar();
        getchar();
        return 0;   
        
    }
    Thanks for your help
    What happens if they type a number?

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    how to get invalid message when user enter char for shift

    i fixed my string problem using
    Code:
    fgets(message, 80, stdin);
    now i want to know that how can i make it invalid shift when someone enter char instead of number

    Code:
      
    while(key <1 || key > 25)
        {
            printf("Enter shift amount between (1-25): ");
            scanf("%d", &key);
        }
    i have tried using this b/c it don't work

    Code:
      while(key == getchar())
        {
            printf("Enter shift amount between (1-25): ");
            scanf("%d", &key);
        }
    thanks

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    do {
       printf("\nEnter shift amount between (1-25): ");
       scanf("%d", &key);
       getchar();
       if(key < 1 || key > 25) {
          key = 0;
          printf("Invalid input, key now changed to %d\n", key);
       }
    }while(!key);
    Handles letter entry, also

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, 06:06 PM
  2. Caesar Cipher
    By blacknapalm in forum C Programming
    Replies: 8
    Last Post: 11-12-2008, 11:11 PM
  3. Another Caesar Cipher
    By Swordsman in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 08:56 AM
  4. caesar cipher help.
    By stormfront in forum C Programming
    Replies: 36
    Last Post: 11-22-2005, 07:45 PM
  5. Help with Caesar cipher
    By jcmichman in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2005, 10:50 AM

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