Write a program to permanently store the data entered by the user. When user is done with Input, then print the confirmation message as "Content saved successfully" along with the number of character stored.
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int main()
{
string str = "";
int count = 0;
cout<<"Enter the string:\n";
getline(cin, str);
char arr[str.length() + 1];
strcpy(arr, str.c_str());
//String to char array conversion
for (int i = 0; i < str.length(); i++)
{
//Avoid counting empty characters
if(arr[i] != ' ')
{
count ++;
}
}
// Create and open a text file
ofstream file("save.txt");
// Write string to the file
file << str;
// Close the file
file.close();
cout <<"Content saved successfully "<<endl;
cout<<"The number of characters are: "<<count<<endl;
return 0;
}
Comments
Leave a comment