String problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
using namespace std;
int main()
{
string StringToEncrypt;
cin >> StringToEncrypt;
char KeyToEncrypt = '@';
int temp = 0;
while (temp < StringToEncrypt.size())
{
temp++;
StringToEncrypt[temp] ^= KeyToEncrypt;
}
cout << "\n\n";
cout << StringToEncrypt;
cout << "\n\n\n";
system("pause");
return 0;
}
|
I wrote a program that uses XOR encryption but when you type in two words seperated by a space e.g "Hello world." it only outputs the first word.
Last edited on
You increment temp early,
cin >> std::string reads only one word.
What should I use as an alternative, something that reads more than one word.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
#include <cctype>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
char encrypt(char ch1, char ch2)
{
if(isspace(ch1))
return ch1;
return ch1 ^ ch2;
}
int main()
{
transform(istreambuf_iterator<char>(cin), istreambuf_iterator<char>(),
ostreambuf_iterator<char>(cout), bind2nd(ptr_fun(encrypt), '@'));
return 0;
}
|
Ok?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cctype>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace std;
char encrypt(char ch1, char ch2)
{
if(isspace(ch1))
return ch1;
return ch1 ^ ch2;
}
int main()
{
transform(istreambuf_iterator<char>(cin), istreambuf_iterator<char>(),
ostreambuf_iterator<char>(cout), bind2nd(ptr_fun(encrypt), '@'));
return 0;
}
Ok? |
I don't even....
Topic archived. No new replies allowed.