Write a program to process string data The input is on one l

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.

Solution

solution

#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

 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
 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site