SOURCE FILE: 06_wordSubst.cpp
/*
This program counts the contiguous strings ('words') contained in a file
called myTextFile. It writes versions of tne words to the screen and to a
file called words+counts. The versions written are obtained by changing
each s to an f, and each S to an F. Each word is written by itself on a
numbered line.
*/
#include <iostream>
#include <fstream>
using namespace std ;
void subst (char word[]) ;
// changes each s in word to f (upper case too)
int main (void)
{
ifstream wordFile ;
wordFile.open("myTextFile") ;
if (wordFile.fail())
{
cout << "FAILED TO OPEN FILE: " << "myTextFile" << endl ;
exit(1) ;
}
ofstream resultsFile ;
resultsFile.open("words+counts");
if (resultsFile.fail())
{
cout << "FAILED TO OPEN FILE: " << "words+counts" << endl ;
exit(1) ;
}
int wordCount = 0 ;
char nextWord[256] ;
while (wordFile >> nextWord)
{
wordCount++ ;
subst (nextWord) ;
cout << wordCount << " " << nextWord << endl ;
resultsFile << wordCount << " " << nextWord << endl ;
}
cout << "\nThere was a total of " << wordCount << " words.\n\n" ;
resultsFile << "\nThere was a total of " << wordCount << " words.\n\n" ;
wordFile.close() ;
resultsFile.close() ;
return 0;
}
void subst (char word[])
{
int pstn = 0 ;
while (word[pstn] != '\0')
{
if (word[pstn] == 's') word[pstn] = 'f' ;
else if (word[pstn] == 'S') word[pstn] = 'F' ;
pstn++ ;
}
}