Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

c - Program to capitalise starting letter of each word

My code is :

#include <stdio.h>
int main(){
    FILE *fp;
    FILE *fp1;
    fp=fopen ("input.txt","r");
    fp1=fopen ("output.txt","w+");
    char a;
    while ((a=fgetc(fp))!=EOF){
        fputc(a,fp1);
    }
    char b=fgetc(fp1);
    int e='a';
    int f='z';
    if (b>=e && b<=f){
        b-=32;
        fputc(b,fp1);
    }
    char c;
    while ((c=fgetc(fp1))!=EOF){
        if (c==' '){
            c=fgetc(fp1);
            if (c>=e && c<=f){
                c-=32;
                fputc (c,fp1);
            }
        }
    }
    return 0;
}

Sample Input:

C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around 1970 The language was formalized in 1988 by the American National Standard Institue (ANSI). By 1973 UNIX OS almost totally written in C. Today C is the most widely used System Programming Language. Most of the state of the art software have been implemented using C. Easy to learn Structured language It produces efficient programs. It can handle low-level activities. It can be compiled on a variety of computers.

Sample output:

C Was Invented To Write An Operating System Called UNIX. C Is A Successor Of B Language Which Was Introduced Around 1970 The Language Was Formalized In 1988 By The American National Standard Institue (ANSI). By 1973 UNIX OS Almost Totally Written In C. Today C Is The Most Widely Used System Programming Language. Most Of The State Of The Art Software Have Been Implemented Using C. Easy To Learn Structured Language It Produces Efficient Programs. It Can Handle Low-level Activities. It Can Be Compiled On A Variety Of Computers.

However on compiling, I am getting same output as input. Can someone help?

question from:https://stackoverflow.com/questions/66049325/program-to-capitalise-starting-letter-of-each-word

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're over complicating things. This would do:

FILE *fp=fopen ("input.txt","r");
FILE *fp1=fopen ("output.txt","w+");    

int capitalize = 1;
int c; // int, not char

while((c = fgetc(fp)) != EOF) {
    char a = c;

    if(capitalize) {
        a = toupper(c);
        capitalize = 0;
    }

    if(isspace(a)
        capitalize = 1;

    fputc(a, fp1);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...