// Name: Choi Ka Ho
// No.: 240021155
// Class: EG114403/1D
// Program: Ex5_Array.ino
char buf[100] = {0}; //Enter a maximum of 100 bytes
char inChar;
bool stringComplete = false;
int i = 0;
char ConvertChar(char);
void setup()
{
Serial.begin(115200);
}
void loop() {
int num;
while (Serial.available())
{
// (char)Serial.read(); The previously defined inChar
inChar = Serial.read(); //can not be proved here
if (inChar == '\n')
{
buf[i++] = inChar;
buf[i] = 0;
stringComplete = true;
}
else
{
inChar = ConvertChar(inChar);
buf[i++] = inChar;
}
}
if (stringComplete) { //Nested loop
if ((i == 2) && (isDigit(buf[0]))) { //Control units digit
num = buf[0] - '0';
PrintStars(num); //Input star'*'
} else {
if ((i == 3) && (isDigit(buf[0])) && (isDigit(buf[1]))) {
num = (((buf[0] - '0' ) * 10) + (buf[1] - '0'));
PrintStars(num); //Control two digit
} else {
Serial.println("Incorrect input"); //If it's another input
Serial.println();
}
}
stringComplete = false; //After each input
i = 0; //Restore basic step
num = 0;
}
}
void PrintStars(int n) {
for (int j = 0; j < n; j++) {
Serial.print('*');
if ((j + 1) % 10 == 0) { //every 10 stars, go to a new line
Serial.println();
}
}
Serial.println();
}
char ConvertChar(char x)
{
// if((x >= 65) && (x <= 90))
if ((x >= 'A') && (x <= 'Z'))
return (x + 32);
else if (isLowerCase(x))
return (toUpperCase(x));
else
return (x);
}