View Single Post
  #1 (permalink)  
Old 09-16-2008, 05:14 AM
trevordunstan trevordunstan is offline
Newbie
 
Join Date: May 2008
Posts: 3
Credits: 0
Rep Power: 0
trevordunstan is on a distinguished road
Default Ceasar shift program troubles

Hey there guys,

I am writing a ceasar shift program for uni and I am having a little trouble. I have essentially written the program and it works except for a couple of small details which I am really finding hard to get around. Here is the code for the function that accepts the input then applies the ceasar shift



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 not '\n'

         
          if  (isalpha(input))  {         
          
          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;
                             }
          
          }
           
         else if (input == ' ') {
                   printf("%c", input);   
                       }
         
         printf("%c",output);
         scanf("%c", &input);
          
         
         }
         
          
          


}
my problem is that when I enter a space it prints a space followed by the previous shifted character then the correct shifted output. eg

input = I like to eat pizza
output = L LOLNH HWR RHDW WSLCCD

The characters in red are not supposed to be there. The thing is that I understand why it is happening it is because I have 2 printfstatements the problem is that i just dont know the best way to fix it. I have been sitting here for days trying different ways to fix it but i just cant seem to crack it, even though i think it is just something simple that i am over looking. Could someone run the code and suggest a way that I can get the function to accept characters but only apply the ceasar shift to alpha characters but ignore spaces and numbers ie the numbers and spaces should be output but not shifted.

I have only been studying C for a couple of months now so any advice would be hugely appreciated.

thanks

trev
Reply With Quote

Sponsored Links