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 calculation.
Using character 'a' or '9' leads to incorrect results.
✗ Incorrect
Subtracting the character '0' from a digit character converts it to its integer value.
2fill in blank
mediumComplete the code to skip leading whitespace characters in the string.
DSA C#
while (index < str.Length && str[index] == [1]) { index++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Skipping only tabs or newlines misses spaces.
Using '0' character skips digits incorrectly.
✗ Incorrect
Leading spaces are represented by the space character ' '.
3fill in blank
hardFix the error in the code to correctly check if the current character is a digit.
DSA C#
if (str[index] >= [1] && str[index] <= [2]) { /* process digit */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using letters 'a' or 'z' instead of digits causes wrong checks.
Checking only one boundary leads to errors.
✗ Incorrect
Digits in ASCII range from '0' to '9'. Checking this range ensures the character is a digit.
4fill in blank
hardFill both blanks to correctly update the result integer while parsing digits.
DSA C#
result = result [1] 10 [2] (str[index] - '0');
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or division instead of addition causes wrong results.
Not multiplying by 10 loses place value.
✗ Incorrect
To build the integer, multiply the current result by 10 and add the new digit.
5fill in blank
hardFill all three blanks to handle sign and return the final integer result.
DSA C#
int sign = [1]; if (str[index] == '-') { sign = [2]; index++; } return result [3] sign;
Attempts:
3 left
💡 Hint
Common Mistakes
Adding sign instead of multiplying changes the number incorrectly.
Not updating sign when '-' is found causes wrong sign.
✗ Incorrect
Sign is 1 by default, changes to -1 if '-' found, and final result is multiplied by sign.
