Return string using function.

Hi i'm stuck on this question.

// create a function that inputs a seed and used the function below create_permutation to generate a pairing of the alphabet and returns the corresponding enciphering permutation and paring of string e.g(AXYDUT = (A,X),(Y,D),...//

I need to return a permutation of a string input, but it only returns a permutation of the ALPHABET string. I assume because of the const ALPHABET string.

I'm sure what i've tried is completely wrong.

thanks for any help.

I change it so it compiles.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' ";

string create_permutation(unsigned seed)
{
srand(seed);
string permutation = ALPHABET;
// using built-in random generator:
random_shuffle(permutation.begin(), permutation.end());
return permutation;
}
string reflector()
{
    int seed = 2;
    string permu = create_permutation(seed);
    string refl;
    cout << "enter string" << endl;
    getline(cin,refl);

    for(int i=0; i<refl.length(); i++)
    {
        for(int j=0; j<ALPHABET.length();j++)
        {
            if(refl[i] == permu[j])
            {
                cout << refl[i];
            }
        }
    }
}
Last edited on
I'm sure what i've tried is completely wrong.

That's a fairly safe bet, have you even tried to compile this code? There are quite a few warnings and errors being generated when I try to compile the program. Perhaps you should start by fixing the warnings and errors before proceeding.

Yeah i know, it should compile now.

i wanted to know how to return a string using the create_permutation function. i want it to return the character in the string not the whole alphabet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>
using namespace std;

const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' ";

string create_permutation(unsigned seed)
{
srand(seed);
string permutation = ALPHABET;
// using built-in random generator:
random_shuffle(permutation.begin(), permutation.end());
return permutation;
}

//it supposed to be similar to this. with strings and not one character. Such as AXYDUT maps
//A - X, Y - D, U - T.

string inversePermutation()
{

    string permuStr;

    cout << "enter string" << endl; // example ***B*,
    getline(cin,permuStr);

    for(int i = 0; i < permuStr.length(); i++)
    {
        for (int j = 0; j < ALPHABET.length(); j++)
        {
            if (permuStr[i] == ALPHABET[j])
            {
               string d;

               d[i] = ALPHABET[i];
               cout << "        Permutation: " << permuStr << endl;
               swap(d[i],permuStr[i]);
               swap(permuStr[i],permuStr[j]);
            }
        }
    }

    string invPermStr;
    invPermStr = permuStr;

    cout << "Inverse Permutation: " << invPermStr;
}


This basically returns the position and letter of the character :
input ***B*
output *D***





Last edited on
i wanted to know how to return a string using the create_permutation function. i want it to return the character in the string not the whole alphabet.

What? In the first sentence you say you want to return a string, but in the second sentence you say you want to return a character.

What are you trying to return?

By the way your function "string inversePermutation()" should be returning a string.
for example string a = "ABCD"

i want to return a pairing of the string a ="ABCD" using create_permutation() function.

so it should pair and map the letters next to each other. (A,B),(C,D)....etc so like the inversePermutation() function, A returns B and C to D.
Last edited on
Topic archived. No new replies allowed.