Whenever I try to return the value of string encode() function I get nothing. Can you please help me out and tell me what's wrong with my code? I want to store the value of the string encode() function in a vector and then print it out on a console. How do I do that? String encode() function replaces every character of the input string with character whose ASCII value is n times more than it.
int largestDig (int num)
{
int largestDig=0;
while(num>0)
{
int digit=num%10;
if (digit>largestDig)
largestDig=digit;
num=num/10;
}
return largestDig;
}
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;
}
int main()
{
string s1;
int numberIndex;
cout<<"Enter a string: ";
getline(cin, s1);
cout<<"Input string is: "<<s1<<"\n";
cout<<"Enter index number (only 3 digits): ";
cin>>numberIndex;
cout<<"The largest digit of "<<numberIndex<<" is: "<<largestDig(numberIndex)<<"\n";
int n=largestDig(numberIndex);
cout<<"The replacement of every character of the string by character whose ASCII value is n times more than it is: ";
encode(s1, n);
return EXIT_SUCCESS;
}
I am sorry that my code isn't written properly, I got error many times trying to format and post it.
int largestDig( int num );
string encode( string s, int k );
string s1;
int n = largestDig( numberIndex );
encode( s1, n );
You have two functions. One returns int, the other string.
For some reason you store the return value from one function call to int n, but discard the return value of the second function call. Why?