-2

The code gets an exception of type out of range and i don't know why. It seems to work when i debug it, teh string is converted to what i want it to be. First time on stack overflow btw:)

#include <iostream>

using namespace std;

string s;
string alpha = "abcdefghijklmnopqrstuvwxyz";
string crypto(string& s);

int main()
{
    cin >> s;

    cout << crypto(s);

    return 0;
}

string crypto(string& s)
{

    size_t i = 0;
    while (i < s.length()) {
        for (size_t j = 0; j < alpha.length(); j++) {
            if (s.at(i) == alpha.at(j)) {
                s.at(i) = alpha.at(alpha.length() - 1 - j);
                ++i;
            }
        }
    }

    return s;
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30

4 Answers4

0

Think about the case: if s.length() < alpha.length().

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
0

The inner loop of the crypto function can increment i past the end of the string. There's no test to stop it.

You could avoid this by breaking out of the loop when a letter is changed (that's more correct and potentially faster). That then means you should increment i outside the inner loop which means the outer loop can be a for loop as well.

string crypto(string &s) {
  for (size_t i = 0; i < s.length(); ++i) {
    for (size_t j = 0; j < alpha.length(); ++j) {
      if (s.at(i) == alpha.at(j)) {
        s.at(i) = alpha.at(alpha.length() - 1 - j);
        break;
      }
    }
  }
  return s;
}
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
0

The problem was the i++ at the wrong place. You should format your code better, then this is much easier to spot.

Also avoid non-constant global variables and using namespace std:

#include <iostream>
#include <string> //Include the headers you use

const std::string alpha="abcdefghijklmnopqrstuvwxyz";
void crypto(std::string &s);

int main(){

    std::string s;
    crypto(s);
    std::cout<<s;
    return 0;
}

void crypto(std::string &s) //If you take the string by reference, it is changed, so you do not have to return it
{   
    for(std::size_t i = 0; i < s.length(); ++i) { //The for loop avoids the mistake completely
      for(size_t j=0; j < alpha.length(); ++j){
        if(s.at(i)==alpha.at(j)){
          s.at(i)=alpha.at(alpha.length()-1-j);
        }   
      }
    }
}

To not solve your problem completely, there is still a bug in the code, that was also in yours. Try to find the error yourself.

Henk
  • 826
  • 3
  • 14
0

The i++ was not put at the right place.

Moreover, there is another issue in the code: once an element has been replaced, you must leave the inner loop immediately (break). If not, you can have a -> z -> a in the same loop.

Input

 abcyz

Output

zyxba
abcyz
#include <iostream>
#include <string>

std::string crypto(std::string& s) {
    const std::string alpha = "abcdefghijklmnopqrstuvwxyz";
    for (size_t i = 0; i < s.length(); ++i) {
        for (size_t j = 0; j < alpha.length(); j++) {
            if (s.at(i) == alpha.at(j)) {
                s.at(i) = alpha.at(alpha.length() - 1 - j);
                break;
            }
        }
    }
    return s;
}

int main() {
    std::string s;
    std::cin >> s;
    std::cout << crypto(s) << std::endl;
    std::cout << crypto(s) << std::endl;
    return 0;
}
Damien
  • 4,809
  • 4
  • 15
  • 20