//Student name: Ip Wing Yi
//Class: EG524403-1A
//Student number:230317896
//following is part 1
char buf[100] = {0}; // this is an array, total 100 nos.
char inChar;
bool stringComplete = false;//two situation:true or fales
int i = 0;
//following is part 2
// declare/announce the function
char ConvertString(char);//will have this function in use
//following is part 3
void setup()
{
// Initialize serial and wait for port to open
Serial.begin(115200);
while (!Serial)
{
// Wait for serial port to connect, needed for native USB port only
}
}
void loop()//row 25-41 is a loop
{
while (Serial.available())
{
inChar = (char)Serial.read(); // while loop's 1st step
if (inChar == '\n') // change '\n' to '~' when using Tinkercad
{
buf[i++] = inChar; // last character is newline
buf[i] = 0; // string array should be terminated with a zero
stringComplete = true;//as stringcomplete==true,will go row 43
}
else
{
// some actions after received
inChar = ConvertString(inChar);
buf[i++] = inChar;
}
}
if (stringComplete)// only when received /n (enter), stringcomplete=true
{
Serial.print(buf); // the printing of string will be stopped when zero is reached
stringComplete = false;//when print
i = 0;
}
}
// body of the function
char ConvertString(char x)
{
if (isUpperCase(x))
return (toLowerCase(x));
else if (isLowerCase(x))
return (toUpperCase(x));
else
return (x);
}