In: Computer Science
Problem: Text manipulation as a technology has a myriad of applications, including coding and decoding, textual analysis to determine authorship of a document, and automatic translation between languages. Learning to parse and process textual data is a valuable skill to master.
Your assignment: Design, develop, and test an
Object-Oriented C++ program to process multi-word phrases and
sentences in a variety of ways. In this case, the required
processing includes 1) reversing the words in a phrase, 2) sorting
the words within the phrase in ascending order, and 3) applying the
Rot13 encoding to all the characters in the original phrase.
Discussion: Phrases are composed of words, which
are in turn composed of characters. In C++, those characters are
represented as bytes, using the ASCII standard to encode each
character as a number. Parsing a string involves breaking a string
up into its component words, using spaces as the delimiters between
words. This project requires first that the user’s input phrase be
parsed into its component words, and then each word must be handled
as an individual entity. In all the phrase processing except the
Rot13 conversion, words must be kept intact.
Consider the following issues when preparing your solution.
- Create a user-defined Phrase class to manage a phrase and the
words within it. This class must have one or more constructor(s)
for initialization and multiple methods to perform the various
operations on the phrase and its words.
- Create a single instance of the class in function main()
- Repeat the following steps until the user opts to quit:
- Prompt the user for a phrase or sentence, and read it into a single string
- Initialize the Phrase object with the user-input string
- Display the following conversions of the user-input string as produced by public methods of the Phrase class. The case of each letter must be preserved from the input string to each of these conversions. Each conversion must produce a separate output string.
- Reversed word order
- Sorted word order
- Rot13 character encoding
- In the Reversed Word and Sorted Word conversions, all words must remain intact and readable with regards to the spelling and capitalization of each word. For this assignment, leading and trailing punctuation should be considered a part of each word.
Coding:
- Each user-defined class must be defined within its own set of .h (.hpp in Xcode) and .cpp files.
- There should be three files.
- The first file is main.cpp.
- The second file is Phrase.cpp The is the file where the definitions are written for the classes.
- The third file is Phrase.h This is the header file where the functions are declared for the classes.
- The program must accept any string, including multiple words with embedded spaces, as input.
- Validate all inputs and do not proceed until valid values are entered.For input strings, the only validation required is that the input string must not be empty.
- Program must reverses the characters in the words
- Program must sort the characters in the words
Please do not use other solutions that has been provided on here before since they use the same code (all copies of one another) and do not work as required. Please ensure that the program meets ALL of the requirements.
Make the program as simple as possible. Use introductory Object-Oriented C++ programming. If all the requirements are met, then this will be upvoted.
Do use the C++ Standard Library headers, not use the C Standard Library headers (For instance, include cmath instead of math.h)
Do not use cstring’s or any of the cstring functions (strlen(), strcpy(), strcmp(), etc.)
: Design, develop, and test an Object-Oriented C++ program to process multi-word phrases and sentences in a variety of ways. In this case, the required processing includes
1) reversing the words in a phrase
Filename: main.cpp
// C++ program to reverse words in phrase
#include <iostream>
#include<phrase.h>
using namespace std;
int main()
{
string str;
int begin,end,i,j=0,len,temp,count=0;
cout<<"ENTER STRING: ";
getline(cin,str);
//To find the length of string
len=str.length();
//To reverse whole string
for(i=0;i<(len/2);i++)
{
temp=s[i];
str[i]=str[len-1-i];
s[len-1-i]=temp;
}
//To reverse each word seperately
for(i=0;i<len;i++)
{
if(str[i]==' ' || str[i]=='\0')
{
for(begin=j,end=i-1 ; begin<(i+j)/2 ; begin++,end--)
{
temp=str[begin];
str[begin]=str[end];
str[end]=temp;
}
j=i+1;
}
}
cout<<"Reversed string:";
cout<<s;
return 0;
}
2.Filename Phrase.cpp
#include<iostream>
#include<phrase.h>
using namespace std;
int main()
{
string str;
//To find the length of string
n=str.length();
char temp;
cout << "Enter the phrase : " << endl;
for(int i = 0; i < n; ++i)
{
getline(cin, str[i]);
}
for(int i = 0; i < 9; ++i)
for( int j = i+1; j < 10; ++j)
{
if(str[i] > str[j])
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
cout << "In ascending order: " << endl;
for(int i = 0; i < 10; ++i)
{
cout << str[i] << endl;
}
return 0;
}
Filename: phrase.h
//
// ROT13 Caesar Cipher
#include<bits/stdc++.h>
using namespace std;
// Map to lookup the index of alphabets
map <char,int> dict1;
// Map to lookup alphabets corresponding
// to the index after shift
map <int,char> dict2;
// Function to create map to lookup
void create_dict()
{
for(int i = 1; i < 27; i++)
dict1[char(64 + i)] = i;
dict2[0] = 'Z';
for(int i = 1; i < 26; i++)
dict2[i] = char(64 + i);
return;
}
// Function to encrypt the string
// according to the shift provided
string encrypt(string str, int shift)
{
string cipher = "";
for(int i = 0; i < str.size(); i++)
{
// Checking for namespace
if(str[i] != ' ')
{
// loooks up the
map and
// adds the
shift to the index
int num =
(dict1[str[i]] + shift) % 26;
// looks up the
second map for the
// shifted
alphabets and adds them
cipher +=
dict2[num];
}
else
{
// adds
space
cipher += "
";
}
}
return cipher;
}
// Function to decrypt the string
// according to the shift provided
string decrypt(string str, int shift)
{
string decipher = "";
for(int i = 0; i < str.size(); i++)
{
// checks for space
if(message[i] != ' ')
{
// looks up the
map and
// subtracts the
shift to the index
int num =
(dict1[str[i]] - shift + 26) % 26;
// looks up the
second map for the
// shifted
alphabets and adds them
decipher +=
dict2[num];
}
else
{
// adds
space
decipher += "
";
}
}
return decipher;
}
// Driver code
int main()
{
create_dict();
string str;
int shift = 13;
cout << encrypt(str, shift) << "\n";
shift = 13;
cout << decrypt(str, shift) << "\n";
return 0;
}