Categories
Uncategorised

Text Files – Count Lines

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html /

/ Text File – Count number of lines starting with uppercase vowel in a text file /

#include <fstream.h>
#include <stdio.h>
#include <conio.h>

fstream obj(“text.txt”, ios::out);
char s[101], ans;
int ln = 0;

void count()
{
obj.open(“text.txt”, ios::in);
while ( !obj.eof() )
{
obj.getline(s, 101);
switch (s[0])
{
case ‘A’ :
case ‘E’ :
case ‘I’ :
case ‘O’ :
case ‘U’ : ++ln;
}
}
obj.close();
}

void main()
{
clrscr();
do
{
cout<<endl<<“Enter data (max 100 characters) : “;
gets(s);
obj<<s<<‘\n’;
cout<<“Do you wish to continue (y / n) : “;
cin>>ans;
} while (ans == ‘y’);
obj.close();
count();
cout<<endl”Number of lines in the file starting with an uppercase vowel is “<<ln;
getch();
}

/ Output */

Enter data (max 100 characters) : A quick brown fox jumped over the lazy dog
Do you wish to continue (y / n) : y
Enter data (max 100 characters) : Ouija Bored?
Do you wish to continue (y / n) : n

Number of lines in the file starting with an uppercase vowel is 2

Categories
Uncategorised

Text Files – Count Characters

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html /

/ Text Files – WAP to create text file and count number of words, vowels, special characters and digits /

#include <fstream.h>
#include <conio.h>
#include <stdio.h>
#include <ctype.h>

void main()
{
clrscr();
fstream obj(“text.txt”, ios::out);
char s[101], ans, ch;
int wd, vw, dg, sc;
wd = 1;
vw = dg = sc = 0;
do
{
cout<<endl<<“Enter data (max 100 characters) : “;
gets(s);
obj<<s<<‘\n’;
cout<<“Do you wish to continue (y / n) : “;
cin>>ans;
} while (ans == ‘y’);
obj.close();
obj.open(“text.txt”, ios::in);
while ( !obj.eof() )
{
ch = obj.get();
switch (ch)
{
case ‘A’ :
case ‘a’ :
case ‘E’ :
case ‘e’ :
case ‘I’ :
case ‘i’ :
case ‘O’ :
case ‘o’ :
case ‘U’ :
case ‘u’ : ++vw;
break;
default : if ( isdigit(ch) )
++dg;
if (ch == ‘ ‘)
++wd;
if ( !isalnum(ch) )
++sc;
}
}
cout<<endl<<“Statistics for given file”
<<endl<<“Number of words : “<<wd
<<endl<<“Number of vowels : “<<vw
<<endl<<“Number of digits : “<<dg
<<endl<<“Number of special characters : “<<sc;
getch();
}


/
Output */

Enter data (max 100 characters) : The Answer to Life, the Universe, and
Everything is 42.
Do you wish to continue (y / n) : n

Statistics for given file
Number of words : 10
Number of vowels : 16
Number of digits : 2
Number of special characters : 14