+ 2

Why it is not counting the string c

Its not working https://code.sololearn.com/c4FoM86qY7o3/?ref=app

16th Mar 2023, 6:50 AM
Chirag Sharma
Chirag Sharma - avatar
2 Réponses
+ 5
there is a mistake in the countLength function. The for loop condition i != '\0' is incorrect because i is an integer index, not a character. Instead, the loop condition should check if the current character in the string is not equal to '\0', which represents the end of the string. The correct condition is arr[i] != '\0'. Additionally, the if statement inside the loop checks if i is equal to 'c', which is a character literal, instead of checking if the current character in the string is equal to 'c'. The correct condition is arr[i] == 'c'. int countLength(char arr[]) { int count = 0; for (int i = 0; arr[i] != '\0'; i++) { if (arr[i] == 'c') { count++; } } return count; }
16th Mar 2023, 7:07 AM
Minh Lưu
Minh Lưu - avatar
+ 1
Thanks 👍 it worked
16th Mar 2023, 8:09 AM
Chirag Sharma
Chirag Sharma - avatar