okkk...
here is my code..
look at the code with ease...
//Program implementing a string class and all the related
//operations...
#include "iostream.h"
#include "conio.h"
#include "stdlib.h"
class strings //Class definition
{
char *string; //for the string data
public:
int len; //for lenght of string
strings() //Default constructor
{*string = NULL;len = 0;}
strings(char *a) //Parameterized constructor
{
len = str_len(a);
/*while (*a)
len++;*/
//*a -= len;
string = new char[len+1];
while (*a)
{
*string = *a;
string++;
a++;
}
*string = '\0';
string -= len;
}
// ~strings() //Destructor freeing memory
// {delete []string;}
void getdata() //When the entry is going to be taken from user
{
delete [] string;
char *a;
cout << endl << endl <<"Enter the string to be operated upon (Maxlength = 100): ";
cin.getline (a,100);
len = str_len(a);
//while (*a)
// len++;
//a -= len;
string = new char[len+1];
while (*a)
{
*string = *a;
string ++;
a++;
}
*string = '\0';
string -= len;
}
void displaydata() //Displaying data to the user
{
cout << endl << endl << "Your string is : " << string;
}
//Function Declarations which will operate on the string
void strcpy(strings &);
};
void strings::strcpy (strings &source)
{
delete []string;
string = new char[source.len+1];
while (*source.string)
{
*string = *source.string;
string ++;
source.string++;
}
*string = '\0';
string -= len;
}
int str_len (const char *a)
{
int len = 0;
while (*a)
{
len++;
a++;
}
return len;
}
void main()
{
clrscr();
char *str;
int choice;
strings main_string;
//Taking entry for the main string
main_string.getdata();
clrscr();
do
{
clrscr();
//Providing menu
cout << "\t\tMenu for the operations on the given string" << endl
<< "1. Change the Main String." << endl
<< "2. Display the string." << endl
<< "3. Count the length of the string." << endl
<< "4. Copy a string into the main string." << endl
<< "0. Exit" << endl;
//Taking choice
cout << endl << "Enter the no. of your choice here : ";
cin >> choice;
cin.ignore();
//Taking actions
switch (choice)
{
case 1:
main_string.getdata();
break;
case 2:
main_string.displaydata();
cin.get();
break;
case 3:
cout << endl << "Length of the string = " << main_string.len;
cin.get();
break;
case 4:
cout << endl <<"Enter a string to be copied (Maxlength = 100) : ";
cin.getline(str,100);
strings temp_string (str);
temp_string.displaydata();
cin.get();
main_string.strcpy(temp_string);
break;
case 0:
cout << endl << "\t\t\t\tThank You";
cin.get();
exit(0);
break;
default :
cout << endl << "\t\t\t\tWrong Entry";
cin.get();
break;
}
getch();
} while (choice != 0);
}
i am also attaching the .cpp file...