Member-only story
In C++, write a method that checks if 2 char[] arrays are equal to each other
1 min readMay 11, 2022
Explanation
We will check if 2 char[] array has the same values. We have a for loop which will iterate from 0 to when there is no more value in the index of the array, which is verified with this statement: str1[i]!=’\0'.
If you don’t want case sensitive, then add the tolower() statement to make the letter lowercase. Otherwise, if you want it case sensitive, then there is no need to use tolower().
If the letter of the char[] str1 does not match with the char[] str2, then return false, which will exit the method. If each of the letter matched, then the method will return true once it exists the for loop.
Code
bool isSameStringInCharArray(char str1[MAX_WORD_LENGTH+1], char str2[MAX_WORD_LENGTH+1]){for (int i=0; str1[i]!='\0'; i++){char toLower1 = tolower(str1[i]);char toLower2 = tolower(str2[i]);if (toLower1 != toLower2){return false;}}return true;}