Write a c program that can find the number of words that con

Write a c++ program that can find the number of \"words\" that contain only letters in a large textfile (e.g. dffdesfd is a word). Here, we define a word as a sequence of characters delimited a white space (that is, any number of spaces, new lines, tabs, etc).

Solution

// C++ code open file count number of words containing only letters

#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <stdlib.h>
#include <vector>

using namespace std;

bool checkletterWord( string word)
{
bool letterWord = true;
int i=0;
while (i < word.size())
{
if (!isalpha(word[i]))
{
letterWord = false;
break;
}
  
i++;
}

return letterWord;
}

int main ()
{
string word;
int count = 0;
ifstream inputFile (\"input.txt\");

if (inputFile.is_open())
{
while ( true )
{
if(inputFile.eof())
break;

inputFile >> word;
if (checkletterWord(word) == true)
{
count++;
}
  
}
inputFile.close();
}

else cout << \"Unable to open file\";

cout << \"Number of words that contain only letters: \" << count << endl;

return 0;
}

/*
input.txt
Elmer Lod45ging is xyz92
Elmer Conference cmp56dr
Daffy Din2ner cmp56dr
Daffy Conf33erence cmp56pqq
Mickey Dinner cmp56pqq
Mickey Conference cmp56er

output

Number of words that contain only letters: 10


*/

Write a c++ program that can find the number of \
Write a c++ program that can find the number of \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site