// demo of fgets

// User will type in a sentence in camelcase
// We will split it into separate strings

#include <stdio.h>
#include <ctype.h>

#define MAX_CHARS 50
#define MAX_WORDS 10
#define MAX_INPUT (MAX_CHARS * MAX_WORDS)

int main(void) {
    // array of strings to contain words
    char words[MAX_WORDS][MAX_CHARS];
    
    // read line of input
    char inputLine[MAX_INPUT];
    fgets(inputLine, MAX_INPUT, stdin);
    
    // loop through and split the line whenever we see an upper case character
    int i = 0;
    int currentWord = 0;
    int currentLetter = 0;
    while (i < MAX_INPUT && inputLine[i] != '\0') {
        if (isupper(inputLine[i])) {
            // letter is upper case
            // move on to a new word and add this letter
            currentWord++;
            currentLetter = 0;
        }
        words[currentWord][currentLetter] = inputLine[i];
        currentLetter++;
    }
}

Resource created Tuesday 11 August 2020, 07:41:20 PM.

file: fgetsDemo.c


Back to top

COMP1511 20T2 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G