The motive of my program is to test for the longest word in a string and return whether or not the longest word matches the expected result.
I wrote the code but my brain got stuck in the part where I'm to store the result of my function longest word on line into the char array result. How do you do this in C?
My effort:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
static int testsExecuted = 0;
static int testsFailed = 0;
char testLongestWord(char line[], char expected[]);
void longestWord(char line[]);
int main(int args, char *argv[]){
printf("%s\n", "Testing typical cases, including punctuation\n");
testLongestWord("the quick brown foxes jumped over the lazy dogs", "jumped");
testLongestWord("hello world she said", "hello");
testLongestWord("Hello\tworld\tshe\tsaid", "Hello");
testLongestWord("HELLO, world she said", "HELLO");
testLongestWord("hello world! she said???", "hello");
testLongestWord("\"hello world!\", she said.", "hello");
testLongestWord("easy as abc123", "abc123");
testLongestWord("easy as abc,123", "easy");
printf("\n%s\n", "Testing empty cases\n" );
testLongestWord("", "");
testLongestWord("!", "");
testLongestWord(" ", "");
testLongestWord("\t", "");
testLongestWord(" ", "");
testLongestWord("# $ ? % !", "");
printf("\n%s\n", "Testing edge cases\n" );
testLongestWord("a", "a");
testLongestWord("abc", "abc");
testLongestWord("abc d e f ghi", "abc");
testLongestWord("a a b cc dd abc", "abc");
testLongestWord("\"a a b cc dd abc.\"", "abc");
printf("\n%s\n", "Testing apostrophes and dashes\n" );
testLongestWord("this isn't five chars", "chars");
testLongestWord("this should've been eight chars said the computer", "should've");
testLongestWord("'this should've been eight chars', said the computer", "should've");
testLongestWord("'hello world!', she said softly.", "softly");
testLongestWord("topsy-turvy is a tenletter word", "topsy-turvy");
testLongestWord("topsy-turvy should not be incorrectly eleven characters", "incorrectly");
testLongestWord("---in-between-these---", "in-between-these");
testLongestWord("---in---between---these---", "between");
testLongestWord("here-is-an-edge-case but a muchmuchlongerword", "muchmuchlongerword");
testLongestWord("d-o-n't-g-o-o-v-e-r-t-h-e-e-d-g-e with muchmuchlongerwords", "muchmuchlongerwords");
testLongestWord("two=five-3 isn't three", "three");
printf("\n%s\n", "These tests will be opposite in the C version\n");
testLongestWord("the word antidisestablishmentarianism is very long but not as long as 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch'.", "Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch");
testLongestWord("the word antidisestablishmentarianism is very long but not as long as 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch'.", "antidisestablishment");
testLongestWord("Java strings may contain \0 in the interior", "interior");
testLongestWord("C strings cannot contain \0 in the interior", "strings");
printf("Total number of test executed: %d\n", testsExecuted );
printf("number of test passed: %d\n", (testsExecuted - testsFailed));
printf("Number of test failed: %d\n", testsFailed );
//longestWord("Java strings may contain \0 in the interior");
}
char testLongestWord(char line[], char expected[]){
//char result[200];
String result = longestWords(line); //This is how it'd have been in Java
/*longestWord(line);*/
//strcpy(result, line);
//char *result = longestWord(line);
//printf("%s\n", line );
//longestWord(&line)
if(strcmp(result,expected)){ // function returns 0 if they are equal
printf("passed: '%s' from '%s'\n", result, line);
}else{
printf("FAILED: '%s' from '%s'\n", expected, result);
testsFailed++;
}
testsExecuted++;
return 0;
}
void longestWord(char line[]){
char longest[200];
int pos = 0;
int longestLength = 0;
char current[300];
int currentLength = 0;
char ch;
size_t maxPos = strlen(line);
while(pos < maxPos){
ch = line[pos++];
for(pos = 0; pos < maxPos;pos++){
ch = line[pos++];
if((ch == '\'' || ch == '-') && (pos > 0) && isalpha(line[pos-1]) && isalpha(line[pos+1])){
strcpy(current, &ch);
}else if(isalpha(ch) || isdigit(ch)){
strcpy(current, &ch);
currentLength++;
//printf("%s\n", longest );
}else{
if(currentLength > longestLength){
strcpy(longest,current);
longestLength = currentLength;
}
//strcpy(current, "");
currentLength =0;
}
}
}
}
Output: (This output was given in a similar code just done in Java)
Testing typical cases, including punctuation
Passed: 'jumped' from 'the quick brown foxes jumped over the lazy dogs'
Passed: 'hello' from 'hello world she said'
Passed: 'Hello' from 'Hello world she said'
Passed: 'HELLO' from 'HELLO, world she said'
Passed: 'hello' from 'hello world! she said???'
Passed: 'hello' from '"hello world!", she said.'
Passed: 'abc123' from 'easy as abc123'
Passed: 'easy' from 'easy as abc,123'
Testing empty cases
Passed: '' from ''
Passed: '' from '!'
Passed: '' from ' '
Passed: '' from ' '
Passed: '' from ' '
Passed: '' from '# $ ? % !'
Testing edge cases
Passed: 'a' from 'a'
Passed: 'abc' from 'abc'
Passed: 'abc' from 'abc d e f ghi'
Passed: 'abc' from 'a a b cc dd abc'
Passed: 'abc' from '"a a b cc dd abc."'
Testing apostrophes and dashes
Passed: 'chars' from 'this isn't five chars'
Passed: 'should've' from 'this should've been eight chars said the computer'
Passed: 'should've' from ''this should've been eight chars', said the computer'
Passed: 'softly' from ''hello world!', she said softly.'
Passed: 'topsy-turvy' from 'topsy-turvy is a tenletter word'
Passed: 'incorrectly' from 'topsy-turvy should not be incorrectly eleven characters'
Passed: 'in-between-these' from '---in-between-these---'
Passed: 'between' from '---in---between---these---'
Passed: 'muchmuchlongerword' from 'here-is-an-edge-case but a muchmuchlongerword'
Passed: 'muchmuchlongerwords' from 'd-o-n't-g-o-o-v-e-r-t-h-e-e-d-g-e with muchmuchlongerwords'
Passed: 'three' from 'two=five-3 isn't three'
These tests will be opposite in the C version
Passed: 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch' from 'the word antidisestablishmentarianism is very long but not as long as 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch'.'
FAILED: 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch' instead of 'antidisestablishment' from 'the word antidisestablishmentarianism is very long but not as long as 'Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch'.'
Passed: 'interior' from 'Java strings may contain in the interior'
FAILED: 'interior' instead of 'strings' from 'C strings cannot contain in the interior'
Total number of tests executed: 34
Number of tests passed: 32
Number of tests failed: 2