Bird
Raised Fist0
Arduinoprogramming~20 mins

String parsing from serial input in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Serial String Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Arduino serial parsing code?

Consider this Arduino code snippet that reads a serial input string and extracts a number:

String input = "TEMP:25;";
int value = 0;

int index = input.indexOf(':');
if (index != -1) {
  String numberPart = input.substring(index + 1, input.indexOf(';'));
  value = numberPart.toInt();
}
Serial.println(value);

What will be printed on the Serial Monitor?

Arduino
String input = "TEMP:25;";
int value = 0;

int index = input.indexOf(':');
if (index != -1) {
  String numberPart = input.substring(index + 1, input.indexOf(';'));
  value = numberPart.toInt();
}
Serial.println(value);
A25
B0
CTEMP
DSyntax error
Attempts:
2 left
💡 Hint

Look at how substring and toInt() work on the extracted part.

🧠 Conceptual
intermediate
1:30remaining
Which function reads incoming serial data as a string until a newline?

In Arduino, which function reads characters from the serial buffer until it encounters a newline character and returns the result as a String?

ASerial.available()
BSerial.read()
CSerial.readStringUntil('\n')
DSerial.parseInt()
Attempts:
2 left
💡 Hint

Think about reading until a specific character.

🔧 Debug
advanced
2:00remaining
What error does this Arduino code cause when parsing serial input?

Analyze this Arduino code snippet:

String data = Serial.readStringUntil(';');
int val = data.toInt();
Serial.println(val);

If the serial input is "123abc;", what happens?

Arduino
String data = Serial.readStringUntil(';');
int val = data.toInt();
Serial.println(val);
APrints 123
BPrints 0
CCauses a runtime error
DPrints 123abc
Attempts:
2 left
💡 Hint

Check how toInt() handles strings with trailing letters.

📝 Syntax
advanced
1:30remaining
Which option causes a syntax error in Arduino string parsing?

Which of these Arduino code snippets will cause a syntax error?

1) String s = Serial.readStringUntil(';');
2) int n = s.toInt();
3) String part = s.substring(2, 5);
4) int x = s.substring(1, 3).toInt()
AOption 3 (substring parameters invalid)
BOption 1 (wrong function name)
COption 2 (toInt() not valid)
DOption 4 (missing semicolon)
Attempts:
2 left
💡 Hint

Look carefully for missing punctuation.

🚀 Application
expert
2:30remaining
How many items are in the parsed array after splitting serial input?

Given this Arduino code:

String input = "CMD,100,ON,45";
int count = 0;
int start = 0;
int commaIndex = -1;

while ((commaIndex = input.indexOf(',', start)) != -1) {
  String part = input.substring(start, commaIndex);
  count++;
  start = commaIndex + 1;
}
count++;
Serial.println(count);

What number will be printed?

Arduino
String input = "CMD,100,ON,45";
int count = 0;
int start = 0;
int commaIndex = -1;

while ((commaIndex = input.indexOf(',', start)) != -1) {
  String part = input.substring(start, commaIndex);
  count++;
  start = commaIndex + 1;
}
count++;
Serial.println(count);
A3
B4
C5
D2
Attempts:
2 left
💡 Hint

Count how many commas split the string and add one.

Practice

(1/5)
1. What does Serial.readStringUntil('\n') do in Arduino programming?
easy
A. Reads characters from serial input until a newline character is found
B. Sends a newline character over serial
C. Clears the serial buffer
D. Reads only one character from serial input

Solution

  1. Step 1: Understand the function purpose

    Serial.readStringUntil('\n') reads characters from the serial buffer until it finds the newline character '\n'.
  2. Step 2: Compare with other options

    It does not send data, clear buffer, or read only one character; it reads a full line until newline.
  3. Final Answer:

    Reads characters from serial input until a newline character is found -> Option A
  4. Quick Check:

    Read until '\n' means read full line [OK]
Hint: Remember '\n' means newline, so it reads until line ends [OK]
Common Mistakes:
  • Thinking it reads only one character
  • Confusing reading with sending data
  • Assuming it clears the buffer
2. Which of the following is the correct way to read a full line from serial input in Arduino?
easy
A. Serial.read()
B. Serial.available()
C. Serial.readStringUntil('\n')
D. Serial.write()

Solution

  1. Step 1: Identify function to read full line

    Serial.readStringUntil('\n') reads characters until newline, capturing a full line.
  2. Step 2: Understand other functions

    Serial.read() reads one byte, Serial.write() sends data, and Serial.available() checks bytes available.
  3. Final Answer:

    Serial.readStringUntil('\n') -> Option C
  4. Quick Check:

    Read full line = readStringUntil('\n') [OK]
Hint: Use readStringUntil('\n') to get whole line input [OK]
Common Mistakes:
  • Using Serial.read() to get full line
  • Confusing read and write functions
  • Using Serial.available() to read data
3. What will be the output of this Arduino code if the serial input is "TEMP:25\n"?
String input = Serial.readStringUntil('\n');
int value = input.substring(5).toInt();
Serial.println(value);
medium
A. 25
B. TEMP:25
C. 0
D. Error

Solution

  1. Step 1: Read the input string

    The input string is "TEMP:25" (newline removed by readStringUntil).
  2. Step 2: Extract substring and convert to integer

    input.substring(5) takes characters from index 5 onward, which is "25". Then toInt() converts "25" to integer 25.
  3. Final Answer:

    25 -> Option A
  4. Quick Check:

    Substring from 5 = "25", toInt() = 25 [OK]
Hint: Use substring index to isolate number, then toInt() converts [OK]
Common Mistakes:
  • Forgetting substring index starts at 0
  • Expecting full string printed
  • Not converting substring to int
4. Identify the error in this Arduino code snippet for parsing serial input:
String data = Serial.readStringUntil('\n');
int num = data.toInt();
if(num = 10) {
  Serial.println("Number is 10");
}
medium
A. toInt() cannot convert string to int
B. Missing semicolon after Serial.println
C. Serial.readStringUntil('\n') does not read input
D. Using assignment '=' instead of comparison '==' in if condition

Solution

  1. Step 1: Check if condition syntax

    The condition if(num = 10) uses assignment '=' instead of comparison '=='. This causes a bug.
  2. Step 2: Verify other parts

    Semicolon is present, toInt() works correctly, and readStringUntil('\n') reads input properly.
  3. Final Answer:

    Using assignment '=' instead of comparison '==' in if condition -> Option D
  4. Quick Check:

    Use '==' to compare values in if [OK]
Hint: Use '==' for comparison, not '=' [OK]
Common Mistakes:
  • Using '=' instead of '==' in conditions
  • Assuming toInt() fails on valid numbers
  • Thinking readStringUntil doesn't read input
5. You receive serial input in the format "CMD:VALUE\n", for example "LED:1\n". How can you parse the command and value separately in Arduino?
hard
A. Use String input = Serial.readStringUntil('\n'); int val = input.substring(0,3).toInt(); String cmd = input.substring(4);
B. Use String input = Serial.readStringUntil('\n'); String cmd = input.substring(0,3); int val = input.substring(4).toInt();
C. Use String input = Serial.readString(); String cmd = input.split(':')[0]; int val = input.split(':')[1].toInt();
D. Use String input = Serial.read(); String cmd = input.substring(0,3); int val = input.substring(4).toInt();

Solution

  1. Step 1: Read full line input

    Serial.readStringUntil('\n') reads the entire line including command and value.
  2. Step 2: Extract command and value

    input.substring(0,3) extracts the first 3 characters as command (e.g., "LED"), and input.substring(4).toInt() converts the value part after ':' to integer.
  3. Final Answer:

    Use String input = Serial.readStringUntil('\n'); String cmd = input.substring(0,3); int val = input.substring(4).toInt(); -> Option B
  4. Quick Check:

    Substring command and value parsing works [OK]
Hint: Use substring with indexes to split command and value [OK]
Common Mistakes:
  • Using read() instead of readStringUntil
  • Trying to split string with split() which is not available
  • Mixing up substring indexes for command and value