0% found this document useful (0 votes)
114 views

Write A Program To Count The Number of Words

The document describes a C program that counts the number of words, spaces, vowels, digits, and special characters in a given string. The program uses pointers to iterate through the string and increment various counters based on the character. It accepts a string from the user, initializes counters to 0, then loops through each character of the string. It checks the character and increments the appropriate counter, such as for spaces, words, vowels, digits, or special characters. At the end, it prints out the final values of each counter.

Uploaded by

kalanithi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Write A Program To Count The Number of Words

The document describes a C program that counts the number of words, spaces, vowels, digits, and special characters in a given string. The program uses pointers to iterate through the string and increment various counters based on the character. It accepts a string from the user, initializes counters to 0, then loops through each character of the string. It checks the character and increments the appropriate counter, such as for spaces, words, vowels, digits, or special characters. At the end, it prints out the final values of each counter.

Uploaded by

kalanithi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Write a program to count the number of words, lines and characters in a

text
Program finding number of words, blank spaces, special symbols, digits, vowels using
pointers

Count Number of Words using Pointer


efe
'

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
/*low implies that position of pointer is within a word*/
#define low 1
/*high implies that position of pointer is out of word.*/
#define high 0
void main() {
int nob, now, nod, nov, nos, pos = high;
char *str;
nob = now = nod = nov = nos = 0;
clrscr();
printf("Enter any string : ");
gets(str);
while (*str != '\0') {
if (*str == ' ') {
// counting number of blank spaces.
pos = high;
++nob;
} else if (pos == high) {
// counting number of words.
pos = low;
++now;
}
if (isdigit(*str)) /* counting number of digits. */
++nod;
if (isalpha(*str)) /* counting number of vowels */

2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3 }
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5

switch (*str) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
++nov;
break;
}
/* counting number of special characters */
if (!isdigit(*str) && !isalpha(*str))
++nos;
str++;
}
printf("\nNumber
printf("\nNumber
printf("\nNumber
printf("\nNumber
printf("\nNumber
getch();

of
of
of
of
of

words %d", now);


spaces %d", nob);
vowels %d", nov);
digits %d", nod);
special characters %d", nos);

3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4

Output :

1 Enter any string : pritesh a taral from c4learn.com


2
3 Number of words 5
4 Number of spaces 4
5 Number of vowels 9
6 Number of digits 1
7 Number of special characters 5

Explanation of Program :
Now we are going to accept string using gets(). We are not using scanf() to accept string
because scanf() will accept string only upto whitespace.

1 printf("Enter any string:");

2 gets(str);

We are accepting string using character pointer. Now we are checking each character
using character pointer and in each loop we are incrementing the character pointer.

1 while (*str != '\0') {


2 ------3 ------4}

Whenever first space is encountered then number of space counter is incremented by


one.

1 if
2
3
4}
5
6
7}

(*str == ' ') {


pos = high; // counting number of blank spaces.
++nob;
else if (pos == high) {
pos = low; // counting number of words.
++now;

In the if block we are checking that whether our pointer position is within the word or
outside the word.

#include <stdlib.h>
#include <stdio.h>
int countWords(FILE *f){
int count = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
if (ch == '\n')
count++;
}
return count;

}
int main(void){

int wordCount = 0;
FILE *rFile = fopen("american0.txt", "r");
wordCount += countWords(rFile);
printf("%d", wordCount);
return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy