My solution of challenge


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

unsigned int spaceCount(const char *s);

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;
}

unsigned int spaceCount(const char *s)
{
    unsigned long length = strlen(s);
    unsigned int count = 0;
    for (int i = 0; i < length; i ++) {
        if (0x20 == s[i]) {
            count++;
        }
    }
    return count;
}

#2

Here are the two options I got:

#1

[code]unsigned int spaceCount(const char *myString)
{
int spaces = 0;
int i = 0;

while (myString[i] != '\0') {
    if (myString[i] == ' ') {
        spaces++;
    }
    i++;
}
return spaces;

}[/code]

Alternatively, using strlen, a for-loop, and checking for the hex code of the space character instead of the literal ’ ':
#2

[code]unsigned int spaceCount(const char *myString)
{
int spaces = 0;

for (int i = 0; i < strlen(myString); i++) {
    if (myString[i] == ' ') {
        spaces++;
    }
}

return spaces;

}[/code]


#3

My solution

[code]unsigned int spaceCount(const char *sentence)
{
int p;
int spaceCount;
char end = ‘\0’;
char space = 0x20;

for (p = 0; sentence[p] != end; p++)
{
    if(sentence[p] == space)
    {
        spaceCount++;
    }
}

return spaceCount;

}[/code]