Coding doubt in serial monitor

Can we compare two strings in this way, is it valid?
My code gets executed till this statement, I mean if I type in the serial monitor "Read 21", the variable instr gets the value "Read 21" and the variable mainpart gets the value "Read"
The next step I need to get done is if the variable mainpart gets the value Read, then I need to read data from the specified location at EEPROM address. If it is Write, then I have to write a value to the corresponding EEPROM location, for this I have come up with a code in the last few lines which doesn't work, any mistakes you can point out?

No, but you can compare two Strings like that.

For strings, you should use one of the flavours of strcmp.

I have editted the last if.. else statement accordingly.

// C++ code
#include<EEPROM.h>
#include<string.h>
char instr[10], mainpart[10], c;
int addr, data = 0, i, j, n;
void setup()
{
  Serial.begin(9600);
  Serial.print("The formats used are: \n Read 201 \n Write 21 93\n");
  for ( addr = 0; addr < 255; addr++, data++)
  {
    EEPROM.write(addr, data);
  }
}

void loop()
{
  while (Serial.available())
  {
    instr[i] = c = Serial.read();
    Serial.print(instr[i]);
    i++;
  }
  if ( c == '\n' )
  {
    for ( i = 0, j = 0; instr[i] != ' '; i++, j++ )
    {
      mainpart[j] = instr[i];
    }
    mainpart[j] = 0;
    Serial.println(mainpart);
    c = i = 0;
  }
  if (strcmp(mainpart, "Read") == 0)
  {
    Serial.println(EEPROM.read(addr), DEC);
  }
  else if (strcmp(mainpart, "Write") == 0)
  {
    EEPROM.write(addr, data);
    Serial.println("Specified location is updated");
  }
  else
  {
    Serial.println("Invalid command");
  }
}

When I type "Read 21", the serial monitor should display the value of the variable instr and mainpart which is "Read 21" and "Read" respectively.
The serial monitor must also show the data stored in location "21" of EEPROM.

The above outputs are shown

Where did I commit a mistake? Please help me out!

The mistake I have done here is, if I type "Read 21", I must store Read in mainpart and 21 in some other variable,(which I haven't shown in my code) if I use Serial.parseInt() I can store 21 in another variable.
But the problem is if I type "Write 20 92", I will have to store 20 and 92 in 2 different variables, if I use Serial.parseInt() it will look for the first numeric value, it retrieves 20(which can be stored in some variable), the parseInt function would discard all the characters before 20 that is "Write " and retain " 92" in the serial receive buffer after retrieving the first integer data "20". How can I retrieve the integer 92 left out in the serial buffer and save it in a variable?

If the Serial.readStringUnit(' '); times out (no ' ' found in one second) it returns an empty String. The do {} while(); repeats the Serial.readStringUnit(' '); if it returned an empty String.

do {} while(); is like while () {} except it does the test AFTER the body is executed, not before.

The if (strcmp(mainpart, "Read") == 0) should be INSIDE the if ( c == '\n' ) statement. You put it outside. Once instr starts with "Read ", every time through loop() it displays the contents of an EEPROM address. Until instr starts with "Read " or "Write " it will display "Invalid command" every time through loop().

Sir, here the variable command is declared as a string, this means command is an array of characters?

No, it is defined as a String.

No,it is an instance of the String class.
It contains an array of char, but you don't need to know that.

That means it is not an array of characters and as in an array we can access each element by using array[pos], hence this is not possible when we declare it as a string?

See: String() - Arduino Reference

#include<EEPROM.h>
#include<string.h>
char instr[10], mainpart[10], c;
int addr, data = 0, i, j, n;
void setup()
{
  Serial.begin(9600);
  Serial.print("The formats used are: \n Read 201 \n Write 21 93\n");
  for ( addr = 0; addr < 255; addr++, data++)
  {
    EEPROM.write(addr, data);
  }
}

void loop()
{
  Serial.setTimeout(20000);
  while (Serial.available())
  {
    instr[i] = c = Serial.read();
    Serial.print(instr[i]);
    i++;
  }
  if ( c == '\n' )
  {
    for ( i = 0, j = 0; instr[i] != ' '; i++, j++ )
    {
      mainpart[j] = instr[i];
    }
    mainpart[j] = 0;
    Serial.println(mainpart);
    c = i = 0;
    if (strcmp(mainpart, "Read") == 0)
    {
      addr = Serial.parseInt();
      Serial.print("The data stored in location %d is : ",addr);
      Serial.println(EEPROM.read(addr), DEC);
    }
    else if (strcmp(mainpart, "Write") == 0)
    {
      addr = Serial.parseInt();
      data = Serial.parseInt();
      EEPROM.write(addr, data);
      Serial.println("Specified location is updated");
    }
    else
    {
      Serial.println("Invalid command");
    }
  }

}

The output displayed on serial monitor is

The contents of the variable mainpart is also not displayed, Please help me out!!

Your sketch won't compile for an Arduino UNO because:
Serial.print("The data stored in location %d is : ", addr);

A big part of the problem comes from reading all of the characters up to '\n' into 'instr'. That means Serial no longer has your numbers and Serial.parseInt() won't work.

Changing if ( c == '\n' ) into if ( c == ' ' )will help.

Here is a version of your sketch with a few edits to get it working:

#include<EEPROM.h>

char instr[10];
int instr_index = 0;

void setup()
{
  Serial.begin(115200);
  delay(200);

  Serial.print("The formats used are: \n Read 201 \n Write 21 93\n");
  for (int addr = 0; addr < 255; addr++)
  {
    EEPROM.write(addr, 255 - addr);
  }
}

void loop()
{
  while (Serial.available())
  {
    char c = Serial.read();
    
    if (c != ' ')
    {
      instr[instr_index++] = c; // Not a space yet, so store it
    }
    else  // Act on the first space
    {
      instr[instr_index] = '\0'; // Add termionator
      Serial.println(instr);

      if (strcmp(instr, "Read") == 0)
      {
        int addr = Serial.parseInt();
        // Serial.print("The data stored in location %d is : ", addr);
        // Serial.println(EEPROM.read(addr), DEC);

        Serial.print("The data stored in location ");
        Serial.println(addr, DEC);
        Serial.print(" is ");
        Serial.println(EEPROM.read(addr), DEC);
      }
      else if (strcmp(instr, "Write") == 0)
      {
        int addr = Serial.parseInt();
        int data = Serial.parseInt();
        EEPROM.write(addr, data);
        Serial.println("Specified location is updated");
      }
      else
      {
        Serial.println("Invalid command");
      }

      // Throw away any trailing characters in the buffer
      delay(1000);
      while (Serial.available())
        Serial.read();

     instr_index = 0; // Start over at the beginning
    }
  }
}

I do understand my mistake, I corrected the code by changing the condition c == '\n' to c != ' ' and the print statements on the serial monitor were taken care of

#include<EEPROM.h>
#include<string.h>
char instr[10], c;
int addr, data = 0, j, n, i = 0;
void setup()
{
  Serial.begin(9600);
  Serial.print("The formats used are: \n Read 201 \n Write 21 93\n");
  for ( addr = 0; addr < 255; addr++, data++)
  {
    EEPROM.write(addr, data);
  }
}

void loop()
{
  Serial.setTimeout(20000);
  while (Serial.available())
  {
    c = Serial.read();
    if ( c != ' ' )
    {
      instr[i] = c;
    }
    else
    {
      instr[i] = '\0';
      Serial.println(instr);
    }
    if (strcmp(instr, "Read") == 0)
    {
      addr = Serial.parseInt();
      Serial.print("The data stored in location ");
      Serial.println(addr, DEC);
      Serial.print("is");
      Serial.println(EEPROM.read(addr), DEC);
    }
    else if (strcmp(instr, "Write") == 0)
    {
      addr = Serial.parseInt();
      data = Serial.parseInt();
      EEPROM.write(addr, data);
      Serial.print("The data stored in location ");
      Serial.println(addr, DEC);
      Serial.print("is");
      Serial.println(EEPROM.read(addr), DEC);
    }
    else
    {
      Serial.println("Invalid command");
    }
  }
}

When I type Read 21 on the serial monitor, the output is still wrong?

Sir, why have you changed the baud rate?
Why is delay required?

Why are you placing a while loop here? The same loop was already given at the beginning.
What is the use of delay(1000)

The mistake I have committed here was that I was not updating the increment variable

#include<EEPROM.h>
#include<string.h>
char instr[10], c;
int addr, data = 0, j, n, i = 0;
void setup()
{
  Serial.begin(9600);
  Serial.print("The formats used are: \n Read 201 \n Write 21 93\n");
  for ( addr = 0; addr < 255; addr++, data++)
  {
    EEPROM.write(addr, data);
  }
}

void loop()
{
  Serial.setTimeout(20000);
  while (Serial.available())
  {
    c = Serial.read();
    if ( c != ' ' )
    {
      instr[i] = c;
      i++;
    }
    else
    {
      instr[i] = '\0';
      Serial.println(instr);
      if (strcmp(instr, "Read") == 0)
      {
        addr = Serial.parseInt();
        Serial.print("The data stored in location ");
        Serial.println(addr, DEC);
        Serial.print("is");
        Serial.println(EEPROM.read(addr), DEC);
      }
      else if (strcmp(instr, "Write") == 0)
      {
        addr = Serial.parseInt();
        data = Serial.parseInt();
        EEPROM.write(addr, data);
        Serial.print("The data stored in location ");
        Serial.println(addr, DEC);
        Serial.print("is");
        Serial.println(EEPROM.read(addr), DEC);
      }
      else
      {
        Serial.println("Invalid command");
      }
      i = 0;
    }
  }
}

When I type "Read 21" in the serial monitor, the variable instr gets stored with Read, but the data stored in address location 21 in EEPROM is not displayed, Can anyone help me out, please!!

Usually declaration of variables takes place in setup function or before any function definition, so why is this declaration in loop function?

You forgot to increment 'i'. Try:

    if ( c != ' ' )
    {
      instr[i++] = c;
    }

A variable should have the minimum scope possible.
I'm not clear what you mean by "usually".

Yes, that was the mistake, I figured it out, thankyou for your patience and time, I am just a beginner in coding, all your feedbacks were quite helpful. I also thank each and everyone of the Arduino Forum who have come forward to clear my doubts.