Bird
0
0
DSA Cprogramming~10 mins

String to Integer atoi in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to skip leading spaces in the input string.

DSA C
while (str[i] == [1]) {
    i++;
}
Drag options to blanks, or click blank then click option'
A' '
B'\t'
C'\n'
D'0'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '\t' instead of space.
Skipping newline characters instead of spaces.
2fill in blank
medium

Complete the code to check if the current character is a minus sign.

DSA C
if (str[i] == [1]) {
    sign = -1;
    i++;
}
Drag options to blanks, or click blank then click option'
A'0'
B'+'
C'-'
D' '
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for '+' instead of '-'.
Not incrementing the index after sign detection.
3fill in blank
hard

Fix the error in the loop condition to process digits only.

DSA C
while (str[i] [1] '0' && str[i] <= '9') {
    result = result * 10 + (str[i] - '0');
    i++;
}
Drag options to blanks, or click blank then click option'
A<=
B>=
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which only checks one character.
Using '!=' which allows non-digit characters.
4fill in blank
hard

Fill both blanks to correctly handle integer overflow before multiplying and adding.

DSA C
if (result > [1] || (result == [2] && (str[i] - '0') > 7)) {
    return sign == 1 ? INT_MAX : INT_MIN;
}
Drag options to blanks, or click blank then click option'
AINT_MAX / 10
BINT_MAX % 10
CINT_MIN / 10
DINT_MIN % 10
Attempts:
3 left
💡 Hint
Common Mistakes
Using INT_MIN values instead of INT_MAX for overflow check.
Using INT_MAX % 10 for the equality check instead of INT_MAX / 10.
Not checking the next digit properly.
5fill in blank
hard

Fill all three blanks to complete the atoi function's main loop and return statement.

DSA C
while (str[i] >= '0' && str[i] <= '9') {
    if (result > [1] || (result == [2] && (str[i] - '0') > [3])) {
        return sign == 1 ? INT_MAX : INT_MIN;
    }
    result = result * 10 + (str[i] - '0');
    i++;
}
return result * sign;
Drag options to blanks, or click blank then click option'
AINT_MAX / 10
BINT_MAX % 10
C7
D8
Attempts:
3 left
💡 Hint
Common Mistakes
Using different values for the first two blanks.
Using 8 instead of 7 for the last digit check.