I have to read lines from a text file and reformat them so that the first letter is uppercase and the rest are lowercase. I did that with no problem. also i needed to count the total words in the file, and did that easily.,but i also need to count the digits and punctuation marks in the file.For some reason i cant get it to work.

can anyone help with a good way to count the digits and puntuation marks( '.', '?', '!')

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>

using namespace std; //separated by exactly 1 blank.
void reformat(string&);
void getfilename(string,string&);
void printFile(string);
void word_count(int);
void line_count(int);

int main()
{
  char ch;
  ifstream input;
  ofstream output;
  string filename, word;
  int numWords = 0;
  int lines = 0;
  int digits = 0;
  int punct = 0;
  char next;
  getfilename("input",filename);
  input.open(filename.c_str());
  printFile(filename);
  input >> word;
  while (!input.eof())
    {
      reformat(word);
      cout << word;
      input.get(ch);
      if(ch == '\n')
        {
      cout << endl;
        }
      else
        cout << " ";

      numWords ++;
      input >> word;
        }

  word_count(numWords);
  line_count(lines);
  cout << "Digit count: " << digits << endl;
  cout << "Punctuation count: "<< punct << endl;
return 0;
}


void reformat(string& word)
{
  int wlen;
  wlen = word.length();
  word[0] = toupper(word[0]);
  for (int i=1; i<wlen; i++)
    word[i] = tolower(word[i]);
void printFile(string filename)
{
  cout << "Input file name: " << filename << endl;
  cout << endl;
}

void getfilename(string filetype,string& filename)
{

  cout << "Enter name of " << filetype << " file\n";
  cin >> filename;
}
void word_count(int numWords)
{
  cout << endl;
  cout << "Word count: " << numWords << endl;
}
void line_count(int lines)
{
  cout << "Sentence count: " << lines << endl;
}

Look up the functions isdigit() and ispunct() in ctype.h or cctype headers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.