Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert the first character of the string to an integer digit.
DSA C++
int digit = str[0] - '[1]';
Attempts:
3 left
💡 Hint
Common Mistakes
Using '1' instead of '0' causes wrong digit conversion.
Using character 'a' causes incorrect subtraction.
✗ Incorrect
Subtracting the character '0' from a digit character converts it to its integer value.
2fill in blank
mediumComplete the code to skip leading spaces in the string.
DSA C++
while (str[i] == '[1]') i++;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '\n' skips newlines, not spaces.
Using '0' skips digits, not spaces.
✗ Incorrect
Leading spaces are represented by the space character ' '.
3fill in blank
hardFix the error in checking if the current character is a digit.
DSA C++
if (str[i] >= '[1]' && str[i] <= '9') {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'a' checks for letters, not digits.
Using ' ' checks for space, not digits.
✗ Incorrect
Digits in ASCII range from '0' to '9'.
4fill in blank
hardFill both blanks to correctly update the result while parsing digits.
DSA C++
result = result [1] 10 [2] (str[i] - '0');
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong result.
Using '/' instead of '*' breaks number building.
✗ Incorrect
Multiply result by 10 and add the new digit value.
5fill in blank
hardFill all three blanks to handle sign and return the final integer.
DSA C++
int sign = [1]; if (str[i] == '-') { sign = [2]; i++; } return result [3] sign;
Attempts:
3 left
💡 Hint
Common Mistakes
Adding sign instead of multiplying causes wrong output.
Not updating sign when '-' found causes incorrect sign.
✗ Incorrect
Sign is 1 by default, -1 if negative sign found, multiply result by sign.
