Created
September 11, 2020 19:44
-
-
Save gokhancerk/a6113ea06254eada495c672a738cb3ff to your computer and use it in GitHub Desktop.
cs50 my vigenere task solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://pastebin.pl/view/0cdfb99e | |
#include <stdio.h> | |
#include <cs50.h> | |
#include <ctype.h> | |
#include <stdlib.h> | |
#include <string.h> | |
bool checkKeyword(string key); | |
int shiftValue(char c); | |
void cipher(char plainText, int key); | |
int main(int argc, string argv[]) | |
{ | |
if(argc == 2) | |
{ | |
bool isNumber = checkKeyword(argv[1]); | |
int count = 0; | |
if(isNumber) | |
{ | |
//printf("%i",shiftValue(argv[1][0])); //baz | |
string plainText = get_string("plain text: "); | |
printf("ciphertext: "); | |
for(int i=0; i < strlen(plainText); i++) | |
{ | |
if(i >= strlen(argv[1])) | |
{ | |
for(int j=count; j <= count; j++) | |
{ | |
if(j < strlen(argv[1])) | |
{ | |
if(plainText[i] == 44) | |
{ | |
cipher(plainText[i],0); | |
count -= 1; | |
}else if(plainText[i] == 32) | |
{ | |
cipher(plainText[i],0); count -= 1; | |
}else | |
{ | |
cipher(plainText[i],shiftValue(argv[1][j])); | |
} | |
// printf(" i: ( %i )", i); | |
// printf(" j: ( %i )", j); | |
// printf(" [ %c ] ", plainText[i]); | |
} | |
} | |
if(count == strlen(argv[1])-1) | |
count = -1; | |
count++; | |
} | |
else{ | |
cipher(plainText[i],shiftValue(argv[1][i])); | |
} | |
} | |
printf("\n"); | |
return 0; | |
} | |
}else | |
{ | |
printf("Usage: ./vigenere keyword\n"); | |
} | |
return 1; | |
} | |
/** | |
plainText: plaintext (düz metin) | |
key: shift value (kayma değeri) | |
**/ | |
void cipher(char plainText, int key) | |
{ | |
int shiftValue = plainText; // harfin ascii konumu | |
// printf("plaintext: %c ", shiftValue); | |
// printf("ascii: %i ", shiftValue); | |
// printf("shift value: %i\n ",key); | |
if(shiftValue >= 65 && shiftValue <= 90) // A...Z | |
{ | |
for(int i=0; i < key; i++) | |
{ | |
if(shiftValue < 90) | |
{ | |
shiftValue += 1; | |
}else if(shiftValue == 90) | |
{ | |
shiftValue = 65; | |
} | |
} | |
//printf("%c : \n", shiftValue); | |
} | |
else if(shiftValue >= 97 && shiftValue <= 122) // a...z | |
{ | |
for(int i=0; i < key; i++) | |
{ | |
if(shiftValue < 122) | |
{ | |
shiftValue += 1; | |
}else if(shiftValue >= 122) | |
{ | |
shiftValue = 97; | |
} | |
} | |
//printf("%c : \n", shiftValue); | |
} | |
printf("%c", shiftValue); | |
} | |
bool checkKeyword(string key) | |
{ | |
for(int i=0, n = strlen(key); i < n; i++) | |
{ | |
if(isdigit(key[i])) | |
{ | |
printf("invalid keyword\n"); | |
return false; | |
} | |
} | |
return true; | |
} | |
int shiftValue(char c) | |
{ | |
int getKey = c; | |
int count = 0; | |
if(getKey > 90) // a...z | |
{ | |
for(int x=97; x<=122; x++) | |
{ | |
if(getKey == x) | |
{getKey = count; break;} | |
count++; | |
} | |
return getKey; | |
} | |
else | |
{ | |
for(int x=65; x<=90; x++) // A...Z | |
{ | |
if(getKey == x) | |
{getKey = count; break;} | |
count++; | |
} | |
return getKey; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment