CS50

Problem set 2

This pset is getting more interesting when we can make a secret ciphered message. Sounds cool? Alrighty, this one has only 3 files: Initials, Caesar, Vigenere. There is one point you should take notice of when doing this set that is the decimal code of alphabetical letters (both uppercase and lowercase) in ASCII table. Again, if you are taking the course and still working on this pset, be aware that copying my code for your submission is considered unreasonable academic dishonesty. Please be reminded of this term at the beginning of every problem set in CS50.

Else, help me point out any problem (even smallest) with my code. I find it quite long and not really smart. Thanks in advance!

Ok, this is initials.c:

#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#include <string.h>

int main(void)
{
   string s = GetString();
   if (s != NULL)
   {
      for (int i = 0, n = strlen(s); i < n; i++) 
      {
         if (i == 0)
         {
            printf ("%c", toupper (s[0]));
         }
         else if (s[i]==' ')
         {
            printf("%c", toupper(s[i+1]));
         }
      }
      printf("\n");
   }
}

This is caesar.c:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <cs50.h>
#include <string.h>

int main(int argc, string argv[])
{
   if (argc != 2)
   {
      printf ("Please add your key after './caesar'\n");
      return 1;
   }
   else
   {
      int k = atoi(argv[1]);
      string s = GetString();

      if (s != NULL)
      {
         for (int i = 0, n = strlen(s); i < n; i++) 
         {
            if (isalpha(s[i]))
            {
               if (isupper(s[i]))
               {
                  int u = ((s[i] - 65 + k)%26) + 65;
                  printf("%c", u);
               }
               if (islower(s[i]))
               {
                  int l = ((s[i] - 97 + k)%26) + 97;
                  printf("%c", l);
               }
            }
            else
            {
               printf("%c", (s[i]));
            }
         }
         printf("\n");
      }
   }
}

And finally, this is vigenere.c:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <cs50.h>
#include <string.h>

int main(int argc, string argv[])
{
   if (argc != 2)
   {
      printf ("Please add your key after './vigenere'\n");
      return 1;
   }
   else
   {
      string key = argv[1];
      int n = strlen(key);
      for (int i = 0; i < n; i ++)
      {
         if (!isalpha(key[i]))
         {
             printf("Please add your alphabetical key after './vigenere'\n");
             return 1;
         }
      }
 
      string text = GetString();
      for (int j = 0, c=0, m = strlen(text); j < m; j++)
      {
         if (isalpha(text[j]))
         {
            if (isupper(text[j]))
            {
               if (isupper(key[c%n]))
               {
                  printf("%c", ((text[j] - 65 + (key[c%n] - 65))%26) + 65);
                  c++;
               }  
               else
               {
                  printf("%c", ((text[j] - 65 + (key[c%n] - 97))%26) + 65);
                  c++;
               }
            }
            else
            {
               if (isupper(key[c%n]))
               {
                  printf("%c", ((text[j] - 97 + (key[c%n] - 65))%26) + 97);
                  c++;
               }
               else
               {
                  printf("%c", ((text[j] - 97 + (key[c%n] - 97))%26) + 97);
                  c++;
               }
            }
         }
         else
         {
            printf("%c", text[j]);
         }
      }
      printf("\n");
   }
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s