EvilZone

Programming and Scripting => Beginner's Corner => Topic started by: jitterbud on May 15, 2015, 06:21:50 PM

Title: CaesersCypher
Post by: jitterbud on May 15, 2015, 06:21:50 PM
I'm doing cs50 @ Edx.org. One of the problem sets was to take a key from the user and use  caeser's algorithm to scramble the user input text. Not all that, but its the start to cryptology which underpin's  cryptography. Its was also a learning curve into how to convert ASCII char into integer and converting back when shifting the letters.
Code: [Select]
#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, string argv[])
{
   int k,Isum;

   if (argc !=2)
           {
                return 1;
           }
   string p = GetString();
   k = atoi(argv[1]);
   k = (k%26);
                  for(int i=0, n=strlen(p);i<n;i++)
                                      {
                                            Isum = (p[i]+k);
                  if(toupper(p[i] && (Isum > 'Z')))
                      {
                                Isum = (Isum - 26);
                       }                                     
                   if(tolower(p[i] && (Isum > 'z'))
                            {
                                        Isum = (Isum -26);
                            }

                  if(isalpha(p[i]))
                          {
                               printf("%c", Isum);
                           }
                           else {
                                           printf("%c", p[i]);
                                     }
                  }
      return 0;

}

Title: Re: CaesersCypher
Post by: Kulverstukas on May 15, 2015, 06:39:37 PM
so... do you need help?
Title: Re: CaesersCypher
Post by: jitterbud on May 16, 2015, 02:35:01 PM
Sure. We all need some sort help to being self aligned any ways. I'm having an issue with the key. Key doesn't loopback from z to a, instead it goes on to the ASCII [,|,^. I don’t think Isum = (Isum -26) is the way to do it...