summaryrefslogtreecommitdiff
path: root/hw3/problem5.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw3/problem5.c')
-rw-r--r--hw3/problem5.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/hw3/problem5.c b/hw3/problem5.c
new file mode 100644
index 0000000..4af17d1
--- /dev/null
+++ b/hw3/problem5.c
@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+void printArray(char **words, int wordcount){
+ for(int i = 0; i < wordcount; i++)
+ printf("%s\n",words[i]);
+}
+void proper(char *word){
+ word[0] = toupper(word[0]);
+}
+int string2array(char *inString, char **wordarray){
+ const char whitespace[] = " ";
+ char **cursor = wordarray;
+ int ret = 0;
+ char *this = NULL;
+ while(1){
+ this = strtok(inString,whitespace);
+ if(this == NULL)
+ break;
+ char *dest = strcpy(wordarray[ret],this);
+ inString = NULL;
+ ret++;
+ }
+ return ret;
+}
+void getString(char *in, int buffer){
+ char *c = in;
+ for(; c < in + buffer; c++){
+ *c = getchar();
+ if(*c == EOF)
+ break;
+ }
+ *c = '\0';
+}
+
+struct Sentence {
+ char string[50];
+ char *words[10];
+ int wordCount;
+};
+
+/*
+ a. Store input into the sentence string,
+ b. Parse the string into an array of words and store the number of words found,
+ c. Proper case each word in the sentence,
+ d. and print out the proper cased sentence
+*/
+int main(int argc, char **argv){
+ struct Sentence s;
+ for(int i = 0; i < 10; i++)
+ s.words[i] = malloc(sizeof(char) * 50);
+ s.wordCount = 0;
+ getString(s.string,50);
+ s.wordCount = string2array(s.string, s.words);
+ for(int i = 0; i < 10; i++)
+ proper(s.words[i]);
+ printArray(s.words,s.wordCount);
+ return 0;
+}