0

How to use the check function?

bool check (char* word, int size) { // How does check work? Why is * used? for (int x=1; x<size; x+=2) { // what does the condition mean in this line? if (word[x] != 'a'){ // why use [], are we dealing with an array? return false; } } return true; } Also, are single quotes mandatory in the 3rd line?

27th Jul 2020, 1:06 AM
Solus
Solus - avatar
1 Réponse
0
The function checks whether any of the characters at odd indices (starting at index 1) equal to character 'a'. When either one of the characters positioned at odd index equals to 'a', the function should return false. But if the loop goes through without finding a match, then it should return true. Q: Why * is used? A: Parameter <word> is defined as a char pointer, meaning it can accept a char array or C-string (constant char pointer) as argument. Q: What does the loop condition mean? A: It specifies that the for-loop should repeat while value of <x> is less than value of given argument <size>. Q: Why use [] A: The [] (subscript operator) is used to refer to one of the character in <word> by their index. Q: Are single quotes mandatory? A: In this case it is, because we are comparing the characters at odd index (one at a time) to character 'a'. Single quotes defines a character, double quotes defines constant string literal (const char pointer). Hth, cmiiw
27th Jul 2020, 2:07 AM
Ipang