Question

11.3 LAB: Convert String to Cyphertext cstring

mplement the following (Called a Ceasar Cypher, since it was used in Ceasars time) using this function prototype in the same file char cypher (string str, int rotate); The idea is that you will declare a variable of type string and give it a value in main. Then pass it into the cypher function. Cypher will create a cstring by copying the str to a new char* of size str.size). Remember that you can covert with str.c.str() Then you will rotate ever letter by an amount passed into the function. If you look at the ascii table You will see that A is 65 and z is 122. We want to keep every character between these ascii codes. So, you will probably need an if statement noting: If after adding the rotate value and taking the modulus of one after z (123), the value is between 65 and 123, then set the character to that value. Otherwise, because you did a mod of 123, you know the number is no larger than 122 (then goes back to zero.. so in that case you would want to add 65 to take it to A Dont forget to return the char. Then print out the cstring with puts.e.g char* cstrnew char [sizel: puts (cstr) For output, set the string to ABC and call cypher(ABC, O) #include «string> for test purpose result

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cstring>
#include<string>
using namespace std;

char* cypher(string str, int rotate)
{
    int size=str.size();
    char* cstr=new char[size];
    cstr=&str[0];
    for (int i=0; i < size; i++)
    {
        if (*(cstr+i) >= 'A' && *(cstr+i) <= 'Z')
        {
            *(cstr+i) = (char)(((*(cstr+i) + rotate - 'A' + 26) % 26) + 'A');
        }
        else if (*(cstr+i) >= 'a' && *(cstr+i) <= 'z')
        {
            *(cstr+i) = (char)(((*(cstr+i) + rotate - 'a' + 26) % 26) + 'a');
        }
    }
    return cstr;
}

int main()
{
    char* s;
    s=cypher("ABC",0);
    puts(s);
    return 0;
}

С https://www.onlinegdb.com/online-c++ compiler Run Debug StopShare SavBeautify LanguageC++ OnlineGDB beta main. cpp 4 using namespace std; code. compile. run. debug. share. IDE My Projects Learn Programming Programming Questions Sign Up Login 6 char cypher (string str, int rotate) int size-str.size(); char cstr-new char[size]: cstr=&str[0]; for (int i-e; i< size; i++) 18 12 13 14 15 16 17 18 19 28 21 (cstr恒) = (char)(((*(cstr-i) + rotate .A + 26) % 26) + A); - 11.JK (cstr-i) (char)(((*(cstr+1) + rotate- a + 26) % 26) + a ); return cstr; 23 24 25 int main() 26 27 28 29 30 slack char* s; s-cypher(ABC,0); All the tools your team needs in ornc place. Slack: Where work happens. ADS VIA CARBON return 8 input Program finished with exit oode 0 S ENTER to exit oonsole About . ГАС2-Blog . Terms of Use . Contact Us . 2018 GDB Online

Add a comment
Know the answer?
Add Answer to:
11.3 LAB: Convert String to Cyphertext cstring mplement the following (Called a Ceasar Cypher, since it...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please explain lowing function should allocate space for a new string, copy the nd convert every...

    Please explain lowing function should allocate space for a new string, copy the nd convert every string from the passed argument into the new string, a lower-case character in the new string into an upper-case modify the original st character (do not ring). Fill-in the blanks and the body of the for0 loop: char* upcase char str)t char p; char* result; result, (char*) malloc( - strcpy( for( p-result; *p!-10': p++) / Fill-in 'A'-65, 'a' 97, 'Z 90, 'z' 122/ return...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • Write this function in c++.Use of string and <cstring> is not allowed .You are required to...

    Write this function in c++.Use of string and <cstring> is not allowed .You are required to use char* and implement the use of ** in this question . Gtest case to pass: #include "q1.cpp" #include <gtest/gtest.h> //-------------------Q1_8----------------- TEST(Question1_8, First) { char t1[]="Hello World"; char res1[] = "Hello"; char res2[] = "World"; char** r= StrTok(t1,' '); ASSERT_EQ(0, strcmp(r[0],res1) ); ASSERT_EQ(0, strcmp(r[1],res2) ); } TEST(Question1_8, Second) { char t1[]="Hello?World?OOP"; char res1[] = "Hello"; char res2[] = "World"; char res3[] = "OOP"; char**...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • Write a new subroutine in assembly to convert the upper-case letters to lower-case letter. Example of...

    Write a new subroutine in assembly to convert the upper-case letters to lower-case letter. Example of lower case to upper is provided below: Let’s look at a subroutine to capitalize all the lower-case letters in the string. We need to load each character, check to see if it is a letter, and if so, capitalize it. Each character in the string is represented with its ASCII code. For example, ‘A’ is represented with a 65 (0x41), ‘B’ with 66 (0x42),...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • 1. Write a C++ program called Password that handles encrypting a password. 2. The program must...

    1. Write a C++ program called Password that handles encrypting a password. 2. The program must perform encryption as follows: a. main method i. Ask the user for a password. ii. Sends the password to a boolean function called isValidPassword to check validity. 1. Returns true if password is at least 8 characters long 2. Returns false if it is not at least 8 characters long iii. If isValidPassword functions returns false 1. Print the following error message “The password...

  • Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string"...

    Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string" is a sequence of characters. In the C language anything within double quotes is a "string constant" so you have been seeing strings all semester. But we can also have string variables. In the C language these are implemented as an array of char, e.g. char name (10]: In order to make these variables easier to work with, it has been universally agreed that...

  • I need help finding what is wrong with this code, it is for a CS course...

    I need help finding what is wrong with this code, it is for a CS course I am taking which codes in C (I am using XCode on Mac to code). This is the assignment: "Write a program that performs character processing on 10 characters read in from a file, and writes the results to output files. The program should read from “input.dat”. The program should write the ASCII values of the characters to “output_ascii.dat”. The program should print statistics...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT