ccplusplus.com
Learn C, C++ Concepts
Friday, October 21, 2011
c program to count blank spaces
/************************************************************* * File : count-space-character-number-special-char.c * Author : Saurabh Gupta * Desc : read a file and count blank space, characters, * numbers & speical char * counting blank spaces in a file * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : AM 09:15 21 October 2011 ************************************************************/ #include <stdio.h> int main() { char cfilename[20],ch; FILE *fp; int nBlankSpaces = 0, nAlphabets = 0, nNumbers = 0, nWhiteSpaces = 0, nSpecialchar = 0; printf("\nEnter the name of the file : "); scanf("%s",cfilename); fp=fopen(cfilename,"r"); if(fp == NULL){ printf("\nError in reading\n"); return 0; } while((ch = fgetc(fp))!=EOF){ if(ch == ' ') { nBlankSpaces = nBlankSpaces+1; } else if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')) { nAlphabets = nAlphabets+1; } else if(ch>=0 && ch<=9) { nNumbers = nNumbers+1; } else if(ch=='\n' || ch=='\r' || ch=='\t') { nWhiteSpaces = nWhiteSpaces+1; } else { nSpecialchar = nSpecialchar+1; } } fclose(fp); printf("\nBlank space\t=\t%d",nBlankSpaces); printf("\nAlphabets\t=\t%d",nAlphabets); printf("\nNumbers\t\t=\t%d",nNumbers); printf("\nWhite spaces\t=\t%d",nWhiteSpaces); printf("\nSpecial char\t=\t%d",nSpecialchar); printf ("\n"); } /* * OUTPUT * [sgupta@ivog-ks-200 c]$ gcc count-space-character-number-special-char.c -o count-space-character-number-special-char [sgupta@ivog-ks-200 c]$ ./count-space-character-number-special-char Enter the name of the file : count-space-character-number-special-char.c Blank space = 470 Alphabets = 975 Numbers = 0 White spaces = 122 Special char = 480 [sgupta@ivog-ks-200 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment