Question

In: Computer Science

C Programing How would i edit this code to have it return the cipher text. void...

C Programing

How would i edit this code to have it return the cipher text.

void vigenereCipher(char* plainText, char* k){

int i;

char cipher;

int cipherValue;

int len = strlen(k);

//Loop through the length of the plain text string

for(i=0; i<strlen(plainText); i++){

//if the character is lowercase, where range is [97 -122]

if(islower(plainText[i]))

{

cipherValue = ( (int)plainText[i]-97 + (int)tolower(k[i % len])-97 ) % 26 +97;

cipher = (char)cipherValue;

}

else // Else it's upper case, where letter range is [65 - 90]

{

cipherValue = ( (int)plainText[i]-65 + (int)toupper(k[i % len])-65 ) % 26 +65;

cipher = (char)cipherValue;

}

//Print the ciphered character if it is alphanumeric (a letter)

if(isalpha(plainText[i]))

{

printf("%c", cipher);

}

else //if the character is not a letter then print the character (e.g. space)

{

printf("%c", plainText[i]);

}

}

}

Solutions

Expert Solution

Answer:

// change return type to char* as we need to return the cipher text.
char* vigenereCipher(char* plainText, char* k)
{
int i;
char cipher;
int cipherValue;
int len = strlen(k);
char* cipherText = (char*)malloc(sizeof(plainText)*sizeof(char));

//Loop through the length of the plain text string
for(i=0; i<strlen(plainText); i++)
{
//if the character is lowercase, where range is [97 -122]
if(islower(plainText[i]))
{
cipherValue = ( (int)plainText[i]-97 + (int)tolower(k[i % len])-97 ) % 26 +97;
cipher = (char)cipherValue;
}
else // Else it's upper case, where letter range is [65 - 90]
{
cipherValue = ( (int)plainText[i]-65 + (int)toupper(k[i % len])-65 ) % 26 +65;
cipher = (char)cipherValue;
}

//Print the ciphered character if it is alphanumeric (a letter)
if(isalpha(plainText[i]))
{
// Assign the cipher to the cipherText instead of printing it.
*cipherText = cipher;
cipherText++;
}
else //if the character is not a letter then print the character (e.g. space)
{
// Assign the character to the cipherText instead of printing it.
*cipherText = plainText[i];
cipherText++;
}
}
  
return cipherText;
}


Related Solutions

How can I edit this C code to make sure that the letter P and L...
How can I edit this C code to make sure that the letter P and L would show up separately one at a time on an interval of one second on a raspberry pi? 1 #include <stdio.h> 2 #include <unistd.h> 3 #include "sense.h" 4 5 #define WHITE 0xFFFF 6 7 int main(void) { 8     // getFrameBuffer should only get called once/program 9     pi_framebuffer_t *fb=getFrameBuffer(); 10     sense_fb_bitmap_t *bm=fb->bitmap; 11 12      bm->pixel[0][0]=WHITE; 13      bm->pixel[0][1]=WHITE; 14      bm->pixel[0][2]=WHITE; 15      bm->pixel[0][3]=WHITE; 16      bm->pixel[0][4]=WHITE; 17      bm->pixel[0][5]=WHITE;...
I need to understand how I would edit this code without changing anything, only adding. I...
I need to understand how I would edit this code without changing anything, only adding. I am not looking for the exact answer, just information on how I would use the given code to complete some of the todo statements. Thank you! // include this header file so we can use `printf()` #include <stdio.h> // every C program must implement the `main()` function int main(int argc, char *argv[]) {    //TODO: check for enough arguments       // save the...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an input file. For example, with this input: Raspberry Grapefruit Straw berry raspBERRY Blue$$berry apple Pine_apple raspberry The output should be: raspberry grapefruit straw berry blue berry apple pine NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output. #include <stdio.h> #include <stdlib.h> int main() {     //Pointer to access the...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a...
WRITE IN C++ Add to the Coord class Edit the provided code in C++ Write a member function named      int distance2origin() that calculates the distance from the (x, y, z) point to the origin (0, 0, 0) the ‘prototype’ in the class definition will be      int distance2origin() outside the class definition             int Coord :: distance2origin() {                         // your code } _______________________________________________________________________________________________________ /************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks:...
C++ Code! This code was written/implemented using the "class format." How would I go about in...
C++ Code! This code was written/implemented using the "class format." How would I go about in converting it to the "struct format?" #include <iostream> #include <iomanip> using namespace std; class ListNode{ public: string course_name; string course_number; string course_room; ListNode* next; ListNode(){ this->next = NULL; } ListNode(string name, string number, string room){ this->course_name = name; this->course_number = number; this->course_room = room; this->next = NULL; } }; class List{ public: ListNode* head; List(){ this->head = NULL; } void insert(ListNode* Node){ if(head==NULL){ head...
Programing lanugaue is C++ Purpose of code: Plan and code a menu-driven modular program utilizing an...
Programing lanugaue is C++ Purpose of code: Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
HOW CAN I WRITE A PROGRAMING CODE IN ARDUINO DIE ABOUT THE ROOT MEAN SQUARE VALUE...
HOW CAN I WRITE A PROGRAMING CODE IN ARDUINO DIE ABOUT THE ROOT MEAN SQUARE VALUE OF A SIGNAL
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT