Thursday, April 14, 2011

C++ Program list 2

1. reverse string program
2. Find out the day of a given  date in particular year
3. Print Pascal Triangle
4. To remove same word from the string 
5. we need to find any particular string and replace with convert string
  
reverse string program

/*


* This file contain reverse string program

* */

#include<iostream>
using namespace std;

template<class T>
class str
{
public:
char *p;
str();
void reverse();
void show();
};

template<class T>
str<T>::str()
{
p=new char[20];
cout<<" enter the string"<<endl;
cin>>p;
}

template<class T>
void str<T>::reverse()
{
char temp;
int count=0;
while(p[count]!='\0')
count++;
for(int i=0;i<count/2;i++)
{
temp =p[count-i-1];
p[count-i-1]=p[i];
p[i]=temp;
}
}
template<class T>
void str<T>::show()
{
cout<<p;
}
int main()
{
str<char>s;
s.reverse();
s.show();
return 0;
}

Find out the day of a given  date in particular year
#include<iostream>

#include<string>
#define BASEYEAR 100

using namespace std;
template<class T>
class leap
{
public:
leap();
int leap_year(int year);
long today;
void get_day();
};
template<class T>
leap<T>::leap()
{
today =0;
}
template<class T>
int leap<T>::leap_year(int year)
{
if((year%4==0 && year%100!=0)
(year%400==0))
return 1;

else
return 0;
}

template<class T>
void leap<T>::get_day()
{
int date;
cout<<" Enter date"<<endl;
cin>>date;
int month;
cout<<" Enter month"<<endl;
cin>>month;
int year;
cout<<"Enter year"<<endl;
cin>>year;
string day[]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
int day_of_month[]= {31,28,31,30,31,30,31,31,30,31,30,30};

for(int i=BASEYEAR;i<year-1;i++)
{
if(leap_year(i))
today=today+366;
else
today=today+365;
}

if(leap_year(year))
day_of_month[1]=29;

for(int i=0;i<month-1;i++)
today =today+day_of_month[i];

today+=date;
cout<<"today--->"<<today<<endl;
int wday =today%7;

cout<<" The day is ---->"<<day[wday];
}

Print Pascal Triangle


* Print Pascal Triangle


*

* 1

* 1 1

* 1 2 1

* 1 3 3 1

*

* */

#include<iostream>
using namespace std;
int main()
{
int a[20];
a[0]=1;
int k;
for(int i=0;i<10;i++)
{
int y=0;
for(int j=10;j>=i;j--)
cout<<" ";

for(k=0;k<=i;k++)
{
int p=y+a[k];
y=a[k];
a[k]=p;
cout<<p<<" ";
}

a[k]=0;
cout<<endl;
}
return 0;
}

To remove same word from the string
 
#include<iostream>

using namespace std;

char* word(char *p)
{
char *filter =new char[20];
int count=0;
int i=0;
while(p[count]!='\0')
count++;

for(i=0;p[i]!='\0';i++)
{
for(int j=i+1;p[j]!='\0';j++)
{
if(p[i]==p[j])
{
p[j]=p[j+1];
i=0;
cout<<" inside if block"<<endl;
}
}
}
p[i]='\0';
return p;
}
int main()
{
char *w=new char[20];
cout<<"enter the word"<<endl;
cin>>w;
cout<<word(w);
return 0;
}

/*

* we need to find any particular string and replace with convert string
* Ex.
* hi ss
* abhishek --->ab(hi)shek----->abssshek
*
* */

#include<iostream>

using namespace std;
char *change(char *p,char *find,char *replace)
{
int find_length=0;
while(find[find_length]!='\0')
find_length++;

int replace_length=0;
while(replace[replace_length]!='\0')
replace_length++;

for(int i=0;p[i]!='\0';i++)
{
for(int j=0;p[j]!='\0';j++)
{
if(p[i]==p[j])
{
int k=i;
int j=0;
while(find[j]!='\0')
{
if(p[k++]!=find[j])
break;
j++;
}
if(j==find_length)
{
int re =0;
//if the replace length are same
if(find_length==replace_length)
{
while(replace[re]!='\0')
{
p[i++]=replace[re++];
cout<<p[i]<<endl;
cout<<i<<endl;
}
}
//if the replace lenth are less
if(find_length>replace_length)
{
while(replace[re]!='\0')
p[i++]=replace[re++];

while(p[i]!='\0')
{
p[i]=p[i+2];
i++;
}
}
}
}
}
}
return p;
}
int main()
{
char *p=new char[40];
cout<<"Enter the string"<<endl;
cin>>p;
char *find =new char[40];

cout<<" Enter the find string"<<endl;
cin>>find;
char *replace =new char[40];

cout<<"Enter the replace"<<endl;
cin>>replace;


cout<<" final string "<<change(p,find,replace)<<endl;
return 0;
}





 
 



int main()

{

leap<int> l;

l.get_day();



return 0;

}

2 comments: