char and string

I have code

void loop() {
  String response = gsmAnswer();
  if (response.indexOf("+CMT:") > 0 ) { // SMS arrived
    if (response.indexOf("admin") > 0) {
      char str[] = response;
      char * nomer;
      nomer = strtok (str, "admin,");
      while (nomer != NULL)
      {
        printf ("%s\n", nomer);
        nomer = strtok (NULL, "admin,");
      }
      return 0;
    } else if (response.indexOf("LED OFF") > 0) {
      digitalWrite(LED_PIN, LOW); // turn off
    }
    delay(1000);
  }

}


String gsmAnswer() {
  String answer;
  while (!gsm.available());
  while (gsm.available()) {
    delay(5);
    if (Serial.available() > 0) {
      char s = (char)gsm.read();
      answer += s;
    }
  }
  return answer;
}

how to add respone in char str ?
I am try it, but not work

char str[] = response;

beryindo:
I have code

Can you tell us what the code is supposed to do ?

You need to post the entire program so others can try to compile it. Also, you are mixing the String class with a C string variable.

Have you been here yet? String() - Arduino Reference

Specifically here:
https://www.arduino.cc/en/Reference/CStr

Or here:
https://www.arduino.cc/en/Reference/StringToCharArray

Don't be afraid of the documentation.

srnet:
Can you tell us what the code is supposed to do ?

Read SMS and get number admin from sms. example : I send sms with content admin,08123456789 and then I read number 08123456789

Change gsmAnswer from using Strings (which are bad for your Arduino anyway) to something that populates a char array.

econjack:
You need to post the entire program so others can try to compile it. Also, you are mixing the String class with a C string variable.

#include <SoftwareSerial.h>
// if You use SoftwareSerial lib, declare object for GSM
#define TIMEOUT    5000
#define LED_PIN    13
#define ON LOW
#define OFF HIGH

SoftwareSerial gsm(6,7); // TX, RX

String nomorHPAdmin = "08123456789";
String bacaSMS = "";
String msg = "";
bool siap = 0;

unsigned int errorCounter;

const int Relay1 = 2;
const int Relay2 = 3;
const int Relay3 = 4;
const int Relay4 = 5;

int StatRelay1;
int StatRelay2;
int StatRelay3;
int StatRelay4;

void setup() {
  pinMode (Relay1 , OUTPUT);
  digitalWrite (Relay1, HIGH);
  pinMode (Relay2 , OUTPUT);
  digitalWrite (Relay2, HIGH);
  pinMode (Relay3 , OUTPUT);
  digitalWrite (Relay3, HIGH);
  pinMode (Relay4 , OUTPUT);
  digitalWrite (Relay4, HIGH);
  StatRelay1 = OFF;
  StatRelay2 = OFF;
  StatRelay3 = OFF;
  StatRelay4 = OFF;
  Serial.begin(9600);
  gsm.begin(9600);
  delay(30000);
  gsm.println("AT+CMGF=1");
  printSerialData();
  delay(1000);
  gsm.println("AT+CNMI=2,2,0,0,0");
  printSerialData();
}

void loop() {
  digitalWrite(Relay1, StatRelay1);
  digitalWrite(Relay2, StatRelay2);
  digitalWrite(Relay3, StatRelay3);
  digitalWrite(Relay4, StatRelay4);
  String response = gsmAnswer();
  if (response.indexOf("+CMT:") > 0 ) { // SMS arrived
    // Now You can parse Your Message, if You wont controll only LED, just write
    if (response.indexOf("admin") > 0) {
      char str[100] = "admin,08123456789";
      char * nomer;
      printf ("Splitting string \"%s\" into tokens:\n", str);
      nomer = strtok (str, ",");
      while (nomer != NULL)
      {
        printf ("%s\n", nomer);
        nomer = strtok (NULL, " ,.-");
      }
      return 0;
    } else if (response.indexOf("relay1 ON") > 0) {
      StatRelay1 = ON;
      digitalWrite(Relay1, StatRelay1);
      gsm.println("AT+CMGS=\"" + nomorHPAdmin + "\"");
      delay(1000);
      gsm.print("Relay 1 Sucess ON");
      gsm.write(0x1A);
      printSerialData();
      siap = 0;
      errorCounter = 0;
      bacaSMS.remove(0);
    }
    else if (response.indexOf("relay2 ON") > 0) {
      StatRelay2 = ON;
      digitalWrite(Relay2, StatRelay2);
      gsm.println("AT+CMGS=\"" + nomorHPAdmin + "\"");
      delay(1000);
      gsm.print("Relay 2 Sucess ON");
      gsm.write(0x1A);
      printSerialData();
      siap = 0;
      errorCounter = 0;
      bacaSMS.remove(0);
    }
    else if (response.indexOf("relay3 ON") > 0) {
      StatRelay3 = ON;
      digitalWrite(Relay3, StatRelay3);
      gsm.println("AT+CMGS=\"" + nomorHPAdmin + "\"");
      delay(1000);
      gsm.print("Relay 3 Sucess ON");
      gsm.write(0x1A);
      printSerialData();
      siap = 0;
      errorCounter = 0;
      bacaSMS.remove(0);
    }
    else if (response.indexOf("relay4 ON") > 0) {
      StatRelay4 = ON;
      digitalWrite(Relay4, StatRelay4);
      gsm.println("AT+CMGS=\"" + nomorHPAdmin + "\"");
      delay(1000);
      gsm.print("Relay 4 Sucess ON");
      gsm.write(0x1A);
      printSerialData();
      siap = 0;
      errorCounter = 0;
      bacaSMS.remove(0);
    }
    delay(1000);
  }

}


String gsmAnswer() {
  String answer;
  while (!gsm.available());
  while (gsm.available()) {
    delay(5);
    if (Serial.available() > 0) {
      char s = (char)gsm.read();
      answer += s;
    }
  }
  return answer;
}

void printSerialData()
{
  while (gsm.available() != 0)
    Serial.write(gsm.read());
}

Every time you use the + operator on a String, it frees a chunk of memory in the heap and allocates a new larger array to hold the new larger String. It often can't put it back in the same place, and that leaves a hole in the heap. Keep that up for very long and your memory starts to look like swiss cheese. You've got less and less of the heap that is usable. Finally you try to make a String that is too large to fit between the heap and the stack and your program crashes.

String and microcontroller don't go together. Best fix is to remove the use of the String class all together.

Delta_G:
Change gsmAnswer from using Strings (which are bad for your Arduino anyway) to something that populates a char array.

does gsmAnswere have to be converted to char array?

beryindo:
does gsmAnswere have to be converted to char array?

It doesn't have to. You could read reply #3 and work with what you have. But beware what I wrote in reply #7. That particular function is one that will have the problem I pointed at. As long as you don't plan on running this for very long and it isn't mission critical you can continue to work with String. If it is critical that it doesn't randomly crash then I'd get rid of all the String class instances.

Delta_G:
Every time you use the + operator on a String, it frees a chunk of memory in the heap and allocates a new larger array to hold the new larger String. It often can't put it back in the same place, and that leaves a hole in the heap. Keep that up for very long and your memory starts to look like swiss cheese. You've got less and less of the heap that is usable. Finally you try to make a String that is too large to fit between the heap and the stack and your program crashes.

String and microcontroller don't go together. Best fix is to remove the use of the String class all together.

so is better?

gsm.println("AT+CMGS=\"") + nomorHPAdmin + "\"");

change to

gsm.print("AT+CMGS=\"");
gsm.print(nomorHPAdmin);
gsm.println("\"");

Yes. Except don't use println on the middle one unless you want a cr and lf before the last quote.