spaceCount() Challenge Solution


#1
#include <stdio.h>
#include <string.h>

int spaceCount(const char *sentence){
    int spaces = 0;

     for(int i = 0; i < (int)strlen(sentence); i++){
        if(sentence[i] == ' ') spaces++; // must use single quotes for string literal in c
    }
 
    return spaces;
}

int main(int argc, const char * argv[]) {

    const char *sentence = "He was not in the cab at the time";
    printf("\"%s\" has %d spaces\n", sentence, spaceCount(sentence));

    return 0;
}