Skip to content

Instantly share code, notes, and snippets.

@mmathys
Created October 10, 2018 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmathys/e54025aad2ca9088d787b85cc6e19de6 to your computer and use it in GitHub Desktop.
Save mmathys/e54025aad2ca9088d787b85cc6e19de6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Tuple {
char* firstWord;
int wordCount;
};
struct Tuple processString(char* string) {
printf("our string is: %s and total length %i\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 = {
firstWord, 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