codescracker


c

C Program to count Characters, Spaces, Tabs, Newline in a File



« C Tutorial C Examples »

Before start to write a program in C that counts total number of characters, spaces, tabs, and newline present in any file inside the current directory. Here current directory means the directory where your C program files are getting saved.

Now let's create a textual file named codescracker.txt and write the following line:

hello	everyone
i am	a file
this is codescracker
there are total of 4 lines present here

Now put this file inside the current directory.

Here is the snapshot of the file codescracker.txt in which all the above texts are written (that is of 4 lines). In this screenshot, you can also see that the file is saved inside the current directory:

c program count character in file

Now here is the program that counts characters, spaces, tabes, and newline present inside the file that was created earlier named codescracker.txt. You can also create any file to count these things for that file. Or you can also enter any filename that already exists inside the current directory:

// Write a program in C that counts
// all characters, spaces, tabs, and newline
// in a file entered by user
// ----------codescracker.com---------

#include<stdio.h>
#include<conio.h>
int main()
{
	FILE *fp;
	char ch, fname[30];
	int noOfChar=0, noOfSpace=0, noOfTab=0, noOfNewline=0;
	printf("Enter file name with extension: ");
	gets(fname);
	fp = fopen(fname, "r");
	while(fp)
	{
		ch = fgetc(fp);
		if(ch==EOF)
		{
			break;
		}
		noOfChar++;
		if(ch==' ')
		{
			noOfSpace++;
		}
		if(ch=='\t')
		{
			noOfTab++;
		}
		if(ch=='\n')
		{
			noOfNewline++;
		}
	}
	fclose(fp);
	printf("\nNumber of characters = %d", noOfChar);
	printf("\nNumber of spaces = %d", noOfSpace);
	printf("\nNumber of tabs = %d", noOfTab);
	printf("\nNumber of newline = %d", noOfNewline);
	getch();
	return 0;
}

As the above program is written under Code::Blocks IDE, therefore after successfully build and run, here is the sample run. This is first screenshot:

c program count spaces in file

Now enter the name of that file with extension that has created in the current directory and press enter key. If you will also create the same file with same content, then here is the second screenshot of the sample run that you will also get on your output screen:

c program count tabs in file

As you can see that the program shows total number of characters, spaces, tabs, and newline present in the file codescracker.txt.

You can also type the name of that file in which your current program is written. In my case, the name of the program is 21.c, let's type 21.c and find out what will happen:

c program count newline in file

As you can see, the program also prints the total number of characters, tabs, spaces, newline present inside program that you have written and saved.

Note - Before typing your current program name as filename, be sure that you have saved your program.

Here are some of the main steps used in the above program:

  1. Get the file name as input from user at run-time. Always ask from user to enter filename along with its extension say codescracker.txt. Here .txt is the extension of the file
  2. Open that file using fopen() function to read the content present inside that file
  3. Create a while loop and run this loop until the file pointer goes to the end of the file
  4. Get the character from file using fgetc() function and initialize it to ch variable
  5. Check whether ch is equal to the end of file, if it is, then break out of the loop, otherwise continue to the next statement
  6. First increment the value of variable that is responsible for character counting. Because everything inside the file is character
  7. And then check whether character variable is equal to space, if it is, then increment the value of variable that is responsible for space counting
  8. In similar way, increment the value of tab and newline counting variable, if found
  9. In this way, you have total of four variables, that is, noOfChar, noOfSpace, noOfTab, and noOfNewline
  10. All these four variables holds the total number of characters, spaces, tabs, and newline present inside the given file
  11. After breaking out of the loop, never forgot to close the file pointer using fclose() function
  12. Finally print out the value of all the four variables

You can also add some more lines of codes to count extra things like vowels, numbers etc. Let's create another program with some extra lines of code along with its sample run. Here is the program:

// Write a program in C to count following things inside any file
// entered by user at run-time:
//  [1] Total Number of Characters
//  [2] Total Number of Spaces
//  [3] Total Number of Tabs
//  [4] Total Number NewLines
//  [5] Total Number Vowels
//  [6] Total Number of Consonants
//  [7] Total Number of Numbers
// --------codescracker.com--------

#include<stdio.h>
#include<conio.h>
int main()
{
	FILE *fp;
	char ch, fname[30];
	int noOfChar=0, noOfSpace=0, noOfTab=0, noOfNewline=0;
	int noOfVowel=0, noOfConsonant=0, noOfNumber=0;
	printf("Enter file name with extension: ");
	gets(fname);
	fp = fopen(fname, "r");
	while(fp)
	{
		ch = fgetc(fp);
		if(ch==EOF)
			break;
		noOfChar++;
		if(ch>=65 && ch<=90)
		{
			noOfConsonant++;
			if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
				noOfVowel++;
		}
		if(ch>=97 && ch<=122)
		{
			noOfConsonant++;
			if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
				noOfVowel++;
		}
		if(ch>=48 && ch<=57)
			noOfNumber++;
		if(ch==' ')
			noOfSpace++;
		if(ch=='\t')
			noOfTab++;
		if(ch=='\n')
			noOfNewline++;
	}
	fclose(fp);
	printf("\nTotal Number of Characters = %d", noOfChar);
	printf("\nTotal Number of Spaces = %d", noOfSpace);
	printf("\nTotal Number of Tabs = %d", noOfTab);
	printf("\nTotal Number NewLines = %d", noOfNewline);
	printf("\nTotal Number Vowels = %d", noOfVowel);
	printf("\nTotal Number of Consonants = %d", noOfConsonant);
	printf("\nTotal Number of Numbers = %d", noOfNumber);
	getch();
	return 0;
}

After successfully build and run, here is the first screenshot of the sample run of above program:

count vowels consonants numbers in file

Now enter any file name. Let's enter the current filename, where we have written and saved our current program, that is test.c in this case and press ENTER key to see the following output:

c count vowels in file

« C Tutorial C Examples »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test