c++ programs examples

C++ Program to concatenate two strings and find the length of the two strings

c++ programs examples
/* 10. Write a program to concatenate two strings and find the length of
       two strings. */

 #include<iostream.h>
 #include<conio.h>
 #include<string.h>
 class string
 {
  private:
    char s[20];
    int len;
  public:
    string()	//default constructor
    {
     strcpy(s," ");
     len=0;
     }
    void input()
    {
     char a[10];
     cout<<endl<<"Enter a word less than or equal to 10 letters:"<<endl;
     cin>>a;
     strcpy(s,a);
     }
    string operator+(string s2)
    {
     string temp;
     strcpy(temp.s,s);
     strcat(temp.s,s2.s);
     return temp;
     }
    void length()
    {
     len=strlen(s);
     }
    void len_display()
    {
     cout<<endl<<"Length is =\t"<<len<<endl;
     }
    void st_display()
    {
      cout<<endl<<"After Concatenation =\t"<<s<<endl;
     }
};
void main()
{
 clrscr();
 string s1,s2,s3;
 s1.input();
 s2.input();
 s1.length();
 s2.length();
 s3=s1+s2;
 s3.st_display();
 s1.len_display();
 s2.len_display();
 getch();
 }

Leave a Reply

Your email address will not be published. Required fields are marked *