For example, let's say you have this encrypted text message "ASBBU KQMCF"
in input.txt
, and when you decrypt it, it will become "HELLO WORLD"
in output.txt
.
As you can see, "A"
becomes "H"
, "S"
becomes "E"
, "B"
becomes "L"
, "U"
becomes "O"
, "K"
becomes "W"
, "Q"
becomes "O"
, "M"
becomes "R"
, "C"
becomes "L"
, and "F"
becomes "D"
.
The problem is that, when I decrypt the whole text, it's in one piece and there are no spaces. It looks like this: "HELLOWORLD"
, while it has to look like this: "HELLO WORLD"
. Is it possible to read the text and decrypt it while retaining the spaces that are between words?
I wrote a code that reads a letter and changes it to the correct one, as shown in the code, but I can't figure out how to make spaces between words.
Here's my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
char ch;
ifstream read("input.txt");
ofstream write("output.txt");
while(read>>ch){
switch (ch) {
case 'A':
ch='H';
write<<ch;
}
switch(ch){
case 'S':
ch='E';
write<<ch;
}
switch(ch){
case 'B':
ch='L';
write<<ch;
}
switch(ch){
case 'U':
ch='O';
write<<ch;
}
switch(ch){
case 'K':
ch='W';
write<<ch;
}
switch(ch){
case 'Q':
ch='O';
write<<ch;
}
switch(ch){
case 'M':
ch='R';
write<<ch;
}
switch(ch){
case 'C':
ch='L';
write<<ch;
}
switch(ch){
case 'F':
ch='D';
write<<ch;
}
}
read.close();
write.close();
return 0;
}
CodePudding user response:
operator>>
skips leading whitespace by default, and then reads until whitespace is encountered. As such, your code will never see space characters in your ch
variable.
Since you want to preserve whitespace, you should use std::getline()
instead of operator>>
. Read the file line-by-line into a std::string
, and then you can manipulate the characters in each std::string
as needed.
Also, you are misusing switch
. You don't need a separate switch
for each case
. That is no better than just using a bunch of if-else
blocks. You can have multiple case
s in a single switch
(that is its primary design - to make a selection from multiple choices).
Try this instead:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str;
ifstream read("input.txt");
ofstream write("output.txt");
while (getline(read, str)) {
for(char &ch : str) {
switch (ch) {
case 'A':
ch = 'H';
break;
case 'S':
ch = 'E';
break;
case 'B':
ch = 'L';
break;
case 'U':
ch = 'O';
break;
case 'K':
ch = 'W';
break;
case 'Q':
ch = 'O';
break;
case 'M':
ch = 'R';
break;
case 'C':
ch = 'L';
break;
case 'F':
ch = 'D';
break;
}
}
write << str << '\n';
}
read.close();
write.close();
return 0;
}
Also, just on a side note: your "encryption" has a flaw - you have both B
and C
being "encrypted" into L
, and both Q
and U
being "encrypted" into O
. Which means you will lose data on "decryption" if either L
and/or O
are present, since you won't know which original characters to "decrypt" them back to. Each unique input characters needs a unique output character.
CodePudding user response:
im not sur about what u want to do .
but like i see before u can do unique switch.
and use default case where u write actual char like
write<<ch;
on this example i use switch to print hello world and change some letter
#include <iostream>
#include <string>
int main()
{
std::string str = "Hello, world!";
for (char c : str)
{
switch (c)
{
case 'H':
std::cout << "S" << std::endl;
break;
case 'e':
std::cout << "Q" << std::endl;
break;
default:
std::cout << c << std::endl;
}
}
return 0;
}
Do you think you can reuse this example to fix your issue?