Suppose, we are given a binary tree and also two specific nodes x and y. We have to find out the lowest common ancestor of the two nodes from the binary tree. The lowest common ancestor in a binary tree is the lowest node of which both the nodes x and y are descendants. A particular node can also be a descendant of itself. We have to find the node and return it as an output.The node structure of the tree is like below −TreeNode: data: left: right: parent: We have to utilize ... Read More
A problem faced by several people using Arduino, or any microcontroller board for that matter, is that you may forget to start the Serial Monitor before programming the board, and miss some print statements by the time you open the Serial Monitor.One way to overcome this is to start the sketch only after an input is received from the user, via the Serial Monitor. This will ensure that you don’t miss any prints on the Serial Monitor because of the delay in starting the Serial Monitor.Examplevoid setup() { // put your setup code here, to run once: Serial.begin(9600); ... Read More
It may happen that a string may change length dynamically during the execution of a program.If you want to ensure that there is always enough memory available for your string, you can reserve some memory using the reserve() function.SyntaxString1.reserve(n_bytes);Where String1 is the string for which you are reserving memory, and n_bytes (unsigned int) is the number of bytes to be reserved in the memory.ExampleString s1 = "Hello"; void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); s1.reserve(20); s1 = s1+" World!"; Serial.println(s1); s1 = s1+" I'm now trying ... Read More
Arduino has an inbuilt compareTo() function that helps compare which string comes before another. Very crudely, you can think of it as this: if you are given two strings, which one will come first in a dictionary.SyntaxString1.compareTo(String2)Where String1 and String2 are the two strings to be compared. This function returns an integer. Here’s the interpretation of the value of the integer −Negative − String1 comes before String20 − String1 and String2 are equalPositive − String2 comes before String1Please note that this function is case sensitive. So ‘A’ comes before ‘a’ and ‘B’ comes before ‘a’. But ‘a’ comes before ‘b’. Also, ... Read More
The Serial Monitor of Arduino has a text box at the top, through which, users can send in text to the Arduino board.The text can be read by Serial.read(). Also, the Serial.available() function can be used to check if there is any data to read. It returns the number of characters or bytes available for reading, i.e., the number of bytes stored in the serial receive buffer.ExampleUsing these functions, let’s create a simple echo program for Arduino. The code for the same can be found below −void setup() { // put your setup code here, to run once: ... Read More
The remove function in Arduino helps you remove one or more characters from within a string.SyntaxmyString.remove(index, count)Here, index refers to the index from where removal has to start. Note that indexing in Arduino starts with 0. Thus, within string "Hello", 'H' is at index 0, 'e' is at index 1, and so on.The count argument is optional, and it specifies the number of characters to remove. If you don’t specify the count, then all characters starting from index till the end of the string will be removed. If you specify count as say, 3, then 3 characters starting from index position will ... Read More
In case you don’t want to overwrite a string, but just change a character at a specific position, Arduino provides the setCharAt() function exactly for that.SyntaxString1.setCharAt(ind, new_char);String 1 is the string to modify. ind is the index where the character needs to be set. new_char is the value of the new character that needs to be set.This function returns nothing, and modifies the string in place.ExampleThe following example illustrates the use of this function.void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); String string1 = "Hello World!"; Serial.println(string1); string1.setCharAt(4, ... Read More
In this article, we will walkthrough the example code in Arduino, which helps generate the ASCII table in the Serial Monitor output. For your reference, this is what the ASCII table looks like − http://www.asciitable.com/It contains the character, followed by its ASCII code in decimal, hexadecimal, and sometimes, even octal and binary representations. In this example, we will print out all these representations for printable ASCII characters. Remember that the first printable ASCII character starts from number 33, and the printable characters go on up to number 126. Since we will print out the ASCII table on the Serial Monitor, ... Read More
Switch case in Arduino is just like the switch case in C language. For those who are unaware of switch case, it is a more compact way of writing multiple if statements, when they concern the value of a variable.Syntaxswitch (var) { case value1: // statements for value1 break; case value2: // statements for value2 break; . . . default: // statements for default value break; }var is the variable whose different values we are checking. If its value ... Read More
Just like analogRead() helps you to read analog voltages, digitalRead() helps you read digital levels.SyntaxdigitalRead(pin)When pin is the number of the pin whose digital level you wish to read. This function returns either HIGH or LOW.Please note that if the pin you are wishing to read is not connected to anything, it can return either HIGH or LOW, and this value can change with time and noise. Also, in general, analog pins can be used for digitalRead(). As stated in Arduino’s documentation, the following are the exceptions −Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins, which can only ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP