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...

  • 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...

  • #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 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...

  • 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...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

  • 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...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

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
ADVERTISEMENT