C++ how to return string from a function and store it in a vector

Help me out please!!! I want to return string with this function, but whenever I try to it just doesn't return anything. I want to store the output of this funtion into a vector after. How do I do all of that?

string encode(string s,int k){

// changed string
string newS;

// iterate for every characters
for(int i=0; i<s.length(); ++i)
{
// ASCII value
int val = int(s[i]);

// store the duplicate
int dup = k;

// if k-th ahead character exceed 'z'
if(val + k > 122){
k -= (122-val);
k = k % 26;
newS += char(96 + k);
}
else
newS += char(val + k);

k = dup;
}

// print the new string
return newS;
}
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

string encode(string s,int k){

// changed string
string newS;

// iterate for every characters
for(int i=0; i<s.length(); ++i)
{
// ASCII value
int val = int(s[i]);

// store the duplicate
int dup = k;

// if k-th ahead character exceed 'z'
if(val + k > 122){
k -= (122-val);
k = k % 26;
newS += char(96 + k);
}
else
newS += char(val + k);

k = dup;
}

// print the new string
return newS;
}


First can I ask you what is your goal, what do you want to do with the string s?

Also give more context on your parameters, what does k represent?
Last edited on
First, code tags make posted code easier to read. See https://www.cplusplus.com/articles/jEywvCM9/

You have shown the implementation of function string encode( string s, int k );
It seems to add characters to string 'newS' and return that string.

You have not shown your attempt to use this function. We have no idea what you have tried. Hence, we can't tell if it had an error. Please show.
Topic archived. No new replies allowed.