Sunday, 26 April 2015

U4P1

Define a class to represent a string with operations string length, compare and reverse. Show its use by writing main().

Code :
 #include<iostream.h>  
 #include<conio.h>  
 #include<String.h>  
 class string  
 {  
      char str1[10],str2[10];  
      public:  
      void getstr()  
      {  
           cout<<"\nEnter First String : ";  
           cin>>str1;  
           cout<<"Enter Second String : ";  
           cin>>str2;  
      }  
      void length()  
      {  
           cout<<"\nLength of First String is : "<<strlen(str1);  
           cout<<"\nLength of Second String is : "<<strlen(str2);  
      }  
      void reverse()  
      {  
           cout<<"\n\nReversed String of First String is : "<<strrev(str1);  
           cout<<"\nReversed String of Second String is : "<<strrev(str2);  
      }  
      void compare()  
      {  
           strrev(str1);  
           strrev(str2);  
           if(strcmp(str1,str2)>0)  
           cout<<"\n\nComparing both strings : "<<str1<<" is larger.";  
           else if(stricmp(str1,str2)<0)  
           cout<<"\n\nComparing both strings : "<<str2<<" is larger.";  
           else  
           cout<<"\n\nComparing both strings : Both Strings are same.";  
      }  
 };  
 void main()  
 {  
      clrscr();  
      cout<<"Program will show use of string funtions : strlen(), strrev() and strcmp().\n";  
      string newstr;  
      newstr.getstr();  
      newstr.length();  
      newstr.reverse();  
      newstr.compare();  
      getch();  
 }  

Output:

No comments:

Post a Comment