Docsity

C++ Program for Processing and Shuffling Sentences, Exercises of Object Oriented Programming

Toggle sidebar
bg1
#include<iostream>
using namespace std;
class line
{
private:
char sentence[100];
public:
line* next;
int i;
char* access_rec()
{
return sentence;
}
};
void makeword(char q[100],char tmp[50])
{//q stores whole sentence and tmp stores first word
int i,j=0,p=0,m=0;
for(p=0;p<50;p++)
{
tmp[p]='\0';
}
for(i=0;i<100;i++)
{
if(q[i]==' '&&j==0)//when space detected
{
for(p=0;p<i;p++)
{
tmp[p]=q[p];
}
j++;
}
}
j=strlen(q)-(p+1);//p has an adress of place at which break occurs
for(i=0;i<j;i++)
{
q[i]=q[i+p+1];
}
j=0;
m=strlen(q)-(p+1);
for(i=m;i<strlen(q);i++)
{
if(j==0)
{
q[i]=' ';//adds space in end of sentence
j++;
}
else
{
q[i]='\0';//adds null after space at end of sentence
}
}
}
docsity.com
pf3
pf4

Partial preview of the text

Download C++ Program for Processing and Shuffling Sentences and more Exercises Object Oriented Programming in PDF only on Docsity!

#include using namespace std; class line { private: char sentence[100]; public: line* next; int i; char* access_rec() { return sentence; } };

void makeword(char q[100],char tmp[50]) {//q stores whole sentence and tmp stores first word int i,j=0,p=0,m=0; for(p=0;p<50;p++) { tmp[p]='\0'; } for(i=0;i<100;i++) { if(q[i]==' '&&j==0)//when space detected { for(p=0;p<i;p++) { tmp[p]=q[p]; } j++; } } j=strlen(q)-(p+1);//p has an adress of place at which break occurs for(i=0;i<j;i++) { q[i]=q[i+p+1]; } j=0; m=strlen(q)-(p+1);

for(i=m;i<strlen(q);i++) { if(j==0) { q[i]=' ';//adds space in end of sentence j++; } else { q[i]='\0';//adds null after space at end of sentence } } }

void main() { int counter=0,var_counter=0; line* asd; line* first=NULL; char tmp[100];//temporary array to store input cout<<"enter sentences\n"; while(var_counter!=2)//loop for taking input { cin.getline(tmp,100); cin.sync(); asd=new line;//link list.... strcpy(asd->access_rec(),tmp); asd->next=first; first=asd; counter++; cout<<"PRESS 1 FOR NEXT AND 2 FOR END\n"; cin>>var_counter; cin.sync(); if(var_counter!=1 && var_counter!=2)//input check { while(var_counter!=1 && var_counter!=2) { cout<<"WRONG INPUT\n"; cout<<"PRESS 1 FOR NEXT AND 2 FOR END\n"; cin>>var_counter; cin.sync(); } } //input check ends here }//loop for taking input ends here char** ult;//2d pointer to store different sentences in straight manner ult=new char[counter]; for(var_counter=0;var_counter<counter;var_counter++) { ult[var_counter]=new char[100]; } line temp=first; var_counter=counter-1; while(temp != NULL) //this loop makes linkked list straight { strcpy(ult[var_counter],temp->access_rec()); temp=temp->next; var_counter--; } var_counter=counter-1; char** v;//double pointer to store first word of stream v=new char*[counter]; for(var_counter=0;var_counter<counter;var_counter++) {

for(int s=0;s<counter;s++)//Free the Memory { delete[] v[s]; //delete[] ult[s]; it does not work so please check it out why while at the same time v is deleting successfully } //delete [] ult; delete [] v; }