Different Operations On String


Program to implement Different Operations On String in C++

The Following Programs Reads A String From User And Displays
The Menu Consisting Of Different Operations On String And
Performs Accordingly





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

class String
{
public: char *s; char *s2;

/*
 Constructors
*/
public:
String()
{
s = "";
}
String(char *a)
{
s = a;
}
/*
 String Functions
*/
int mystrlen() //Length of String
{
int i;
for(i = 0; s[i] != '\0'; i++);
return i;
}
char *mystrcpy() //Copy given String
{
int i;
char *ob1 = new char[mystrlen()];
for(i = 0; (ob1[i] = s[i]) != '\0'; i++);
return ob1;
}
char *mystrcat(char *s1) //Concatenate given String with new String
{
int i, j;
String ob0(s1);
int l = mystrlen();
int k = ob0.mystrlen();
int m = k + l;
char *s2 = new char[m];
for(i = 0; (s2[i] = s[i]) != '\0'; i++);
for(j = 0; (s2[i] = s1[j]) != '\0'; j++, i++);
return s2;
}
char *mystrrev() //Reverse given String
{
int i, j, k;
char *s3 = new char[mystrlen()];
for(i = 0; (s3[i] = s[i]) != '\0'; i++);
for(j = 0, i--; i > j; i--, j++)
{
k = s3[j];
s3[j] = s3[i];
s3[i] = k;
}
return s3;
}
};
/*
  Main Function
*/
void main()
{
clrscr();
char *f;
char *n;
int s,l;
cout<<"\nEnter Your String::";
cin>>f;
String ob(f);
do
{
cout<<"\n\nSelect Your Option From Below::";
cout<<"\n1. Find Length.\n2. Copy String.\n3. Concatenate String.\n4. Reverse String.\n5. Exit.";
cout<<"\n\n\tEnter Your Choice::";
cin>>s;
switch(s)
{
case 1: //Find Length
l = ob.mystrlen();
cout<<"\n  There Are "<<l<<" Characters.";
break;
case 2: //Copy Given String
char *a = ob.mystrcpy();
cout<<"\n\n  Source String is::"<<ob.s;
cout<<"\n\n  Copied String is::"<<a;
break;
case 3: //Concatenate Given String With New String
cout<<"\n\n\tEnter New String To Concatenate::";
cin>>n;
char *b = ob.mystrcat(n);
cout<<"\n\n  Concatinated String is::"<<b;
break;
case 4: //Reverse Given String
char *c = ob.mystrrev();
cout<<"\n\n  Reversed String is::"<<c;
break;
case 5: //Exit
cout<<"\n\n Exited Succesfully.";
break;
default:
cout<<"\n\n  Improper Input.\n  Enter Proper Input";
break;
}
}
while(s!=5);
getch();
}


OUTPUT

Enter Your String::firoz

Select Your Option From Below::
1. Find Length.
2. Copy String.
3. Concatenate String.
4. Reverse String.
5. Exit.

Enter Your Choice::1

  There Are 5 Characters.

Select Your Option From Below::
1. Find Length.
2. Copy String.
3. Concatenate String.
4. Reverse String.
5. Exit.

Enter Your Choice::2

Source String is::firoz

Copied String is::firoz

Select Your Option From Below::
1. Find Length.
2. Copy String.
3. Concatenate String.
4. Reverse String.
5. Exit.

Enter Your Choice::3

Enter New String To Concatenate::memon

  Concatenated String is::firozmemon

Select Your Option From Below::
1. Find Length.
2. Copy String.
3. Concatenate String.
4. Reverse String.
5. Exit.

Enter Your Choice::4

  Reverse String is::zorif

Select Your Option From Below::
1. Find Length.
2. Copy String.
3. Concatenate String.
4. Reverse String.
5. Exit.

Enter Your Choice::5

  Exited Succesfully.


-----

Firoz Memon

Please view my other blogs:

          C++ Codes 4 Beginners

          Java Tips

          Java 4 Beginners

Previous Post Next Post