```
int countSubstrings(string s) {
int count = 0;
for (int i = 0; i < s.size(); i++) {
// When length of substring is odd
for (int j = 0; i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]; j++)
count++;
// When length of substring is even
for (int j = 0; i - j >= 0 && i + j + 1 < s.size() && s[i - j] == s[i + j + 1]; j++)
count++;
}
return count;
}
```