This is a little tool to count the Lines, Digts Words, Chars, UpperCase and LowerCase chars in a file.
How to useCode:// File : ts.c // By : Ben a.k.a DreamVB // Date : 20:50 26/05/2020 // Info : Little tool to count the Lines, Digts Words, Chars, UpperCase and LowerCase chars in a file. #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> int m_words = 0; int m_chars = 0; int m_upper = 0; int m_lower = 0; int m_lines = 0; int m_digits = 0; int _countUpper(char *src){ int x = 0; int u = 0; while(x < strlen(src)){ //Check if we have an alpha if(isalpha(src[x])){ if(isupper(src[x])){ u++; } } x++; } return u; } int _countLower(char *src){ int x = 0; int l = 0; while(x < strlen(src)){ //Check if we have an alpha if(isalpha(src[x])){ if(islower(src[x])){ l++; } } x++; } return l; } int main(int argc, char *argv[]) { FILE *fp = NULL; char ch = '\0'; char lzBuffer[30]; int x = 0; if(argc != 2){ printf("Source File Not Found.\n"); printf(" Usage: ts Filename\n"); return 0; } //Open file fp = fopen(argv[1],"r"); if(fp == NULL){ printf("Cannot Read Source File:\n"); printf(argv[1]); } //While not end of file read it while(!feof(fp)){ //Get current char from file. ch = fgetc(fp); //Count digits if(isdigit(ch)){ m_digits++; } //Count number of lines if(ch == '\n' || ch == '\0'){ m_lines++; } //Check if end of file. if(!feof(fp)){ //Check for space char if((ch == 32) || (ch == 9)){ //Set end of string buffer. lzBuffer[x] = '\0'; //Check length of string if(strlen(lzBuffer) > 0 ){ //INC word counter m_words++; //INC number of chars in lzBuffer m_chars+= strlen(lzBuffer); //INC upper. m_upper+= _countUpper(lzBuffer); //INC lower. m_lower+= _countLower(lzBuffer); } //Reset x x = 0; }else{ //Collect chars for string buffer. lzBuffer[x] = ch; //INC buffer char x+=1; } } } //Set end of string buffer lzBuffer[x] = '\0'; //Check length of string if(strlen(lzBuffer) > 0 ){ //INC word counter m_words++; //INC number of chars in lzBuffer m_chars+= strlen(lzBuffer); //INC upper m_upper+= _countUpper(lzBuffer); //INC lower m_lower+= _countLower(lzBuffer); } //Close file fclose(fp); //Print stats of Words and Chars printf("Lines : %d\n",m_lines); printf("Digits : %d\n",m_digits); printf("Words : %d\n",m_words); printf("Chars : %d\n",m_chars); printf("UpperCase : %d\n",m_upper); printf("LowerCase : %d\n",m_lower); //Clear array memset(lzBuffer,0,sizeof lzBuffer); return 0; }
Code:./ts example.c "printf"




Reply With Quote
