code in c++ starter code provided #include<iostream> #include<iostream> using st
ID: 3757483 • Letter: C
Question
code in c++ starter code provided
#include<iostream>
#include<iostream>
using std::cout; using std::cin; using std::endl;
#include<iomanip>
using std::setprecision;
#include<string>
using std::string;
// any other includes you think you might need here
// global variable for count -> char code
const string code = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//
// your functions here
//
int main (){
long test_num;
cin >> test_num;
switch(test_num) {
case 1: {
string input;
char sep;
cin >> input;
cin >> sep;
cout << encode_sequence(input, sep);
break;
}
case 2:{
string input;
char sep;
cin >> input;
cin >> sep;
cout << encode(input, sep);
break;
}
case 3:{
string input;
char sep;
cin >> input;
cin >> sep;
cout << decode_sequence(input, sep);
break;
}
case 4:{
string input;
char sep;
cin >> input;
cin >> sep;
cout << decode(input, sep);
break;
}
case 5:{
string s1,s2;
cin >> s1;
cin >> s2;
cout << setprecision(2);
cout << reduction(s1,s2);
break;
}
} // of switch
} // of main
1 #includec iostream> Related Items d: :couti using std::cin; using std::endl; 4 using std::setprecision; 5 #includec string> 6 using std::string; II any other includes you think you might need here 9 // global variable for count ->char code 10 const string cade"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 13 / your functions here 16 int main 17 long testnum; 18 cin >>test_num; 19 20 switch(test num) 21 22 case 1: string input; char sepi cin >> input; cin >> sepi cout input; cin >> sepi cout input; cin >> sepi cout
Explanation / Answer
#include<iostream>
using std::cout; using std::cin; using std::endl;
#include<iomanip>
using std::setprecision;
#include<string>
using std::string;
// any other includes you think you might need here
// global variable for count -> char code
const string code = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string encode_sequence(string str, char c) //encode_sequence function takes string and char c
{
int len = str.length(); //takes the length of str into len
if(len>3) //if len is greater than 3 then goes for encoding
{
char x = code[len-4]; //takes the char from the code with the index len-4
string s = ""; //creation of string s
s += c; //concatenates char c to s
s += x; //concatenates char x to s.
s += str[0]; //concatenates char str[0] to s
return s; //return s
}
else return str; //else returns the str itself.
}
string encode(string str, char c)
{
string newstr = "";
int len = str.length(),start=0,end=0,count=1;
for(int i=1;i<len;i++) //loops through the str
{
if(str[i]==str[i-1]) //if str[i] and str[i-1] matches then
count++; //increases count
else{
end=i; //assings end to i
if(count>3) //if count >3 then
{
newstr += encode_sequence(str.substr(start,end-start),c); //calls encode_sequence function
}
else newstr += str.substr(start,end-start); //else concatenates the substring of str to newstr
count=1; //assings count to 1
start = i; //assigns start to i
}
}
//below code for the ending sequence
if(count>3) //if count is lessthan 3
{
end = len;
newstr += encode_sequence(str.substr(start,end-start),c);
}
else newstr += str.substr(start,len-start);
return newstr;
}
string decode_sequence(string str, char c) //decode_Sequence function
{
int len = str.length();
string s = "";
if(str[0]==c)
{
int repeat = 0;
for(int j=0;j<code.length();j++) //loops through the code and looks for the char str1[1]
{
if(str[1]==code[j]){ //if matches
repeat = j+4; //assigns j+4 to repeat and
break; //breaks the loop
}
}
for(int j=0;j<repeat;j++)
s += str[2];
}
else return str;
return s;
}
string decode(string str, char c) //decode function
{
int len = str.length();
string s="";
for(int i=0;i<len;i++)
{
int repeat = 0;
if(str[i]==c)
{
s += decode_sequence(str.substr(i,3),c);
i=i+2;
}
else s += str[i];
}
return s;
}
double reduction(string original, string encoded) //reduction function
{
return encoded.length()/(double)original.length();
}
int main (){
long test_num;
cin >> test_num;
switch(test_num) {
case 1: {
string input;
char sep;
cin >> input;
cin >> sep;
cout << encode_sequence(input, sep);
break;
}
case 2:{
string input;
char sep;
cin >> input;
cin >> sep;
cout << encode(input, sep);
break;
}
case 3:{
string input;
char sep;
cin >> input;
cin >> sep;
cout << decode_sequence(input, sep);
break;
}
case 4:{
string input;
char sep;
cin >> input;
cin >> sep;
cout << decode(input, sep);
break;
}
case 5:{
string s1,s2;
cin >> s1;
cin >> s2;
cout << setprecision(2);
cout << reduction(s1,s2);
break;
}
} // of switch
} // of main
//any query, post inthe comments
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.