Bird
Raised Fist0
DSA Cprogramming~10 mins

String to Integer atoi in DSA C - Interactive Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the atoi function do in C programming?
easy
A. Checks if a string contains only digits
B. Converts a string representing a number into an integer
C. Calculates the length of a string
D. Converts an integer into a string

Solution

  1. Step 1: Understand the purpose of atoi

    The atoi function stands for ASCII to integer, which means it converts a string of digits into an integer value.
  2. Step 2: Compare options with definition

    Only Converts a string representing a number into an integer correctly describes this conversion from string to integer.
  3. Final Answer:

    Converts a string representing a number into an integer -> Option B
  4. Quick Check:

    atoi = string to integer conversion [OK]
Hint: Remember atoi means ASCII to integer conversion [OK]
Common Mistakes:
  • Confusing atoi with itoa (integer to ASCII)
  • Thinking atoi calculates string length
  • Assuming atoi validates string digits
2. Which of the following is the correct syntax to use atoi in C?
easy
A. int num = atoi(char *str);
B. int num = atoi();
C. int num = atoi(int str);
D. int num = atoi(str);

Solution

  1. Step 1: Check function parameter type

    The atoi function takes a single argument of type const char *, which is a string pointer.
  2. Step 2: Validate correct syntax

    int num = atoi(str); correctly shows calling atoi with a string variable str without specifying type in call.
  3. Final Answer:

    int num = atoi(str); -> Option D
  4. Quick Check:

    atoi takes string variable as argument [OK]
Hint: Call atoi with just the string variable name [OK]
Common Mistakes:
  • Including type in function call parentheses
  • Calling atoi without arguments
  • Passing integer instead of string
3. What will be the output of the following C code?
char str[] = "-123abc";
int val = atoi(str);
printf("%d", val);
medium
A. -123
B. 0
C. 123
D. Error

Solution

  1. Step 1: Analyze atoi behavior with mixed string

    atoi converts initial numeric part including sign until non-digit character. Here, "-123" is converted, stops at 'a'.
  2. Step 2: Determine output value

    So, val becomes -123 and printf prints -123.
  3. Final Answer:

    -123 -> Option A
  4. Quick Check:

    atoi stops at first non-digit, returns -123 [OK]
Hint: atoi reads number until first non-digit character [OK]
Common Mistakes:
  • Expecting atoi to ignore leading sign
  • Thinking atoi returns 0 on trailing letters
  • Assuming runtime error due to letters
4. Identify the error in this code snippet using atoi:
char *str = NULL;
int num = atoi(str);
printf("%d", num);
medium
A. Passing NULL pointer to atoi causes undefined behavior
B. Missing header file
C. atoi cannot convert strings with digits
D. No error, code runs fine

Solution

  1. Step 1: Check argument passed to atoi

    The code passes a NULL pointer to atoi, which expects a valid string pointer.
  2. Step 2: Understand consequences of NULL argument

    Passing NULL leads to undefined behavior, often causing a crash or runtime error.
  3. Final Answer:

    Passing NULL pointer to atoi causes undefined behavior -> Option A
  4. Quick Check:

    atoi needs valid string pointer, NULL causes error [OK]
Hint: Never pass NULL to atoi; always check string pointer [OK]
Common Mistakes:
  • Ignoring NULL pointer checks
  • Assuming atoi handles NULL safely
  • Confusing missing header with runtime error
5. You want to convert a string to integer but also detect if the string contains invalid characters after the number. Which approach is best in C?
hard
A. Use atoi and then check the rest of the string manually
B. Use atoi and assume input is always valid
C. Use strtol which provides end pointer to check leftover characters
D. Use printf to convert string to integer

Solution

  1. Step 1: Understand limitation of atoi

    atoi converts initial number but does not provide info about leftover invalid characters.
  2. Step 2: Use strtol for better control

    strtol converts string to long and returns pointer to first invalid character, allowing validation.
  3. Step 3: Choose best approach

    Use strtol which provides end pointer to check leftover characters uses strtol with end pointer to detect invalid trailing characters safely.
  4. Final Answer:

    Use strtol which provides end pointer to check leftover characters -> Option C
  5. Quick Check:

    strtol + end pointer = safe string to int with validation [OK]
Hint: Use strtol with end pointer to detect invalid chars [OK]
Common Mistakes:
  • Relying only on atoi for input validation
  • Ignoring leftover characters after number
  • Using printf incorrectly for conversion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard