Answer to Question #182984 in C++ for RAMAKRISHNA

Question #182984

Write program to input a multi-word string from keyboard and write this input string along

with first letter of each of its word into a file “initials.txt”. Data already present in the file

initials.txt must be preseved. Display suitable error message if the file opening/writing

operation fails.


1
Expert's answer
2021-04-19T19:11:48-0400
#include <bits/stdc++.h>
using namespace std;


int main()
{
    string str;
    int i=0;
    cout<<"Enter a string: ";
    getline(cin, str);
  


    fstream file;
    file.open("initials.txt", ios::app);
    
    if(file)
    {
        file<<str[0];
        while(str[i]!='\0')
        {
            if(str[i]==' ')
            file<<str[i+1];
            i++;
            
        }
        
    }
    else
    {
        cout<<"File not opened successfully!";
        
    }


   file.close();


return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS