-3

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;
}
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
cba1067950
  • 35
  • 1
  • 11
  • `for(int i = 0; i > 256; i++) { if (strcmp(sentence[i], space) == 0) { tally++ } }` 1) does not compile missing semicolon 2) for should be `for(int i = 0; i < 256; i++)` not `> 256`. And why is it tagged C++ if you're coding in full C ? – Jean-François Fabre Sep 15 '16 at 21:41
  • 2
    Use `sentence[i] == ' '`. – jxh Sep 15 '16 at 21:42
  • `strcmp()` is used to compare strings. – STLDev Sep 15 '16 at 21:42
  • 2
    Stop trying to learn C++ by trial and error, that will get you nowhere. Learn it systematically from a good book instead. – Baum mit Augen Sep 15 '16 at 21:43
  • 1
    @Mr. Llama *"I can't use std strings"* Sounds like this is supposed to be C++. – Baum mit Augen Sep 15 '16 at 21:44
  • 1
    `for(int i = 0; sentence[i] ; i++) { if (sentence[i] == ' ')` – BLUEPIXY Sep 15 '16 at 21:44
  • @BaummitAugen, or maybe that should be taken as a sign that it is *not* supposed to be C++. Perhaps the poor soul is in the position of trying to learn C with only a C++ compiler at hand (e.g. MSVC++). – John Bollinger Sep 15 '16 at 21:46
  • From the title, this is about "c strings", and based on the code, it can be tagged as _both_ `c` and `c++` because it's only using elements common to both. – Craig Estey Sep 15 '16 at 21:48
  • @cba1067950 Maybe you misunderstand what kind of resouce this is supposed to be. It's not just for you, it's for all the future users of the site, who want to find questions questions that contain solutions to their problems. Well-written questions with proper tags are important for that function. – Barmar Sep 15 '16 at 22:10
  • @cba1067950 I'm not the one who complained that this site is useless, when most of us have provided enormous help to thousands of programmers. Insulting us is not going to make us more willing to help you. – Barmar Sep 15 '16 at 22:21
  • 1
    I've been helping programmers on the Internet for 3 decades. As far as I'm concerned, if you're getting free technical support from this resource, you don't have any right to complain about the manner. If you want someone who will just answer your questions with no complaint, pay them.[ – Barmar Sep 15 '16 at 22:50

3 Answers3

3

If you really want to count space characters, I think the following method would be better, since it checks where char array ends. A string terminator(\0) signals the end of the char array. I don't know why you hard-coded 256.

int countSpaceCharacters(char* sentence)
{
    int count = 0;
    int i = 0;

    while (sentence[i] != '\0')
    {
        if (sentence[i] == ' ')
        {
            count++;
        }
        i++;
    }

    return count;
}

However, if you want to count words as I can see from the original method name, you need to think a better way. Because this algorithm would fail in non-perfect situations such as having consecutive space characters or punctuation marks that have no space character around them etc.

dnzprmksz
  • 144
  • 1
  • 6
  • Yeah good call. I was just rushing through it to test it out but yeah your way is better. – cba1067950 Sep 15 '16 at 22:26
  • no what is if parsing words where seperated by a tab " " not a single space??? isspace() doesn't differ between single/tab space – Raindrop7 Sep 15 '16 at 22:37
1

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;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • Ok thanks. Still unsure how const_cast works though. Is there anything you could recommend because I looked through the forums and I haven't seen any simple examples. I'll try to look through again though. Thanks. – cba1067950 Sep 15 '16 at 22:27
  • if you search for a single character in an array of scharacters then just use loops comparing this char with all the elements. if you search for a whole word in a text or in a array of characters use strcmp() – Raindrop7 Sep 15 '16 at 22:41
0

Might i suggest this:

for(int i = 0; i < 256; i++) {
    if (isspace(sentence[i])) // or sentence[i] == ' '
    {
        tally++
    }
}

What you are trying to do now is compare a char (sentence[i]) with a c-string (space) which wont work.

Note that your implementation wont do what you expect for sentences like

"a big   space     be    in here."

So you need to think about what to do for multiple spaces.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Yeah that's what I'm trying to do. Yeah I didn't know about isspace. That would work also but I'm more wondering how I can understand what the function is doing so I can trouble shoot issues, not that I don't appreciate the logic. Can I cast the variable to not be a const and still make the strcmp function work? – cba1067950 Sep 15 '16 at 21:56
  • isspace(char c) just compares a single char parameter with white space (single space or tabulated space), if it is true then it returns true and vice-versa. but in your example you could write: if(sentence[i] == ' ' || sentence ]) – Raindrop7 Sep 15 '16 at 22:33
  • `isblank()` is for tab or space/blank; `isspace()` looks for tab, space, newline, carriage return, form feed, vertical tab. – Jonathan Leffler Sep 16 '16 at 00:15