Professional Documents
Culture Documents
Technology
Grand Trunk Road, Barnala - Amritsar
Bypass Road, Jalandhar , Punjab - 144011
'
LAB FILE
Code :
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream outFile("lab1.txt");
cout << "Enter lines of text (type 'exit' to stop):\n";
string input;
while (true)
{
getline(cin, input);
if (input == "exit")
break;
outFile << input << endl;
}
ifstream fp("lab1.txt");
if(!fp.is_open())
{
cout<<"Not opened";
return 1;
}
string s,b;
// First letter of the words
while(getline(fp,s))
{
b+=s[0];
for(int i=0;i<s.length();i++)
{
if(s[i]==' ')
{
b+=s[i+1];
}
}
}
cout<<"the decoded message is : ";
cout<<b;
return 0;
}
Output :
2. Second logic: The actual message is made of the first letters of all the even-length
words and the last letters of all the odd letter words in sent message.
Code
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream outFile("lab1.txt");
cout << "Enter lines of text (type 'exit' to stop):\n";
string input;
while (true)
{
getline(cin, input);
if (input == "exit")
break;
outFile << input << endl;
}
ifstream fp("lab1.txt");
if(!fp.is_open())
{
cout<<"Not opened";
return 1;
}
string s,b;
while(getline(fp,s))
{
int k=0;
char f;
for(int i=0;i<s.length();i++)
{
if(k==0)
{
f=s[i];
}
k++;
if(s[i]==' ')
{
k--;
if(k%2==0)
{
b+=f;
k=0;
}
else
{
b+=s[i-1];
k=0;
}
}
}
cout<<endl;
cout<<"the decoded message is : ";
cout<<b;
return 0;
Output:
3. Third logic: The actual message is made of the letters just following the punctuation
marks (,.!?) in your sent message.
Code
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream outFile("lab1.txt");
cout << "Enter lines of text (type 'exit' to stop):\n";
string input;
while (true)
{
getline(cin, input);
if (input == "exit")
break;
outFile << input << endl;
}
ifstream fp("lab1.txt");
if(!fp.is_open())
{cout<<"Not opened";
return 1;
}
string s,b;
while(getline(fp,s))
{
for(int i=0;i<s.length();i++)
{
if(s[i]==',' || s[i]=='.' || s[i]=='!' ||s[i]=='$')
{
b+=s[++i];
}
if(s[i]=='?')
{
b+=" ";
}
}
}
cout<<endl;
cout<<"The decoded message is : ";
cout<<b;
return 0;
}
Output :
4. Fourth logic: The actual message is made of the middle elements in the message
sent.
Code :
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream outFile("lab1.txt");
cout << "Enter lines of text (type 'exit' to stop):\n";
string input;
while (true)
{
getline(cin, input);
if (input == "exit")
break;
outFile << input << endl;
}
ifstream fp("lab1.txt");
if(!fp.is_open())
{cout<<"Not opened";
return 1;
}
string s,b;
while(getline(fp,s))
{int k=0;
for(int i=0;i<s.length();i++)
{
k++;
if(s[i]==' ')
{
k--;
k=(k/2)+1;
b+=s[i-k];
k=0;
}
if(s[i]=='?')
{
b+=" ";
k=0;
}
}
}
cout<<endl;
cout<<"The decoded message is : ";
cout<<b;
Output :