Write a program to process string data. The input is on one line in an input fil
ID: 3791049 • Letter: W
Question
Write a program to process string data. The input is on one line in an input file, and has up to seven components with a colon (:) at the end of each component. The program separates each component into separate strings (without the colon), and outputs each separated string along with its length. Use an output file, with one line for each separated string. One input sample could be George: April May William: Johnny: Another input could be Red: Fuscia: Magenta: A third input example is (note internal spaces) Hello, there.: How are you?: I'm fine.: Let's go out to dinner. Follow Program Guidelines requirements.Explanation / Answer
#include<stdio.h>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main () {
string sentence;
ifstream myfile ("D:\example.txt");
if (myfile.is_open())
{
while ( getline (myfile,sentence) )
{
cout<<"the sentence with delimeters"<<endl;
cout << sentence << ' ';
std::string str =sentence;
std::string delim = ":";
size_t position = 0;
std::string token;
while ((position = str.find(delim)) != std::string::npos) {
token = str.substr(0, position);
std::cout << token << std::endl;
str.erase(0, position + delim.length());
}
std::cout << str << std::endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
output
the sentence with delimeters
george:william:john:abraham
george
william
john
abraham
the sentence with delimeters
kate:winslin
kate
winslin
the sentence with delimeters
hello there how: r u iam fine:
hello there how
r u iam fine
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.