Arduino:
Capitalizing a string
How to:
Arduino, primarily known for interacting with hardware, also includes basic string manipulation capabilities through its String
object. However, it lacks a direct capitalize
function seen in higher-level languages. Thus, we implement capitalization by iterating over a string and applying case transformations.
Here’s a basic example without using third-party libraries:
String capitalizeString(String input) {
if (input.length() == 0) {
return ""; // Return an empty string if input is empty
}
input.toLowerCase(); // Convert the entire string to lowercase first
input.setCharAt(0, input.charAt(0) - 32); // Capitalize the first character
// Capitalize letters that follow a space
for (int i = 1; i < input.length(); i++) {
if (input.charAt(i - 1) == ' ') {
input.setCharAt(i, input.charAt(i) - 32);
}
}
return input;
}
void setup() {
Serial.begin(9600);
String testStr = "hello arduino world";
String capitalizedStr = capitalizeString(testStr);
Serial.println(capitalizedStr); // Output: "Hello Arduino World"
}
void loop() {
// Empty loop
}
This code snippet defines a capitalizeString
function that first converts the entire string to lowercase to standardize its case. It then capitalizes the first character and any character that follows a space, effectively capitalizing each word in the input string. Note that this rudimentary implementation assumes ASCII character encoding and may need adjustments for full Unicode support.
Currently, there aren’t widely adopted third-party libraries specifically for string manipulation in the Arduino ecosystem, mainly due to its focus on hardware interaction and efficiency. However, the provided example is a straightforward way to achieve string capitalization within Arduino’s programming environment.