[Solved] fixing an error i cannot spot in main() in C (stuck for hours on it – short program)


You can try this to see if it produces the desired results.
Characters that are not letters, space, newline or dot are rejected at the top of the while and all letters are set to lower case.
Then the choice is to print one upper case letter at the start of the sentence or all upper case inside double quotes.
There are no breaks as oneUpper needs to fall through to allUpper. allUpper needs to fall through to default.
getchar returns an int so int a instead of char a

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

#define oneUpper 1
#define allUpper 2

int main (int argc, const char *argv[])
{
    int status = oneUpper;
    int a;
    while ( EOF != (a = getchar ()))
    {
        //discard non letter except space, newline and .
        if ( !isalpha ( a) && a != ' ' && a != '\"' && a != '.') {
            continue;
        }
        //set all to lower and let oneUpper or allUpper do the rest.
        a = tolower ( a);
        switch (status)
        {
            case oneUpper:
                if ( a == ' ' || a == '\n') {
                    putchar ( a);//print leading space and continue
                    continue;
                }
            case allUpper:
                a = toupper ( a);
            default:
                putchar ( a);
                if ( a == '\"') {
                    if ( status == allUpper) {
                        status = 0;//turn all upper off
                    }
                    else {
                        status = allUpper;//turn all upper on
                    }
                }
                if ( status == oneUpper) {
                    status = 0;//after printing one upper turn it off
                }
                if ( a == '.') {
                    if ( status != allUpper) {
                        putchar ( '\n');
                        status = oneUpper;//after a . turn one upper on
                    }
                }
        }
    }
    return 0;
}

2

solved fixing an error i cannot spot in main() in C (stuck for hours on it – short program)