Jokiz won our last book giveaway! now its time for another programming quiz!
Problem:Given a sentence or phrase, count the number of words that exist in a phrase. Each word is delimited by a space and words that are wrapped in quotes ("") is considered 1 word
Oh, No using of built in String methods(e.g String.Split, String.Join, String.ToCharArray and etc.)
Below are test inputs that you should consider when writing the application
The phrase: [ this is a long text consist of different words. ] has 9 wordsThe phrase: [ "this is a long text consist of different words." ] has 1 wordsThe phrase: [ this "is a long" text consist "of different words. ] has 7 wordsThe phrase: [ "this is a long text consist of different words. ] has 9 wordsThe phrase: [ this is a long text consist of different words." ] has 9 wordsThe phrase: [ ""this is a long text consist of different words. ] has 9 wordsThe phrase: [ this is a long text consist of different words."" ] has 9 wordsThe phrase: [ this is a long text"" consist of different words. ] has 9 wordsThe phrase: [ this is a long text "" consist of different words. ] has 9 wordsThe phrase: [ this is a long text consist of different words. ] has 9 wordsThe phrase: [ this is a long text consist of different words. ] has 9 wordsThe phrase: [ ] has 0 words
prize: A TDD Book + C# Book(or Java)
*note: the best solution will win the prize | Deadline for submissions is December 18, 2006
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
public class WordCount { public bool StartOfNewWord(char prev, char next) { if ((prev == ' ') && (next != ' ')) return true; else return false; } public bool ForbiddenWord(string word) { const string doubleQuotesOnly = "\"\""; const string lonelyQuote = "\""; return ((word == doubleQuotesOnly) || (word == lonelyQuote) || (word == String.Empty) || OnlySpacesIn(word)); } public bool OnlySpacesIn(string word) { bool allSpaces = true; string strippedWord = RemoveInitialQuoteFrom(word); for (int i = 0; i < strippedWord.Length; i++) if (strippedWord[ i ] != ' ') { allSpaces = false; break; } return allSpaces; } public string RemoveInitialQuoteFrom(string word) { string newWord = String.Empty; for (int i = 0; i < word.Length; i++) if (word[ i ] != '"') newWord += word[ i ]; return newWord; } public bool HasOnlyOneQuote(string word) { int quoteCount = 0; for (int i = 0; i < word.Length; i++) { if (word[ i ] == '"') quoteCount++; } return (quoteCount == 1); } public int CountWordsIn(string message) { char prev = ' '; int numWords = 0; bool inQuote = false; string currentWord = String.Empty; for (int i = 0; i < message.Length; i++) { char current = message[ i ]; if (inQuote) { currentWord += current; if (current == '"') { inQuote = !inQuote; } } else if (StartOfNewWord(prev, current)) { if (!ForbiddenWord(currentWord)) numWords++; currentWord = String.Empty; currentWord += current; if (current == '"') { inQuote = !inQuote; } } else { if (current != ' ') currentWord += current; } prev = current; } if (!ForbiddenWord(currentWord) && HasOnlyOneQuote(currentWord)) { numWords += CountWordsIn(RemoveInitialQuoteFrom(currentWord)); } else if (!ForbiddenWord(currentWord)) numWords++; return numWords; } }
cruizer:what if the quotes only contain spaces? likethis is a long " " textdoes that count as 5 words or 6?
according to the instruction it should be considered a word so the correct answere is 6
Ahh suko na ko sa C# nakaklito masyado, here's mine.
12345678910111213141516171819202122232425262728
Public Class clsWords Public Function Count(ByVal strSentence As String) As Integer Dim chrArray As Char() = strSentence Dim intCntr As Integer Dim blnDoubleQuoteEncountred As Boolean Dim intStringCount As Integer For intCntr = 0 To chrArray.Length - 1 If chrArray(intCntr) = """" Then If blnDoubleQuoteEncountred Then blnDoubleQuoteEncountred = False intStringCount += 1 Else blnDoubleQuoteEncountred = True End If ElseIf chrArray(intCntr) = " " And blnDoubleQuoteEncountred = False Then If intCntr > 0 Then If chrArray(intCntr - 1) <> " " Then intStringCount += 1 End If Next Return intStringCount End FunctionEnd Class
cruizer:my modified solution to conform to Keith's "user story:" public class WordCount { public bool StartOfNewWord(char prev, char next) { if (IsASpace(prev) && !IsASpace(next)) return true; else return false; } public bool ForbiddenWord(string word) { const string doubleQuotesOnly = "\"\""; const string lonelyQuote = "\""; return ((word == doubleQuotesOnly) || (word == lonelyQuote) || (word == String.Empty)); } public bool ForbiddenWord(StringBuilder word) { return ForbiddenWord(word.ToString()); } public string RemoveInitialQuoteFrom(string word) { string newWord = String.Empty; for (int index = 0; index < word.Length; index++) if (!IsAQuote(word[index])) newWord += word[index]; return newWord; } public string RemoveInitialQuoteFrom(StringBuilder word) { return RemoveInitialQuoteFrom(word.ToString()); } public bool HasOnlyOneQuote(string word) { int quoteCount = 0; for (int index = 0; index < word.Length; index++) { if (IsAQuote(word[index])) quoteCount++; } return (quoteCount == 1); } public bool HasOnlyOneQuote(StringBuilder word) { return HasOnlyOneQuote(word.ToString()); } public int CountWordsIn(string message) { char prev = ' '; int numWords = 0; bool inQuote = false; StringBuilder currentWord = new StringBuilder(); for (int index = 0; index < message.Length; index++) { char current = message[index]; if (inQuote) { currentWord.Append(current); } else if (StartOfNewWord(prev, current)) { if (!ForbiddenWord(currentWord)) numWords++; currentWord = new StringBuilder(); currentWord.Append(current); } else { if (!IsASpace(current)) currentWord.Append(current); } if (IsAQuote(current)) { inQuote = !inQuote; } prev = current; } if (HasOnlyOneQuote(currentWord)) { numWords += CountWordsIn(RemoveInitialQuoteFrom(currentWord)); } else if (!ForbiddenWord(currentWord)) numWords++; return numWords; } private bool IsAQuote(char c) { return (c == '"'); } private bool IsASpace(char c) { return (c == ' '); } }(edited for a bit of refactoring and removed line numbers)
using System.Text;
public class WordCount{ private int _index; private bool IsValidWord(string word) { return word.Length > 0 && word != "\"\"" && word != "\" " && word != "\""; }
private string GetWord(string message, int start) { StringBuilder word = new StringBuilder(); int sIndex = -1; string sWord = ""; int qIndex = -1; int i = start; while (i < message.Length && message[ i ] == ' ') i++; if (i >= message.Length) return "";
word.Append(message[ i ]); int j = i + 1;
if (word.ToString() == "\"") { while (j < message.Length) { word.Append(message[j]); if (sIndex == -1 && (message[j] == ' ' || j == message.Length - 1)) { sIndex = j; sWord = word.ToString(); } else if (message[j] == '"' && (j == message.Length - 1 || message[j+1] == ' ')) { qIndex = j; break; } j++; } if (qIndex != -1) { _index = qIndex; } else if (sIndex != -1) { _index = sIndex; word = new StringBuilder(sWord); //word.Replace(word.ToString(), sWord); // word.Replace might be illegal } else { _index = message.Length - 1; } } else { while (j < message.Length && message[j] != ' ') { word.Append(message[j]); j++; } _index = --j; } string wordString = word.ToString(); return word.ToString();
}
public int CountWords(string message) { string word; int wordCount = 0; for (_index = 0; _index < message.Length; _index++) { word = GetWord(message, _index); if (IsValidWord(word)) wordCount++; } return wordCount; }}
(x-a)(x-b)(x-c)...(x-z) Join MSPhil community
↑ Grab this Headline Animator
lea esi, bufferinvoke lstrlen, esicmp eax, 0jz _nothingmov ecx, eaxxor edi, edi_counter:dec ecxjz _finishedlodsbcmp al, ' 'je _validate_spacejmp _not_space_validate_space:inc edicmp edi, 1je _found_spacejmp _counter_not_space:xor edi, edicmp al, '"'je _found_quotejmp _counter_found_space:inc ebxjmp _counter_found_quote:dec ecxjz _finishedlodsbcmp al, '"'jne _found_quotejmp _counter_nothing:invoke MessageBox, hWin, addr nothing, addr caption, MB_OKjmp _breakout_finished:add ebx, 1invoke dwtoa, ebx, addr resultinvoke szMultiCat, 3, addr message_complete, addr message_start, addr result, addr message_endinvoke MessageBox, hWin, addr message_complete, addr caption, MB_OK_breakout:invoke lstrcpy, addr message_complete, addr blank