0

I am writing simple program to reverse string using solidity ,everything looks ok but while executing the program am getting the error

"call to reverseString.reverseStr errored : VM exception while processing transaction Invalid opcode"

contract reverseString{

string a;

function setA(string memory x)public{
    a = x;
}

function reverseStr() public view returns(string memory){
    bytes memory entString = bytes(a);

    string memory revStrings =new  string(entString.length);

    bytes memory revString = bytes(revStrings);

    uint k=0;
    require(revString.length>0);

    for(uint i=revString.length;i>0 ;i--){
       revString[k++] = entString[i];
   }

    return string(revString);
}

}

0

The first time through your loop, i is revString.length, which is also the length of entString. So entString[i] is trying to read one element past the end of the array.

I haven't checked for other issues, but that first issue should be fixed with:

for (uint i = revString.length - 1; i >= 0; i--) {

EDIT

The above code will produce an infinite loop, because i >= 0 is always true, given that i is unsigned. You could switch to using a signed variable or count down from revString.length to > 0 and use i - 1 as your index, but it's probably simpler to just write the loop in the more straightforward way (forward):

for (uint256 i = 0; i < revString.length; i++) {
    revString[i] = entString[revString.length - i - 1];
}

Simplifying the whole function to use fewer variables:

bytes memory b = bytes(a);
bytes memory ret = new bytes(b.length);

for (uint256 i = 0; i < b.length; i++) {
    ret[i] = b[b.length - i - 1];
}

return string(ret);
  • I tried the same but still getting the same error : function reverseStr() public view returns(string memory){ bytes memory entString = bytes(a); string memory revStrings =new string(entString.length); bytes memory revString = bytes(revStrings); uint k=0; require(revString.length>0); for(uint i=revString.length-1;i>=0 ;i--){ revString[k++] = entString[i]; } return string(revString); } – Punnu Sinha Apr 26 at 17:59
  • Ah, the remaining issue is because uints, being unsigned, are always greater than or equal to zero. The i-- is underflowing the integer, so you have an infinite loop. There are a few fixes... I'll edit my answer now with a couple. – smarx Apr 26 at 23:00
  • any further update on it – Punnu Sinha Apr 30 at 7:42
  • Are you still having some sort of trouble? If so, what? – smarx Apr 30 at 11:28
0

=> myContract.reverseStrMain() will return reversed string for the myString variable.

=> myContract.reverseStrArg('HelloWorld') will return reversed string for the given argument.

I did single arithmatic operation (subtraction) inside the for loop.


pragma solidity ^0.5.7;

contract myContract {
    string myString;

    function setStr(string memory _str) public
    {
        myString = _str;
    }

    function reverseStrMain() public view returns (string memory)
    {
        return reverseStr(myString);
    }

    function reverseStrArg(string memory strIn) public view returns (string memory)
    {
        return reverseStr(strIn);
    }

    function reverseStr(string memory strIn) internal pure returns (string memory)
    {
        bytes memory _myString = bytes(strIn);
        uint256 count = _myString.length;
        uint256 end   = count - 1;
        bytes memory reveresedStr = new bytes(count);

        for (uint256 begin = 0; begin < count; begin++)
            reveresedStr[begin] = _myString[end--];

        return string(reveresedStr);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.