Monday 10 June 2013

C++ Program to Concatenate two strings

/*WAP that has a class with character array as its data member.
One object should contain "Computer Scientists are" and another
should contain " creators of logic". Member function join() should
concatanate two strings by passing two objects as arguments. Display
the concatanated string through a member function. Use constructor
to allocate and intialize the data member. Make one function for
concatanation of two strings.*/

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

class concatanate{
char string[50];
   public:
    static count =1;
      concatanate(){ //constructor
      if (count == 1){
          strcpy(string, "Computer Scientists are");
            count++;
         }
         else{
          strcpy(string, " creators of logic.");
            count++;
         }
      }
      //function to concatanate strings
      void join(concatanate a, concatanate b){
      concatanate merged;
         strcat(a.string , b.string);//concatanate a and b strings
         strcpy(merged.string, a.string);
         display(merged);
      }
      //function to display concatanated string
      void display(concatanate a){
      cout<<"Concatanated String is:"<<endl<<endl;
         cout<<a.string;
      }
     
};

void main(){
clrscr();
   concatanate a,b;
   a.join(a,b);
   getch();
}

No comments:

Post a Comment

Was this post helpful? Ask any questions you have, I will try to answer them for you.