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

#define MAX_CHARS 50
#define MAX_WORDS 10

int main(void) {
    // array of strings to contain words
    char words[MAX_WORDS][MAX_CHARS];
    
    // read in one character at a time
    // split words if they're captials, otherwise add them to the current word 
    int currentWord = 0;
    int currentLetter = 0;
    
    int currentChar = getchar();
    while (currentChar != EOF) {
        // run the loop if getchar picked up a real character
        if (isupper(currentChar)) {
            // letter is upper case
            // move on to a new word and add this letter
            currentWord++;
            currentLetter = 0;
        }
        words[currentWord][currentLetter] = currentChar;
        currentLetter++;

        currentChar = getchar();
    }
}

Resource created Tuesday 11 August 2020, 07:42:53 PM.

file: getcharDemo.c


Back to top

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