Thread: Ceasar Shift program

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    Ceasar Shift program

    Hey there guys,

    I am currently in the process of writing a ceasar shift program for UNI and would really appreciate some advice. I have pretty much finished the program but there are 2 elements that are stumping me at the moment.
    Basically the help I need is on the function below:

    Code:
    void decryptEncryptLine(int shift) {
      char input;
      char output;
      const char SENT = '\n';
      
      printf("Please enter characters to be encrypted/decrypted\n\n");
      scanf("%c", &input);
                
      while (input != SENT){ // while input is a character
    
              input = (toupper(input)); // convert lowercase to uppercase
              output = input + (shift); // apply ceasar shift
              
              
              if (output > 90) { // encode wrap around
                         output -= 26;
                         }
              
              else if (output < 65) { // decode wrap around
                      output += 26;
                      }
                      
              
              printf("%c",output);
              scanf("%c", &input);
             
              }
              
    }
    the above function takes one input character at a time and applies the ceasar shift then once a "\n" is detected the loop terminates then prints out the shifted characters. The problem I am having is that when it encounters a space, the program shifts the space and I dont want it to. Instead of a space I get the shifted ASCII character. Im not really sure how to go about enabling the user to enter a space and then just print the space with no shift. The other problem that i am having is that I need to use the isalpha() function so that the function will only accept alpha characters. I am also having trouble implementing this. I have tried various ways to incorporate these elements but the out put is never what I want it to be. so far the output looks like this:

    i like to eat pizza
    N?QNPJ?YT?JFY?UNEEF

    Which is correct for the shift but if i try to include these 2 elements the output changes and I end up with everything being displayed up to the first space, then nothing after that. So basically this is as good as I can get it at this stage.

    Any help/advice would be hugely appreciated.

    Thanks Guys

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    100
    Quote Originally Posted by trevordunstan View Post
    Hey there guys,

    I am currently in the process of writing a ceasar shift program for UNI and would really appreciate some advice. I have pretty much finished the program but there are 2 elements that are stumping me at the moment.
    Basically the help I need is on the function below:

    Code:
    void decryptEncryptLine(int shift) {
      char input;
      char output;
      const char SENT = '\n';
      
      printf("Please enter characters to be encrypted/decrypted\n\n");
      scanf("%c", &input);
                
      while (input != SENT){ // while input is a character
    
              input = (toupper(input)); // convert lowercase to uppercase
              output = input + (shift); // apply ceasar shift
              
              
              if (output > 90) { // encode wrap around
                         output -= 26;
                         }
              
              else if (output < 65) { // decode wrap around
                      output += 26;
                      }
                      
              
              printf("%c",output);
              scanf("%c", &input);
             
              }
              
    }
    the above function takes one input character at a time and applies the ceasar shift then once a "\n" is detected the loop terminates then prints out the shifted characters. The problem I am having is that when it encounters a space, the program shifts the space and I dont want it to. Instead of a space I get the shifted ASCII character. Im not really sure how to go about enabling the user to enter a space and then just print the space with no shift. The other problem that i am having is that I need to use the isalpha() function so that the function will only accept alpha characters. I am also having trouble implementing this. I have tried various ways to incorporate these elements but the out put is never what I want it to be. so far the output looks like this:

    i like to eat pizza
    N?QNPJ?YT?JFY?UNEEF

    Which is correct for the shift but if i try to include these 2 elements the output changes and I end up with everything being displayed up to the first space, then nothing after that. So basically this is as good as I can get it at this stage.

    Any help/advice would be hugely appreciated.

    Thanks Guys
    you don't want ' ' to be shifted, isn't it?
    Then why don't you add just a

    Code:
    if (input!=' ') {do everything you want, shift etc}
    else{ printf("%c",input);}
    you could try to add also &&isalpha(input) into the if..

    what do you think?

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    I have tried putting in if (input != ' ') { but whenever I do It does not seem to produce the output that I desire, it will only accept the input up to to the first space then and thats it so i am left with the following:

    input: hi my name is trev
    output: hi

    or it goes into a continuous loop or it produces output completely different then what it should be. So im pretty stuck, but i know it it something simple that I am not seeing so i will keep on chipping away at it. If i figure it out ill post it

    cheers guys

  4. #4
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Don't use scanf, use fgets.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Yeh I have heard that scanf() can be a bit of a dog. We have not been introduced to fgets yet but i will look into it.

    cheers

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    You should use "islower()". Remember, these charactor type functions return NON-ZERO for true. So you could do:

    if(islower(c) != 0)
    x = toupper(c);

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rmetcalf View Post
    You should use "islower()". Remember, these charactor type functions return NON-ZERO for true. So you could do:

    if(islower(c) != 0)
    x = toupper(c);
    How can that help? If islower(c) is false, toupper(c) == c by definition.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    It only converts it if it is NOT equal to zero

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rmetcalf View Post
    It only converts it if it is NOT equal to zero
    I didn't make myself clear. This:
    Code:
    x = toupper(c);
    and this:
    Code:
    if (islower(c) != 0)
        x = toupper(c);
    are completely and totally exactly the same, except that in the first case x will be defined no matter what, and in the second x will be random garbage if c is not a lower-case letter. If you want ' ' to stay ' ', you'll probably want to do the first, since x will be ' ' afterwards, and not the second, where x will be the same letter as before the space (and hence it will appear twice in the output).

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Let's start from the beginning. The issue is that when presented with a string we want to convert lower case letters to uppercase letters without effecting any other charactor types.

    This means:

    islower('c') returns non-zero - convert it to upper case.
    islower('F') returns zero - leave it alone.
    islower('#') returns zero - leave it alone.

    So, since the function islower() returns non-zero when the argument is a lower case letter the following loop will correctly process the string:

    for(ct = 0; ct < strlen(str); ++ ct)
    if(islower(str[ct]) != 0)
    str[ct] = toupper(str[ct]);

    The result will not contain any lower case letters yet any other type of character will remain as it was.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rmetcalf View Post
    Let's start from the beginning. The issue is that when presented with a string we want to convert lower case letters to uppercase letters without effecting any other charactor types.

    This means:

    islower('c') returns non-zero - convert it to upper case.
    islower('F') returns zero - leave it alone.
    islower('#') returns zero - leave it alone.

    So, since the function islower() returns non-zero when the argument is a lower case letter the following loop will correctly process the string:

    for(ct = 0; ct < strlen(str); ++ ct)
    if(islower(str[ct]) != 0)
    str[ct] = toupper(str[ct]);

    The result will not contain any lower case letters yet any other type of character will remain as it was.
    Just for fun, run the loop
    Code:
    for(ct = 0; ct < strlen(str); ++ct)
        str[ct] = toupper(str[ct]);
    and compare with your version. One of us will be surprised.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Your loop will work as well, and actually is more elegant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM