Check if char array equals string

Hello,

I have a char array that represents the first word read in a text file. The first word in my file is "PROGRAM". I want to have an if statement that says if the first word is "PROGRAM", do this....

The code I have right now for it is:
1
2
if(nextTokenData[0] == 'P' && nextTokenData[1] == 'R' && nextTokenData[2] == 'O' && nextTokenData[3] == 'G' && nextTokenData[4] == 'R' && nextTokenData[5] == 'A' && nextTokenData[6] == 'M')
        cout << "It Worked";


There has to be a more efficient way of doing this.

Thanks for the help,

Dennis
1
2
if(!strcmp(nextTokenData,"PROGRAM"))
  cout << "It Worked";


Or instead of using a char array, use a string:

1
2
3
4
string nextTokenData = whatever;

if(nextTokenData == "PROGRAM")
  cout << "It Worked";
Also possible:

1
2
if (equal(nextTokenData,nextTokenData+7,"PROGRAM"))...;
if (string(nextTokenData,nextTokenData+7)=="PROGRAM")...; //note that this is potentially less efficient than the above 
Disch, The strcmp worked perfectly! Thank you so much for the help.
Topic archived. No new replies allowed.