Trying to use char array instead of Strings but have a problem

Hi, I have created working project using Strings. And since there are so many warnings not to use them, I have decided to rewrite code using char arrays. I have experienced some problems.
For example, what I would like to do is to read string from Serial monitor, store it in char array and then compare it with pre-defined fixed string. I have written simple code just to test and it doesn’t work as wanted.

Could someone please just re-write this code so that it will work and I will start building on the top of this.

This is my starting code:

char text[4];
char textOriginal[] = "abc";

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) {
    for (int i=0; i<=3; i++) {
      text[i]=Serial.read();
    }
    if (strcmp(text, textOriginal) == 0) {
      Serial.println("Strings are equal");
    }
    else {
      Serial.println("Strings are not equal");
    }
  }
}

Thank you very much!

in this code you know you have at least 1 byte available on the Serial line but you read 4

if (Serial.available() > 0) {
    for (int i=0; i<=3; i++) {
      text[i]=Serial.read();
    }

so you might get garbage if only 2 bytes had made it to the Serial buffer when you start reading.

Also you don’t add a trailing NULL char that is needed for cStrings to work. the ‘\0’ character is what marks the end of the cString (so you need to increase the size of your buffer by 1 to accommodate the trailing NULL char)

have a look at Serial Input Basics

You can leverage the functions in stdlib.h and string.h