Created
April 19, 2017 06:49
-
-
Save trentrand/d8870d1b8b5e38117f3e27fdcf8b41e5 to your computer and use it in GitHub Desktop.
Psuedo-code for Count Words problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test Cases: | |
// “A BIG DOG” should return 3 | |
// “A” should return 1 | |
// “ BC “ should return 1 | |
int CountNumWordsInSentence(const char* InString) { | |
bool wasLetter = true; | |
int wordCount = 0; | |
// For this for loop. take advantage that InString is a char pointer. | |
// This means that each character in the string points to the next one, | |
// making easy to iterate through the letters. | |
// Lookup how to iterate through letters of a char pointer string | |
for(// each char in InString) { | |
charIsLetter = isLetter(char); | |
if (!wasLetter & isLetter) { | |
wordCount++; | |
} | |
wasLetter = isLetter; | |
} | |
} | |
bool isLetter(char* letter) { | |
// a char in c is actually just an ASCII character code | |
// these codes go in order and go from 65 to 90 for uppercase and 97 to 122 | |
// so you can see if letter is between those values or just do | |
// (letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z') | |
if ( // letter is A-Z ) { | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment