The document describes a C++ program that takes a string as input, creates a text file, and analyzes the string to count the number of words, alphabets, vowels, consonants, uppercase letters, and lowercase letters. It uses functions like isalpha(), isupper(), islower() to classify each character and count the occurrences of each type of character or component. The output text file displays the results of the analysis for a sample input string.
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
C++ Program to Count Text File Elements
1. /* WAP to create a text file(.txt) and display number of words, alphabets, vowels and consonants and number
of lowercase and uppercase letters using the concept of DATA FILE HANDLING. */
#include<fstream.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
void main()
{
clrscr();
char a[80];
int words=0, upper_letters=0, lower_letters=0, alpha=0, vowels=0,consonants=0;
ofstream string("str.txt");
cout<<"nnEnter the string : ";
cin.getline(a,79);
string<<"The string is : "<<a<<"nn";
for(int i=0; i<strlen(a); i++)
{
if(a[i]==' ')
while(a[i]==' ')
{
i++;
}
if(isalnum(a[i]))
{
while(a[i]!=' ')
{
i++;
}
words++;
}
}
string<<"nnThe number of words in string are : "<<words;
string<<"nnnAlphabets in string are : nn";
for(i=0; i<strlen(a); i++)
{
if(isalpha(a[i]))
{
alpha++;
string<<a[i]<<" ";
}
2. }
string<<"nnTotal number of alphabets in string => "<<alpha;
string<<"nnnUppercase letters in string are : nn";
for(i=0; i<strlen(a); i++)
{
if(isupper(a[i]))
{
upper_letters++;
string<<a[i]<<" ";
}
}
string<<"nnTotal number of uppercase letters in string => "<<upper_letters;
string<<"nnnLowercase letters in string are : nn";
for(i=0; i<strlen(a); i++)
{
if(islower(a[i]))
{
lower_letters++;
string<<a[i]<<" ";
}
}
string<<"nnTotal number of Lowercase letters in string => "<<lower_letters;
string<<"nnnVowels in string are : nn";
for(i=0; i<strlen(a); i++)
{
if(isalpha(a[i]))
{
if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=='u'||a[i]=='U')
{
vowels++;
string<<a[i]<<" ";
}
}
}
string<<"nnTotal number of vowels in string => "<<vowels;
string<<"nnnConsonants in string are : nn";
for(i=0; i<strlen(a); i++)
{
if(isalpha(a[i]))
{
if(a[i]!='a'&&a[i]!='A'&&(a[i]!='e'&&a[i]!='E'&&a[i]!='i'&&a[i]!='I'&&(a[i]!='o'&&a[i]!='O')&&a[i]!='u'&&a[i]!='U')
{
4. Output :
Enter the string: Rahul Verma
Output of created textfile:
The string is: Rahul Verma
The numberof wordsinstringare : 2
Alphabetsinstringare :
R a h u l V e r m a
Total numberof alphabetsinstring=> 10
Uppercase lettersinstringare :
R V
Total numberof uppercase lettersinstring=>2
Lowercase lettersinstringare :
a h u l e r m a
Total numberof Lowercase lettersinstring=>8
Vowels instringare :
a u e a
Total numberof vowelsinstring=> 4
Consonantsinstringare :
R h l V r m
Total numberof vowelsinstring=> 6