please write in the c language parsing practice input file is input8.txt Functio
ID: 3860894 • Letter: P
Question
please write in the c language
parsing practice
input file is input8.txt
Function Prototypes:
bool isATime(char* word)
bool isADate(char* word)
bool is APhoneNumber(char* word)
each has the same definition where the input is a string and it returns a true or false
RULES FOR FUNCTION
DATES:
x/xx/xxxx x/x/xxxx xx/x/xxxx xx/xx/xxxx
numbers only
valid date (example 2/30 is invalid)
TIME
xx:xxam x:xxam xx:xx pm x:xxpm
only numbers in place of x's
valid hours only (1-12) and min (0-59)
PHONE NUMBERS
xxx-xxx-xxxx
only numbers
SAMPLE OUTPUT
you will be given a file filled with tokens.
input8.txt file contains
OUTPUT:
TIME FOUND:5:54pm
TIME FOUND:10:30am
DATE FOUND:11/4/2000
PHONE NUMBER FOUND: 123-456-7890
Explanation / Answer
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void func(const string& word)
{
//set field width
cout.width(30);
cout << left << word << " " << word.size() << endl;
}
int main(int argc, char* argv[])
{
ifstream ifs("F:\tmp\test.txt");
if(ifs.fail())
{
cout << "fail to open file" << endl;
return -1;
}
_Ctypevec ct = _Getctype();
for(char ch = 0; ch < SCHAR_MAX; ch++)
{
//set all punctuations as field separator of extraction
if(ispunct(ch))
{
(const_cast<short*>(ct._Table))[ch] = ctype<char>::space;
}
}
//change the default locale object of ifstream
ifs.imbue(locale(ifs.getloc(), new ctype<char>(ct._Table)));
string word;
while(ifs >> word)
{
func(word);
}
ifs.close();
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void printString( const string & str ) { // ignore the & for now, you'll get to it later.
cout << str << " : " << str.size() << endl;
}
int main() {
ifstream fin("your-file-name.txt");
if (!fin) {
cout << "Could not open file" << endl;
return 1;
}
string word; // You only need one word at a time.
while( fin >> word ) {
printString(word);
}
fin.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.