Author Topic: CaesersCypher  (Read 495 times)

0 Members and 1 Guest are viewing this topic.

Offline jitterbud

  • /dev/null
  • *
  • Posts: 8
  • Cookies: 0
    • View Profile
CaesersCypher
« 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;

}

« Last Edit: May 15, 2015, 06:34:14 pm by jitterbud »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: CaesersCypher
« Reply #1 on: May 15, 2015, 06:39:37 pm »
so... do you need help?

Offline jitterbud

  • /dev/null
  • *
  • Posts: 8
  • Cookies: 0
    • View Profile
Re: CaesersCypher
« Reply #2 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...