Created
October 10, 2018 12:19
-
-
Save mmathys/816da8205f9406c752627a61b7643c14 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/** | |
* | |
* Question: Why don't the last two strings show up correctly in the terminal? | |
* | |
*/ | |
struct Tuple { | |
char* firstWord; | |
int wordCount; | |
}; | |
struct Tuple processString(char* string) { | |
printf("our string is: %s and total length %lu\n", string, strlen(string)); | |
int wordCount = 0; | |
for(int i = 0; i < strlen(string); i++) { | |
if(string[i] != ' ') wordCount++; | |
else break; | |
} | |
char firstWord[wordCount]; | |
for(int i = 0; i < wordCount; i++) { | |
firstWord[i] = string[i]; | |
} | |
struct Tuple t; | |
t.firstWord = firstWord; | |
t.wordCount = wordCount; | |
return t; | |
}; | |
void test(char* string) { | |
struct Tuple t = processString(string); | |
printf("'%s' -> ('%s', %i)\n", string, t.firstWord, t.wordCount); | |
} | |
int main(int argc, char* argv[]) { | |
test("lorem ipsum"); | |
test("ay ipsum"); | |
test(". ipsum"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment