I'm trying to count spaces using c strings. I can't use std strings. And on the line comparing the two chars I get the error 'invalid conversion from 'char' to 'const char*'.
I understand that I need to compare two const chars* but I'm not sure which one is which. I believe sentence[] is the char and char space[] is const char* is that right? And I need to use some sort of casting to convert the second but I'm not understanding the syntax I guess. Thanks for the help <3
int wordCount(char sentence[])
{
int tally = 0;
char space[2] = " ";
for(int i = 0; i > 256; i++)
{
if (strcmp(sentence[i], space) == 0)
{
tally++
}
}
return tally;
}
strcmp is used to compare two character strings not a single character with one character string.
there is no function: strcmp(char c, char*); // if there's it's illogical!
if you want to search for a single character in a character string just compare this character with all elements using iteration:
iint wordCount(char* sentence)
{
int tally = 0;
char space[2] = " ";
for(int i = 0; i < strlen(sentence); i++)
{
if (sentence[i] == space[0])
{
tally++;
}
}
return tally;
}