Changing Every Starting Character of a Sentence to Capital - C Programming


In this program we are going to change every character starting a sentence in a paragraph to upper case (capital letter) known as Sentence Case generally.

Note that we are performing these actions on two files
1. Input File (in.dat).
2. Output File (out.dat). [you can change names to what so ever you want]

we first of all prompt user to write to file in.dat and then copy data inside in.dat to another file out.dat with Sentence Casing (i,e changing small case to upper case letter of every character starting a string). And at last print the saved data in out.dat to screen.

Here is the code:-

/*Changing first character of sentence to upper case*/
#include<stdio.h>
#include<ctype.h>

int main(){
    char c;
    FILE *in = fopen("in.dat", "w");
    FILE *out = fopen("out.dat", "w");
    if(in == NULL){
        printf("[X] Can't Find File, Terminating Program...");
        return 0;
    }
    if(out == NULL){
        printf("[X] Can't Create File, Terminating Program...");
        return 0;
    }
    printf("Enter Text\nPress '#' to end\n");
    c = getchar();
    fprintf(in, "%c", toupper(c));
    while((c = getchar()) != '#'){
        fprintf(in, "%c", c);
    }
    fclose(in);
    in = fopen("in.dat", "r");
    while((c = fgetc(in)) != EOF){
        if(c == '.'){
            c = fgetc(in);
            if(c == '\n' || c == ' ' || c == '\t'){
                c = fgetc(in);
                fprintf(out, ". %c", toupper(c));
            } else {
                fprintf(out, ". %c", toupper(c));
            }
        }
        else
            fprintf(out, "%c", c);
    }
    fclose(out);
    out = fopen("out.dat", "r");
    while((c = fgetc(out)) != EOF){
        printf("%c", c);
    }
    fclose(in);
    fclose(out);
    return 0;
}

Comments