// https://www.robotronix.co.il
// String Programming Exercises - Robotics and Microcontrollers
// Learn C Strings, Arduino and ESP32 Programming
#include <string.h> // Include C string functions
char buf[80]; // Buffer for string operations
char source[40]; // Buffer for the source string
void setup() { // Run once at startup
Serial.begin(115200); // Start serial communication
Serial.println("---------------------------------------");
// Example 1: strcpy - Copy a string
strcpy(source, "http://www.robotronix.co.il"); // Store website address
strcpy(buf, source); // Copy source into buf
Serial.print("1 - Copied string: "); // Print label
Serial.println(buf); // Print copied string
// Example 2: strcat - Concatenate two strings
strcat(buf, "/product-category/robotics/"); // Add text to the string
Serial.print("2 - Combined string: "); // Print label
Serial.println(buf); // Print combined string
// Example 3: strlen - Get string length
size_t length = strlen(buf); // Count characters in the string
Serial.print("3 - String length: "); // Print label
Serial.println(length); // Print string length
// Example 4: strstr - Find a substring
if (strstr(buf, ".co.il") != NULL) { // Search for ".co.il"
Serial.println("4 - Substring .co.il found");
} else { // Substring does not exist
Serial.println("4 - Substring not found");
}
// Example 5: strcmp - Compare two strings
const char* key = "12345"; // Define the correct password
char userInputKey[20]; // Store the entered password
strcpy(userInputKey, "24923"); // Set example password
int comparison = strcmp(key, userInputKey); // Compare passwords
if (comparison == 0) { // Strings are identical
Serial.println("5 - Password correct");
} else { // Strings are different
Serial.println("5 - Wrong password");
}
}
void loop() { // Run continuously
// Nothing to execute
}