Question

C Programming I have this function to i want to return the cipher text, but its...

C Programming

I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong.

I need it 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++;

}

}

printf("%s", cipherText); //<------TRY PRINTING IT HERE AND NOTHING!!!!!

//return cipherText;

return strdup(cipherText);

}

Homework Answers

Answer #1

Here, you can check my C++ code.

// C++ code to implement Vigenere Cipher 
#include<bits/stdc++.h> 
using namespace std; 
  
// This function generates the key in 
// a cyclic manner until it's length isi'nt 
// equal to the length of original text 
string generateKey(string str, string key) 
{ 
    int x = str.size(); 
  
    for (int i = 0; ; i++) 
    { 
        if (x == i) 
            i = 0; 
        if (key.size() == str.size()) 
            break; 
        key.push_back(key[i]); 
    } 
    return key; 
} 
  
// This function returns the encrypted text 
// generated with the help of the key 
string cipherText(string str, string key) 
{ 
    string cipher_text; 
  
    for (int i = 0; i < str.size(); i++) 
    { 
        // converting in range 0-25 
        char x = (str[i] + key[i]) %26; 
  
        // convert into alphabets(ASCII) 
        x += 'A'; 
  
        cipher_text.push_back(x); 
    } 
    return cipher_text; 
} 
  
// This function decrypts the encrypted text 
// and returns the original text 
string originalText(string cipher_text, string key) 
{ 
    string orig_text; 
  
    for (int i = 0 ; i < cipher_text.size(); i++) 
    { 
        // converting in range 0-25 
        char x = (cipher_text[i] - key[i] + 26) %26; 
  
        // convert into alphabets(ASCII) 
        x += 'A'; 
        orig_text.push_back(x); 
    } 
    return orig_text; 
} 
  
// Driver program to test the above function 
int main() 
{ 
    string str = "GEEKSFORGEEKS"; 
    string keyword = "AYUSH"; 
  
    string key = generateKey(str, keyword); 
    string cipher_text = cipherText(str, key); 
  
    cout << "Ciphertext : "
         << cipher_text << "\n"; 
  
    cout << "Original/Decrypted Text : "
         << originalText(cipher_text, key); 
    return 0; 
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
This is C programming assignment. The objective of this homework is to give you practice using...
This is C programming assignment. The objective of this homework is to give you practice using make files to compose an executable file from a set of source files and adding additional functions to an existing set of code. This assignment will give you an appreciation for the ease with which well designed software can be extended. For this assignment, you will use both the static and dynamic assignment versions of the matrix software. Using each version, do the following:...
C Programming I am trying to also print the frequency and the occurrence of an input...
C Programming I am trying to also print the frequency and the occurrence of an input text file. I got the occurrence to but cant get the frequency. Formula for frequency is "Occurrence / total input count", this is the percentage of occurrence. Can someone please help me to get the frequency to work. Code: int freq[26] = {0}; fgets(input1, 10000, (FILE*)MyFile); for(i=0; i< strlen(input); i++) { freq[input[i]-'a']++; count++; } printf("Text count = %d", count); printf("\n"); printf("Frequency of plain text\n");...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...