diff --git a/algorithms/ciphers/vigenere_cipher.c b/algorithms/ciphers/vigenere_cipher.c new file mode 100644 index 00000000..9ab03396 --- /dev/null +++ b/algorithms/ciphers/vigenere_cipher.c @@ -0,0 +1,138 @@ +/* +======================================================= +Algorithm: Vigenere Cipher +======================================================= +*/ +#include +#include +#include +#include + +void encryption (); +void decryption (); + +int main() { + + int choice; + +retry: + // Taking users choice + printf("Choose an option: \n"); + printf(" 1. Encryption\n 2. Decryption\n"); + scanf("%d", &choice); + while (getchar() != '\n'); + + if (choice == 1) { + encryption(); + } else if (choice == 2) { + decryption(); + } else goto retry; + + return 0; +} + +// envryption function +void encryption () { + + char chain[100]; + char key[100]; + char text[100]; + char alphabet[] = "abcdefghijklmnopqrstuvwxyz"; + int poschain, poskey, postext; + int lenchain, lenkey; + int textIndex = 0; + + // Taking Plaintext + system("cls || clear"); + printf("Enter plaintext : "); + scanf("%[^\n]", chain); + while (getchar() != '\n'); + + // Taking key + printf("Enter encryption key : "); + scanf("%[^\n]", key); + while (getchar() != '\n'); + system("cls || clear"); + + for (int i = 0; chain[i]; i++) { + chain[i] = tolower(chain[i]); + } + for (int i = 0; key[i]; i++) { + key[i] = tolower(key[i]); + } + + lenchain = strlen(chain); + lenkey = strlen(key); + // Encryption + for (int i = 0; i < lenchain; i++) { + poskey = i % lenkey; + + poschain = chain[i] - 'a'; + poskey = key[poskey] - 'a'; + + //Text concatenation + postext = (poschain + poskey) % 26; + text[textIndex++] = alphabet[postext]; + } + + // terminate the string with a '\0' + text[textIndex] = '\0'; + + // print result + system("cls || clear"); + printf("-======[ Vigenere cipher ]============-\n\n"); + printf("\t- Plaintext: %s\n", chain); + printf("\t- Key : %s\n", key); + printf("\t- Vigenere ciphertext : %s\n", text); + getchar (); +} + +void decryption () { + + char chain[100]; + char key[100]; + char text[100]; + char alphabet[] = "abcdefghijklmnopqrstuvwxyz"; + int poschain, poskey, postext; + int lenchain, lenkey, choice; + int textIndex = 0; + + system("cls || clear"); + printf("Enter ciphertext : "); + scanf("%[^\n]", chain); + while (getchar() != '\n'); + + printf("Enter encryption key : "); + scanf("%[^\n]", key); + while (getchar() != '\n'); + + // converting chain and key to lowercase to facilitate string management + for (int i = 0; chain[i]; i++) { + chain[i] = tolower(chain[i]); + } + for (int i = 0; key[i]; i++) { + key[i] = tolower(key[i]); + } + + lenchain = strlen(chain); + lenkey = strlen(key); + + for (int i = 0; i < lenchain; i++) { + poskey = i % lenkey; + + poschain = chain[i] - 'a'; + poskey = key[poskey] - 'a'; + + //Text concatenation + postext = (poschain - poskey + 26) % 26; + text[textIndex++] = alphabet[postext]; + } + + text[textIndex] = '\0'; + + system("cls || clear"); + printf("-======[ Vigenere cipher ]============-\n\n"); + printf("\t- Vigenere ciphertext: %s\n", chain); + printf("\t- Key : %s\n", key); + printf("\t- Plaintext : %s\n", text); +} diff --git a/algorithms/roundrobin.c b/algorithms/roundrobin.c index c3c88c52..75a62d1c 100644 --- a/algorithms/roundrobin.c +++ b/algorithms/roundrobin.c @@ -5,29 +5,29 @@ int main() int i, limit, total = 0, x, counter = 0, time_quantum; int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10]; float average_wait_time, average_turnaround_time; - printf("nEnter Total Number of Processes:t"); + printf("\nEnter Total Number of Processes: "); scanf("%d", &limit); x = limit; for(i = 0; i < limit; i++) { - printf("nEnter Details of Process[%d]n", i + 1); + printf("\nEnter Details of Process[%d]\n", i + 1); - printf("Arrival Time:t"); + printf("Arrival Time: "); scanf("%d", &arrival_time[i]); - printf("Burst Time:t"); + printf("Burst Time: "); scanf("%d", &burst_time[i]); temp[i] = burst_time[i]; } - printf("nEnter Time Quantum:t"); + printf("\nEnter Time Quantum: "); scanf("%d", &time_quantum); - printf("nProcess IDttBurst Timet Turnaround Timet Waiting Timen"); + printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n"); for(total = 0, i = 0; x != 0;) - { + { if(temp[i] <= time_quantum && temp[i] > 0) { total = total + temp[i]; @@ -42,7 +42,11 @@ int main() if(temp[i] == 0 && counter == 1) { x--; - printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]); + printf("\nProcess [%d]\t\t%d\t\t %d\t\t\t %d", i + 1, + burst_time[i], + total - arrival_time[i], + total - arrival_time[i] - burst_time[i]); + wait_time = wait_time + total - arrival_time[i] - burst_time[i]; turnaround_time = turnaround_time + total - arrival_time[i]; counter = 0; @@ -63,7 +67,8 @@ int main() average_wait_time = wait_time * 1.0 / limit; average_turnaround_time = turnaround_time * 1.0 / limit; - printf("nnAverage Waiting Time:t%f", average_wait_time); - printf("nAvg Turnaround Time:t%fn", average_turnaround_time); + printf("\n\nAverage Waiting Time: %.2f", average_wait_time); + printf("\nAvg Turnaround Time: %.2f\n", average_turnaround_time); + getchar(); return 0; }