write a c++ program that can take an input file with string as for example "i saw 5 teddy bears"
and in a created output file create a new string to just change a digit to a word. To " I saw five teddy bears" . output should be located in an output file
thank you
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//fstream
fstream file;
string word, t, q, filename;
string digitToWord[] = {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};
//open file
file.open("input.txt");
ofstream myfile ("output.txt");
if (myfile.is_open())
{
//read file
while (file >> word)
{
if(isdigit(word[0]) && word[1] == '\0')
{
myfile<<digitToWord[word[0]-'0'-1]<<" ";
}
else
{
myfile<<word<<" ";
}
}
}
//close the file
file.close();
myfile.close();
return 0;
}
INPUT:
input.txt
i saw 5 teddy bears
OUTPUT:
output.txt
i saw five teddy bears
Get Answers For Free
Most questions answered within 1 hours.