Skip to main content
removed solution code
Source Link
curiouskiwi
  • 18.7k
  • 2
  • 18
  • 43
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])

{
    //check if it is valid
    if(argc != 2)
    {
        printf("Error,invalid entry.\n");
        return 1;   
    }
  
    string length = argv[1];
    int klength = strlen(length);
    int shifted[klength];
    int i;
    for(i = 0; i < klength; i++)
    {
        while(!isalpha(argv[1][i]))
        {
            printf("Error,invalid entry.\n");
            return 1;
        }
    //shift ascii to 0-25
           
        if(isupper(argv[1][i]))
        {
            shifted[i] = argv[1][i] - 65;
        }
        else if(islower(argv[1][i]))
        {
            shifted[i] = argv[1][i] - 97;
        }
        else if(!isalpha(argv[1][i]))
        {
            ;
        }    
    }   

    //get plaintext
    string plaintext = GetString();
    int ptlength = strlen(plaintext);
    
    //encryption
    char temp;
    int wrap = 0;
    
    for(i = 0; i < ptlength; i++)
    {
        temp = plaintext[i];
        
        if(isalpha(temp))
        {
            if(isupper(temp))
            {
                temp = temp + shifted[wrap%klength];
                if(temp > 90)
                {
                    temp = (temp % 90) + 64;
                }
                printf("%c",temp);
                wrap++;
            }
            else if(islower(temp))
            {
                temp = temp + shifted[wrap%klength];
                if(temp > 122)
                {
                    temp = (temp % 122) + 96;
                }
                printf("%c", temp);
                wrap++;
            }
            
        }
        else if(temp == ' ')
        {
            printf(" ");
        }
        else if(temp != ' ' && !isalpha(temp))
        {
            printf("%c",temp);
        }   
        else
        {
            printf("%c",temp);
        }
    }
    
    printf("\n");   
    
}

[code snipped per Honor Code. In the future, please describe using pseudocode.]

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

int main(int argc, string argv[])

{
    //check if it is valid
    if(argc != 2)
    {
        printf("Error,invalid entry.\n");
        return 1;   
    }
  
    string length = argv[1];
    int klength = strlen(length);
    int shifted[klength];
    int i;
    for(i = 0; i < klength; i++)
    {
        while(!isalpha(argv[1][i]))
        {
            printf("Error,invalid entry.\n");
            return 1;
        }
    //shift ascii to 0-25
           
        if(isupper(argv[1][i]))
        {
            shifted[i] = argv[1][i] - 65;
        }
        else if(islower(argv[1][i]))
        {
            shifted[i] = argv[1][i] - 97;
        }
        else if(!isalpha(argv[1][i]))
        {
            ;
        }    
    }   

    //get plaintext
    string plaintext = GetString();
    int ptlength = strlen(plaintext);
    
    //encryption
    char temp;
    int wrap = 0;
    
    for(i = 0; i < ptlength; i++)
    {
        temp = plaintext[i];
        
        if(isalpha(temp))
        {
            if(isupper(temp))
            {
                temp = temp + shifted[wrap%klength];
                if(temp > 90)
                {
                    temp = (temp % 90) + 64;
                }
                printf("%c",temp);
                wrap++;
            }
            else if(islower(temp))
            {
                temp = temp + shifted[wrap%klength];
                if(temp > 122)
                {
                    temp = (temp % 122) + 96;
                }
                printf("%c", temp);
                wrap++;
            }
            
        }
        else if(temp == ' ')
        {
            printf(" ");
        }
        else if(temp != ' ' && !isalpha(temp))
        {
            printf("%c",temp);
        }   
        else
        {
            printf("%c",temp);
        }
    }
    
    printf("\n");   
    
}

[code snipped per Honor Code. In the future, please describe using pseudocode.]

Source Link

pset2 vigenere certain letters return wierd symbols

so my vigenere cipher seems to work for uppercase letters but for some lowercase letters, it returns � for some reason. The code for lowercase and uppercase is structurally similar therefore I don't know why this is happening so I would appreciate if anyone has any input on this.

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

int main(int argc, string argv[])

{
    //check if it is valid
    if(argc != 2)
    {
        printf("Error,invalid entry.\n");
        return 1;   
    }
  
    string length = argv[1];
    int klength = strlen(length);
    int shifted[klength];
    int i;
    for(i = 0; i < klength; i++)
    {
        while(!isalpha(argv[1][i]))
        {
            printf("Error,invalid entry.\n");
            return 1;
        }
    //shift ascii to 0-25
           
        if(isupper(argv[1][i]))
        {
            shifted[i] = argv[1][i] - 65;
        }
        else if(islower(argv[1][i]))
        {
            shifted[i] = argv[1][i] - 97;
        }
        else if(!isalpha(argv[1][i]))
        {
            ;
        }    
    }   

    //get plaintext
    string plaintext = GetString();
    int ptlength = strlen(plaintext);
    
    //encryption
    char temp;
    int wrap = 0;
    
    for(i = 0; i < ptlength; i++)
    {
        temp = plaintext[i];
        
        if(isalpha(temp))
        {
            if(isupper(temp))
            {
                temp = temp + shifted[wrap%klength];
                if(temp > 90)
                {
                    temp = (temp % 90) + 64;
                }
                printf("%c",temp);
                wrap++;
            }
            else if(islower(temp))
            {
                temp = temp + shifted[wrap%klength];
                if(temp > 122)
                {
                    temp = (temp % 122) + 96;
                }
                printf("%c", temp);
                wrap++;
            }
            
        }
        else if(temp == ' ')
        {
            printf(" ");
        }
        else if(temp != ' ' && !isalpha(temp))
        {
            printf("%c",temp);
        }   
        else
        {
            printf("%c",temp);
        }
    }
    
    printf("\n");   
    
}