3
string largestOddNumber(string num) {4
// Length of the given string6
// We initialize an empty string for the result8
// We start searching digits from the very right to left because we want to9
// find the first odd digit Which will be the last digit of our biggest odd11
for (int i = len - 1; i >= 0; i--) {12
// Here we just convert char to an integer in C++13
// We can also do the reverse operation by adding '0' to an int to get15
int isOdd = num[i] - '0';16
// We check if the current digit is odd, if so this is the position we19
// Since we have found the correct spot, let's create our result string20
// We can basically extract the part starting from 0th index to right21
// most odd digit's index Like this: 0123456 -> indices 1246878 ->23
// ^....^ -> The part we extracted [0 to 5]24
res = num.substr(0, i + 1); // i+1 is length of substring25
// Because we know this would be the largest substring as we are26
// starting from last We can terminate the loop and return the result