u

VB Projects

Get all Visual Basic Projects here..

ASP Dot Net Projects

Get all Asp Dot Net Projects.

C++ Projects and Programs

Get all C++ Projects and Programs here...

PHP Projects

Get all PHP Projets here..

JAVA Projects

Get All JAVA Projects here...

Friday, 14 September 2012

C++ Program to input any string and print the number of words

C++ Program to input any string and print the number of words

 #include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
main()
{
char a,name[50];
int l=0,c=0,word=0;
cout<<"Enter any string "<<endl;
gets(name);
l = strlen(name);
cout<<endl;
for(int i=0;i<l;i++)
{
a=name[i];
if (a==' ') c = c + 1;
}
word = c + 1;
cout<<"The total number of words in the string is "<<word;
cout<<endl;
getch();
return 0;

}

C++ Program to delete the first common word in two strings and display the strings without the first common words

C++ Program to delete  the first common word in two strings and display the strings without the first common words

#include <conio.h>
#include <stdio.h>
#include <iostream.h>

char string1[25] = {'\0'};
char string2[25] = {'\0'};
char temp[25] = {'\0'};

int start = -1;
int end = -1;

int start2 = 0;
int end2 = 0;


/*GET THE INPUT FOR THE STRINGS*/
void get_input()
{
    cout << "Enter the first string : ";
    gets(string1);

    cout << "Enter the second string : ";
    gets(string2);
}


/*SET INDEXES FOR ALIAS WORD*/
void extract_word()
{
    while( string2[end2] != ' ' )
        end2++;
}


/*COMPARE THE TWO WORDS*/
int compare()
{
    int n;

    for(n=start; n<end; n++)
    {
        if( string2[n-start] != string1[n] )
        {
            return 0;
        }
    }
    return 1;
}


/*FIND THE ALIAS WORD IN THE STRINGS*/
void find_alias()
{
    int same = 0;

    while( (same != 1) && (string1[end] != '\0') )
    {
        end++;
        start = end;

        while( (string1[end] != ' ') && (string1[end] != '\0') )
            end++;

        same = compare();
    }

}

/*REPLACE THE WORD*/
void replace_alias()
{
    int counter = end;

    /*find the end of the string*/
    while( string1[counter] != '\0' )
        counter++;

    /*copy the remaining string to temp*/
    for(int i=end; i<counter; i++)
    {
        temp[i-end] = string1[i];
    }

    counter = start;

    /*nullify the remaining string*/
    while( string1[counter] != '\0' )
    {
        string1[counter] = '\0';
        counter++;
    }

    start2 = end2;
    counter = start-1;

    /*replace the alias by the second string*/
    while( string2[end2] != '\0' )
    {
        string1[counter] = string2[end2];
        end2++;
        counter++;
    }

    int k = 0;

    /*copy the remaining string back from temp*/
    while( temp[k] != '\0' )
    {
        string1[counter] = temp[k];
        k++;
        counter++;
    }
}


void main()
{
    clrscr();

    get_input();
    extract_word();
    find_alias();
    replace_alias();

    cout << string1 << '\n';

    getch();
} 

C++ Program to implement a Recursive Function

C++ Program to implement a Recursive Function


 #include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
void recurfunc(int start,int stop,char* buff);
void main()


    {   clrscr();
    cout << "enter how many times to loop: ";
    char buff[100];
    cin >> buff;
    int num;
    num = atoi(buff);
    if(num >= 1)


        {
            recurfunc(0,num," this is a recursive function");
        }
        else


        {
            cout << "sorry you didnt enter valid data" << endl;
            return;
        }
        getch();
    }
    void recurfunc(int start,int stop,char* buff)


        {
        if(start < stop)


            {
                start++;
                cout << "\n" << buff << "\n";
                recurfunc(start,stop,buff);
            }
            else


            {
                return;
            }
        }

C++ Program to Print out a diamond of any size less than 12

C++ Program to  Print out a diamond of any size less than 12


#include <iostream.h>
#include <conio.h>
const int Center = 20;

void printnc(char c, int n){
    for (int lcv=0; lcv < n; lcv++)
    {
    cout << c;
    }
}

// Print out a triangle of size n
void printTriangle(int n){

   for (int i=0; i < n; i++){
       printnc(' ', Center - i -1);
       printnc('X', 2*i+1);
       cout << endl;
   }
}

// Print reverse of triangle printed by printTriangle
void printRTriangle(int n){

   for (int i=0; i < n; i++){
       printnc(' ', Center - n + i);
       printnc('X', 2*n-2*i-1);
       cout << endl;
   }
}


// Get dimension of triangle from user
void main(void){
   int m;

   cout << "Enter an integer m [0 < m < 12]: "; endl;
   cin >> m;
   while ((m > 0) && (m < 12)){
      printTriangle(m);
      printRTriangle(m-1);
      cout << "Enter an integer m [0 < m < 12]: ";
      cin >> m;
   }
}

C++ Program to print out a checkered board of size n

C++ Program to print out a checkered board of size n

 #include <iostream.h>
#include <conio.h>

const int cellSize = 4;
void printnc(char c, int n){
   for(int k = 0; k<n; ++k)
      cout << c;
}

void printBWcells(void){
   printnc('.', cellSize);
   printnc(' ', cellSize);
}

void printWBcells(void){
   printnc(' ', cellSize);
   printnc('.', cellSize);
}

void printBoard(int n){
   printnc('*', 2+2*n*cellSize);
   cout << endl;
   for(int k = 0; k<n; ++k){
      for(int h = 0; h < cellSize; ++h){
     cout << '*';
     for(int m=0; m<n; ++m){
        if((k%2)==0)
          printBWcells();
        else
          printWBcells();
     }
     cout << '*' << endl;
      }
   }
   printnc('*', 2+2*n*cellSize);
   cout << endl;
}

void main(void){
   int n;
   clrscr();

   cout << "Enter size of board: ";
   cin >> n;
   clrscr();

   printBoard(n);
   cout<<endl;
   cout<<"Press any key to continue";
   getch();
}

C++ Program to play a puzzle

C++ Program to play a puzzle


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<stdio.h>
getkey1()
{union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void main2()
{int gd=DETECT,gm,i,j,n=0,m,q=0,v=0,k;
char *a[16]={"9","3","2","4","7","6","5","8","1","11" ,"13","12","10","14"," " ,"15"}

,*b[16]={"1","2","3","4","5","6","7","8","9","10" ,"11","12","13","14","15" ," "},*t;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(3);
outtextxy(260,100," PUZZLE");
setcolor(4);
setcolor(WHITE);
delay(2000);
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
do{{ setcolor(3);
outtextxy(100,170,"YOU HAVE TO SOLVE IT WITHIN 400 MOVES");

setcolor(8);
outtextxy(280,130,"LEVEL: 2 ");
setcolor(9);
outtextxy(100,150,"YOU HAVE SPENT ");
//outtextxy(220,150,v);
gotoxy(28,10);
cout<<v;
outtextxy(240,150," MOVES ");
setcolor(2);
outtextxy(100,330,"USE ARROW KEYS FOR DIRECTION");
setcolor(4);
outtextxy(200,350,"PRESS '* ' TO QUIT AT ANY TIME");
setcolor(5);
outtextxy(400,50,"ARRANGE LIKE THIS");
m=0;
for(i=425;i<=500;i+=25)
for(j=60;j<=135;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,b[m]);
m++;
}
}
setcolor(WHITE);
if((*a[0]=='1')&&(*a[1]=='2')&&(*a[2]=='3')&&(*a[3]=='4')&&(*a[4]=='5')&&(*a[5]=='6')&&(*a[6]=='7')&&(*a[7]=='8')
&& (*a[8]=='9')&&(strcmp(a[9],"10")==0)&&(strcmp(a[10],"11")==0)&&(strcmp(a[11],"12")==0)&&(strcmp(a[12],"13")==0)&&(strcmp(a[13],"14")==0)&&(strcmp(a[14],"15")==0))

{setcolor(5);
outtextxy(200,350,"CONGRATULATION! YOU WIN");
getch();
break;
}
k=getkey1();
sound(500);
delay(100);
nosound();
switch(k)
{case 75:{clearviewport();
n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n<=11)&&(q==0))
{t=a[n];
a[n]=a[n+4];
a[n+4]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
q=0; break;
}
case 77:{clearviewport();
n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n>3)&&(q==0))
{t=a[n];
a[n]=a[n-4];
a[n-4]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
q=0;break;
}
case 80: {clearviewport();
n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&((n!=0)&&(n!=4)&&(n!=8)&&(n!=12))&&(q==0))
{t=a[n];
a[n]=a[n-1];
a[n-1]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
q=0;break;
}
case 72:{clearviewport();
n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n!=3)&&(n!=7)&&(n!=11)&&(n!=15)&&(q==0))
{t=a[n];
a[n]=a[n+1];
a[n+1]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
q=0; break;
}
}}while((k!=55)&&(v<=400));
if(v>400) {outtextxy(200,350,"SORRY! YOUR MOVE IS OVER");
getch();
}
closegraph();
}

//puzz6.cpp ends here.





#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<stdio.h>
getkey()
{union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void main()
{int gd=DETECT,gm,i,j,n=0,m,q=0,v=0,k;
char *a[9]={"7","2","5","4","3","6","1"," ","8"}

,*b[9]={"1","2","3","4","5","6","7","8"," "},*t;
initgraph(&gd,&gm,"..//bgi");
setcolor(3);
outtextxy(260,100," PUZZLE");
setcolor(4);
setcolor(WHITE);
delay(2000);
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
do{{ setcolor(3);
outtextxy(100,170,"YOU HAVE TO SOLVE IT WITHIN 200 MOVES");
setcolor(1);
outtextxy(210,130,"LEVEL : 1");
setcolor(9);
outtextxy(100,150,"YOU HAVE SPENT ");
//outtextxy(220,150,v);
gotoxy(28,10);
cout<<v;
outtextxy(240,150," MOVES ");

setcolor(2);
outtextxy(100,330,"USE ARROW KEYS FOR DIRECTION");
setcolor(4);
outtextxy(100,350,"PRESS '* ' TO QUIT AT ANY TIME");
setcolor(5);
outtextxy(400,50,"ARRANGE LIKE THIS");
m=0;
for(i=425;i<=475;i+=25)
for(j=60;j<=110;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,b[m]);
m++;
}
}
setcolor(WHITE);
if((*a[0]=='1')&&(*a[1]=='2')&&(*a[2]=='3')&&(*a[3]=='4')&&(*a[4]=='5')&&(*a[5]=='6')&&(*a[6]=='7')&&(*a[7]=='8'))

{setcolor(5);

outtextxy(180,380,"CONGRATULATION! YOU FINISHED LEVEL 1");
getch();
clearviewport();
main2();
break;
}
k=getkey();
sound(300);
delay(100);
nosound();
switch(k)
{case 75:{clearviewport();
n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n<=5)&&(q==0))
{t=a[n];
a[n]=a[n+3];
a[n+3]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+10,j+10,a[n]);
n++;

}
q=0; break;
}
case 77:{clearviewport();
n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n>=2)&&(q==0))
{t=a[n];
a[n]=a[n-3];
a[n-3]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+10,j+10,a[n]);
n++;
}
q=0;break;
}
case 80: {clearviewport();
n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&((n!=0)&&(n!=3)&&(n!=6))&&(q==0))
{t=a[n];
a[n]=a[n-1];
a[n-1]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+10,j+10,a[n]);
n++;
}
q=0;break;
}
case 72:{clearviewport();
n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&((n!=2)&&(n!=5)&&(n!=8))&&(q==0))
{t=a[n];
a[n]=a[n+1];
a[n+1]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+10,j+10,a[n]);
n++;
}
q=0; break;
}
}}while((k!=55)&&(v<=200));
if(v>200) {outtextxy(200,350,"SORRY! YOUR MOVE IS OVER");
delay(2000);
getch();
}
closegraph();
}

C++ Program for converting an int variable to floating point

C++ Program for converting an int variable to floating point

#include <iostream.h>
#include <stdlib.h>      //defines EXIT_SUCCESS etc
#include <conio.h>

int main( void )
{
  // declare variables here

    int x,y;
    float quotient;
    cout <<"enter an integer x="; cin >> x;
    cout <<"enter an integer y="; cin >> y;

    quotient= x / y;
    cout<< "x / y is " << quotient << endl;

    quotient= float(x) / y;
    cout<< "float(x) / y is " << quotient << endl;
    getch();


  return EXIT_SUCCESS;
}


Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More