problem 39;
word processing software/project using c++ with file
by ujjal roy
//problem 39
#include<bits/stdc++.h>
#include<fstream>
#include<conio.h>
using namespace std;
int line_number=0,char_number=0,word_number=0;
void display()
{
string s;
system("cls");
ifstream in;
in.open("output.txt");
in.seekg(0);
while(!in.eof())
{
char ch;
in.get(ch);
s+=ch;
}
in.close();
cout<<s;
}
void text()
{
system("cls");
ofstream out;
out.open("output.txt");
char ch;
out.seekp(0);
cin>>ch;
while(ch!='$')
{
out<<ch;
cin.get(ch);
// if(ch=='$')break;
}
out.close();
}
void frequency()
{
system("cls");
ifstream in;
in.open("output.txt");
in.seekg(0);
while(!in.eof())
{
char ch;
in.get(ch);
char_number++;
if(ch==' '||ch=='\n')word_number++;
if(ch=='\n')line_number++;
}
in.close();
char_number--;
if(char_number>0)
{line_number++;
word_number++;}
cout<<"line number : "<<line_number<<endl;
cout<<"word number : "<<word_number<<endl;
cout<<"character number : "<<char_number<<endl;
}
void find_pattern(string p)
{
//create by ujjal roy
string s;
system("cls");
ifstream in;
in.open("output.txt");
in.seekg(0);
while(!in.eof())
{
char ch;
in.get(ch);
s+=ch;
}
in.close();
for(int i=0;i<s.size();i++)
{
int j=0;
if(s[i]==p[0])
{
for(j==0;j<p.size();j++)
{
if(s[i+j]!=p[j])break;
}
}
if(j==p.size())
{
cout<<"Found\n";
return;
}
}
cout<<"Not found\n";
}
void insertt(int po,string text)
{
string s,ans;
system("cls");
ifstream in;
in.open("output.txt");
in.seekg(0);
while(!in.eof())
{
char ch;
in.get(ch);
s+=ch;
}
in.close();
for(int i=0;i<s.size();i++)
{
if(i==po-1)
{
for(int j=0;j<text.size();j++)
{
ans+=text[j];
}
}
ans+=s[i];
}
ofstream out;
out.open("output.txt");
out.seekp(0);
out<<ans;
out.close();
display();
getch();
}
void deletee(int po,int le)
{
string s,ans;
system("cls");
ifstream in;
in.open("output.txt");
in.seekg(0);
while(!in.eof())
{
char ch;
in.get(ch);
s+=ch;
}
in.close();
po--;
for(int i=0;i<po;i++)
{
ans+=s[i]; //ujjal
}
for(int i=po+le;i<s.size();i++)ans+=s[i];
ofstream out;
out.open("output.txt");
out.seekp(0);
out<<ans;
out.close();
display();
getch();
}
void append(string text)
{
string s,ans;
system("cls");
ifstream in;
in.open("output.txt");
in.seekg(0);
while(!in.eof())
{
char ch;
in.get(ch);
s+=ch;
}
in.close();
s+=text;
ofstream out;
out.open("output.txt");
out.seekp(0);
out<<s;
out.close();
display();
getch();
}
void replacee(string pattern1,string pattern2)
{
string s,ans;
system("cls");
ifstream in;
in.open("output.txt");
in.seekg(0);
while(!in.eof())
{
char ch;
in.get(ch);
s+=ch;
}
in.close();
bool flag=false;
int pattern_index=0;
for(int i=0;i<s.size();i++)
{
int j=0;
if(s[i]==pattern1[0])
{
for(j==0;j<pattern1.size();j++)
{
if(s[i+j]!=pattern1[j])break;
}
}
if(j==pattern1.size())
{
pattern_index=i;
flag=true;
break;
}
}
if(flag)
{
int le=pattern1.size();
for(int i=0;i<pattern_index;i++)//hi ujjal how are you??index=3
{
ans+=s[i]; //ujjal
}
// cout<<ans<<endl;
// cout<<pattern_index<<endl;
for(int i=pattern_index+le;i<s.size();i++)ans+=s[i];
//cout<<ans<<endl;this is ok no mistake found
ofstream out;
out.open("output.txt");
out.seekp(0);
out<<ans;
out.close();
insertt(pattern_index+1,pattern2);
}
else
cout<<"Pattern Not found\n";
}
int main()
{
cout<<"a.write text\n";
cout<<"b.Number of line , character,word\n";
cout<<"c.find pattern\n";
cout<<"d.insert,delete,append\n";
cout<<"e.replace a string\n\n";
cout<<"Enter choose : ";
char ch;
cin>>ch;
if(ch=='a'||ch=='A')
{
text();
}
if(ch=='B'||ch=='b')
{
frequency();
}
if(ch=='C'||ch=='c')
{
string pattern;
cout<<"Enter pattern : ";
cin>>pattern;
find_pattern(pattern);
}
if(ch=='d'||ch=='D')
{
int n;
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.append\n";
cin>>n;
if(n==1)
{
int num;
string text;
cout<<"Enter position and text\n";
cin>>num>>text;
insertt(num,text);
}
if(n==2)
{
int p,l;
cout<<"Enter position and lenhth : ";
cin>>p>>l;
deletee(p,l);
}
if(n==3)
{
cout<<"Enter text : ";
string s;
cin>>s;
//getline(cin,s);
append(s);
}
else cout<<"Wrong choose \n";
}
if(ch=='e')
{
string p1,p2;
cout<<"Enter pattern1 : ";
cin>>p1;
cout<<"Enter pattern2 : ";
cin>>p2;
replacee(p1,p2);
}
else
{
cout<<"Wrong choose\n";
}
return 0;
}
0 Comments