Hello, I have to make a menu, 1. compress Text that will give allow you to input a value that will appear in 2. Extract Text.
The problem is that it outputs the print fine but won't accept any value and will infinitely repeat if done otherwise, I tried making choice a char but then it won't output anything inside the case.
What is an example of a "word" outside the ASCII range, and what would it be replaced with?
e.g. [200, 100, 220] --> [46, 100, 46]?
PS: If you're using std::strings, it's not specified whether <char> (string's underlying character type) is signed or unsigned. So if you are expecting input in range [0, 255] you might want to use std::basic_string<unsigned char> directly.
I have no idea how to go about this except to input every single word for the code to detect and if it doesn't move on, but then what will it replace it within the range of 128-225.
#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>
int main()
{
// create a string long enough to contain the standard ASCII table
std::string str(128, ' ');
// fill the string with the ASCII codes from 0 to 127
std::iota(str.begin(), str.end(), 0);
// display the string, excluding the control characters
for (size_t itr { 32 }; itr < str.size(); ++itr)
{
std::cout << str[itr];
}
std::cout << "\n\n";
// transform the string, adding 128 to each char
std::transform(str.begin(), str.end(), str.begin(),
[ ] (unsignedchar c) -> unsignedchar { return c + 128; });
// display the entire transformed string
for (constauto& itr : str)
{
std::cout << itr;
}
std::cout << '\n';
}
The exact output of the transformed string may be different for you.
Do note the lambda at line 23, that allows using std::transform instead of a for loop to "walk through" the entire string to change each element as an unsigned char.
is it possible to replace the text input with ASCII of range 128-255
Just another "minor quibble".
There are no ASCII characters in the range of 128-255, ASCII is a 7 bit encoding all valid ASCII characters are in the range of 0 - 127. While there are "extended ASCII" codes those "extended" codes are not standard and may differ between individual systems. And be careful, if your string is using a char, 128-255 is out of the range of a signed char type.
what I would do is get a statistical breakdown of english and assign the most common pairs of letters (eg "ee", "oo", "tt", "tr", "ph", ...) to the 128...255 range and play switcheroo with that.
that would get you about a 20-25 % compression over simple text if it were all the same case. Mixed case, far less, as you have to repeat the pairs 4 ways.