Enter a line of characters from the keyboard, which are the statistics of the number of letters, spaces, numbers and other characters, and written to the disk file "stud" in.

 

/*
Line of characters input from the keyboard, wherein each can count the number of letters, spaces, numbers and other characters,
And the input string, and the number of letters, the number of spaces, the number of digits, and other characters are written to the disk file "stud" in.
*/
#include <stdio.h>
#include <string.h>
int main()
{
    char st[100];
    gets(st);
    int i,a[4]={0};
    FILE *fp=fopen("d:\\stud.txt","rb+");
    for(i=0;st[i]!='\0';i++){
        if(st[i]>='A'&&st[i]<='Z'||st[i]>='a'&&st[i]<='z')
            a[0]++;
        else if(st[i]>='0'&&st[i]<='9')
            a[1]++;
        else if(st[i]==' ')
            a[2]++;
        else
            a[3]++;
    }
    for(i=0;i<4;i++){
        printf("%d ",a[i]);
        fwrite(&a[i],4,1,fp);
    }
    fclose(fp);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zhaohuan1996/p/11944522.html