"String" in TURBO C++

C/C++

"String" in TURBO C++
'String' is a very useful keyword but it is not available in TURBO C++. So, I developed a small class named 'String' using which I can perform all the string related operations (as in other languages) in TURBO C++. The 'String' class is capable of performing almost all the string related functions like adding, searching, replacing, assigning, comparing, conversions etc. Below is the overview of 'String' class.

/*##########*/
/*String Class*/
/*##########*/

class String
{
char *wd ;
int Length;

public :

/*********CONSTRUCTORS*********/

/* CONSTRUCTOR 1 */
String (int len);

/* CONSTRUCTOR 2 */
String (const char ch);

/* CONSTRUCTOR 3 */
String (const char *sentence);

/* CONSTRUCTOR 4 */
String (const String *str);

/* COPY CONSTRUCTOR */
String (const String &str) ;


/**********DESTRUCTOR**********/

~String();


/*****OVERLOADED OPERATORS*****/

/* OVERLOADED ASSIGNMENT OPERATORS */
String& operator = (char ch);
String& operator = (const char *sentence);
String& operator = (const String &str);

/* OVERLOADED PRE-INCREMENT OPERATOR */
String& operator ++();

/* OVERLOADED POST-INCREMENT OPERATOR */
String operator ++(int Faltu);

/* OVERLOADED RELATIONAL OPERATORS */
bool operator == (const String str) const;

bool operator != (const String str) const;

bool operator > (const String str) const;

bool operator < (const String str) const; bool operator >= (const String str) const;

bool operator <= (const String str) const; /* OVERLOADED ASSIGNMENT & AIRTHMATIC OPERATORS */
void operator += (const String str);

String operator + (const String &str);

friend String operator + (const String &str1,const String &str2);

/* OVERLOADED EXTRACTOR OPERATOR */
friend ostream& operator << (ostream &outputStream,String &str) /* OVERLOADED INSERTOR OPERATOR */
friend istream& operator >> (istream &inputStream,String &str)

/* OVERLOADED SQUARE BRACKETS */
char& operator [] (int n) const;

/*******ASSIGN FUNCTION********/

/* Assign a substring of str starting at n that is l characters long
to the current string */
String& assign (const String &str,int n,int l);


/*******INSERT FUNCTION********/

/* Inserts the string str into the current string, at location n */
String& insert(const String &str,int n);


/******APPEND FUNCTIONS********/

/* Appends a substring of str starting at n that is l characters long
on to the end of the current string */
String& append (const String &str,int n,int l);

/* Appends n repititions of ch on to the end of the current string */
String& append (const String &str,int n);


/*******COMPARE FUNCTIONS******/

/* Compare a substring of str starting at n that is l characters long to the current string.Returns 0 if two strings are equal,-1 if first string is less than second string, else returns 1 */
int compare (const String &str,int n,int l);

/* Compare a substring of the string str to a substring of the current string,where n and l refer to str and nc and lc refer to current string. Returns 0 if two strings are equal,-1 if first string is less than second string, else returns 1 */
int compare (int nc,int lc,const String&,int n,int l);


/******ERASE FUNCTIONS*********/

/* Removes num characters from the current string, starting at index and returns *this */
String& erase(int index,int num);

/* Removes all characters from the current string, till it finds the character ch ,if not found then dosen't modifies the current string and returns *this */
String& erase_upto (const char &ch);

/* Removes all digits from the current string and returns *this */
String& erase_all_digits ();

/* Removes all isspace characters from the current string and returns *this */
String& erase_all_isspace_chars ();


/*****SUBSTRING FUNCTIONS******/

/* Returns a substring of the current string starting at n that is l characters long */
String substr(int n,int l);

/* Returns a substring of the current string starting from n'th character to m'th character */
String substr(char n,char m);

/* Removes a substring of the current string starting from n'th character to m'th character except the m'th character*/
String cut_substr(char n,char m);


/****FIND ONLY FUNCTIONS*******/

/* Returns the first occurrence of str within the current string, starting at index, -1 if nothing is found */
int find(const String &str,int index);

/* Returns the first occurrence of ch within the current string and num characters, starting at index, -1 if nothing is found */
int find(const String &str,int index,int num);

/* Returns the index of the first character within the current string that matches any character in str, beginning the search at index, -1 if nothing is found */
int find_first_of(const String &str,int index);

/* Returns the index of the first character within the current string that does not match any character in str, beginning the search at index, -1 if nothing is found */
int find_first_not_of(const String &str,int index);

/* Returns the index of the last character within the current string that matches any character in str, beginning the search at index, -1 if nothing is found */
int find_last_of(const String &str,int index);

/* Returns the index of the last character within the current string that does not match any character in str, beginning the search at index,-1 if nothing is found */
int find_last_not_of(const String &str,int index);


/***FIND AND MODIFY FUNCTIONS***/

/* Replaces the first occurence of src with dest if found, else the current string remains the same.Returns the modified string if found else returns a null string. */
String find_first_and_replace (const String &src,const String &dest);

/* Replaces all the occurences of src with dest if found, else the current string remains the same.Returns the modified string if found else returns a null string. */
String find_and_replace_all (const String &src,const String &dest);

/* Removes the first occurence of str if found, else the current string remains the same.Returns the modified string if found else returns a null string. */
String find_first_and_remove (const String &str);

/* Removes all the occurences of str if found, else the current string remains the same.Returns the modified string if found else returns a null string. */
String find_and_remove_all (const String &str);


/*****CONVERSION FUNCTIONS*****/

friend double strtodbl(const String &str)

friend String dbltostr(double dbl)

friend int strtoint(const String &str)

friend String inttostr(int x)


/****MISCELLANEOUS FUNCTIONS***/

/* TO MANUALLY DELETE CURRENT STRING */
void Delete();

/* Returns the length of the current string */
int length() const;

/* Returns a standard C character array version of my String */
char* c_str() const;

/* Returns true if the string has no elements, else returns false */
bool IsEmpty();

/* Removes all elements from the current string */
String& clear();

/* Swaps the current string with given string */
void swap(String &str);

/* Sort the current string */
String& sort ();

/* Copies characters from a string into an array ch, starting at n that is l characters long*/
int Copy_to_array (char *ch,int n,int l);

/* Reverses the current string */
friend String strrev (String &str)

/* Changes the current string characters to uppercase */
friend String strupr (String &str)

/* Changes the current string characters to lowercase */
friend String strlwr (String &str)

/* cin strings with white spaces in b/w */
friend void gets (String &str)

/* Prints a string */
friend void puts (String str)

protected :

String substr(char n,char m,int cut);
void ValidateIndex(int &index,int offset);

};

/*#################*/
/*End Of String Class*/
/*#################*/


I have placed the "String" class inside a header file named "j_string.h". You can download this header file from the link given at the end of this article or you can copy the below written code and save it as "j_string.h".


/*###########*/
/*J_STRING.H*/
/*###########*/


#ifndef __J_STRING_H
#define __J_STRING_H

#ifndef __IOSTREAM_H
#include "iostream.h"
#endif

#ifndef __CONIO_H
#include "conio.h"
#endif

#ifndef __STRING_H
#include "string.h"
#endif

#ifndef __CTYPE_H
#include "ctype.h"
#endif

#ifndef __STDIO_H
#include "stdio.h"
#endif

#ifndef __STDLIB_H
#include "stdlib.h"
#endif

#ifndef __MATH_H
#include "math.h"
#endif

#ifndef __J_BOOL_H
#include "j_bool.h"
#endif

class String
{
char *wd ;
int Length;

public :

/***************CONSTRUCTORS***********/

/* CONSTRUCTOR 1 */
String (int len);

/* CONSTRUCTOR 2 */
String (const char ch);

/* CONSTRUCTOR 3 */
String (const char *sentence);

/* CONSTRUCTOR 4 */
String (const String *str);

/* COPY CONSTRUCTOR */
String (const String &str) ;


/***************DESTRUCTOR*************/

~String();


/*********OVERLOADED OPERATORS*********/

/* OVERLOADED ASSIGNMENT OPERATORS */
String& operator = (char ch);
String& operator = (const char *sentence);
String& operator = (const String &str);

/* OVERLOADED PRE-INCREMENT OPERATOR */
String& operator ++();

/* OVERLOADED POST-INCREMENT OPERATOR */
String operator ++(int Faltu);

/* OVERLOADED RELATIONAL OPERATORS */
bool operator == (const String str) const;

bool operator != (const String str) const;

bool operator > (const String str) const;

bool operator < (const String str) const;

bool operator >= (const String str) const;

bool operator <= (const String str) const;

/* OVERLOADED ASSIGNMENT & AIRTHMATIC OPERATORS */
void operator += (const String str);
String operator + (const String &str);
friend String operator + (const String &str1,const String &str2);

/* OVERLOADED EXTRACTOR OPERATOR */
friend ostream& operator << (ostream &outputStream,String &str)

/* OVERLOADED INSERTOR OPERATOR */
friend istream& operator >> (istream &inputStream,String &str)

/* OVERLOADED SQUARE BRACKETS */
char& operator [] (int n) const;


/************ASSIGN FUNCTION***********/

/* Assign a substring of str starting at n that is l characters long to the current string */
String& assign (const String &str,int n,int l);


/*************INSERT FUNCTION**********/

/* Inserts the string str into the current string, at location n */
String& insert(const String &str,int n);


/*************APPEND FUNCTIONS*********/

/* Appends a substring of str starting at n that is l characters long on to the end of the current string */
String& append (const String &str,int n,int l);

/* Appends n repititions of ch on to the end of the current string */
String& append (const String &str,int n);


/************COMPARE FUNCTIONS*********/

/* Compare a substring of str starting at n that is l characters long to the current string.Returns 0 if two strings are equal,-1 if first string is less than second string, else returns 1 */
int compare (const String &str,int n,int l);

/* Compare a substring of the string str to a substring of the current string,where n and l refer to str and nc and lc refer to current string. Returns 0 if two strings are equal,-1 if first string is less than second string, else returns 1 */
int compare (int nc,int lc,const String&,int n,int l);


/**************ERASE FUNCTIONS*********/

/* Removes num characters from the current string, starting at index and returns *this */
String& erase(int index,int num);

/* Removes all characters from the current string, till it finds the character ch ,if not found then dosen't modifies the current string and returns *this */
String& erase_upto (const char &ch);

/* Removes all digits from the current string and returns *this */
String& erase_all_digits ();

/* Removes all isspace characters from the current string and returns *this */
String& erase_all_isspace_chars ();


/************SUBSTRING FUNCTIONS*******/

/* Returns a substring of the current string starting at n that is l characters long */
String substr(int n,int l);

/* Returns a substring of the current string starting from n'th character to m'th character */
String substr(char n,char m);

/* Removes a substring of the current string starting from n'th character to m'th character except the m'th character*/
String cut_substr(char n,char m);


/***********FIND ONLY FUNCTIONS********/

/* Returns the first occurrence of str within the current string, starting at index, -1 if nothing is found */
int find(const String &str,int index);

/* Returns the first occurrence of ch within the current string and num characters, starting at index, -1 if nothing is found */
int find(const String &str,int index,int num);

/* Returns the index of the first character within the current string that matches any character in str, beginning the search at index, -1 if nothing is found */
int find_first_of(const String &str,int index);

/* Returns the index of the first character within the current string that does not match any character in str, beginning the search at index, -1 if nothing is found */
int find_first_not_of(const String &str,int index);

/* Returns the index of the last character within the current string that matches any character in str, beginning the search at index, -1 if nothing is found */
int find_last_of(const String &str,int index);

/* Returns the index of the last character within the current string that does not match any character in str, beginning the search at index,-1 if nothing is found */
int find_last_not_of(const String &str,int index);


/*********FIND AND MODIFY FUNCTIONS**************/

/* Replaces the first occurence of src with dest if found, else the current string remains the same.Returns the modified string if found else returns a null string. */
String find_first_and_replace (const String &src,const String &dest);

/* Replaces all the occurences of src with dest if found, else the current string remains the same.Returns the modified string if found else returns a null string. */
String find_and_replace_all (const String &src,const String &dest);

/* Removes the first occurence of str if found, else the current string remains the same.Returns the modified string if found else returns a null string. */
String find_first_and_remove (const String &str);

/* Removes all the occurences of str if found, else the current string remains the same.Returns the modified string if found else returns a null string. */
String find_and_remove_all (const String &str);


/***********CONVERSION FUNCTIONS*******/

friend double strtodbl(const String &str)

friend String dbltostr(double dbl)

friend int strtoint(const String &str)

friend String inttostr(int x)


/**********MISCELLANEOUS FUNCTIONS***************/

/* TO MANUALLY DELETE CURRENT STRING */
void Delete();

/* Returns the length of the current string */
int length() const;

/* Returns a standard C character array version of my String */
char* c_str() const;

/* Returns true if the string has no elements, else returns false */
bool IsEmpty();

/* Removes all elements from the current string */
String& clear();

/* Swaps the current string with given string */
void swap(String &str);

/* Sort the current string */
String& sort ();

/* Copies characters from a string into an array ch, starting at n that is l characters long*/
int Copy_to_array (char *ch,int n,int l);

/* Reverses the current string */
friend String strrev (String &str)

/* Changes the current string characters to uppercase */
friend String strupr (String &str)

/* Changes the current string characters to lowercase */
friend String strlwr (String &str)

/* cin strings with white spaces in b/w */
friend void gets (String &str)

/* Prints a string */
friend void puts (String str)

protected :

String substr(char n,char m,int cut);
void ValidateIndex(int &index,int offset);

};

/*************CONSTRUCTORS***********/

String :: String(int len = 0)
{
Length = len;
wd = new char[len+1];
wd[len] = '\0';
*wd = '\0';
}

String :: String(const char ch)
{
*this = ch;
}

String :: String(const char *sentence)
{
*this = sentence;
}

String :: String(const String* str)
{
*this = *str;
}

String :: String(const String& str)
{
*this = str;
}

/*************DESTRUCTOR*************/

String :: ~String()
{
delete wd;
}

/*******OVERLOADED OPERATORS*********/

String& String :: operator = (char ch)
{
Length = 1;
wd = new char[Length+1];
wd[0] = ch;
wd[1] = '\0';
return *this;
}

String& String :: operator = (const char *ch)
{
Length = strlen(ch);
wd = new char[Length+1];
strcpy(wd,ch);
return *this;
}

String& String :: operator = (const String& str)
{
Length = strlen(str.wd);
wd = new char[Length+1];
strcpy(wd,str.wd);
return *this;
}

String& String :: operator ++ ()
{
++(this->wd);
return *this;
}

String String :: operator ++ (int a)
{
a = a;
String temp = *this;
(this->wd)++;
return temp;
}

bool String :: operator == (const String str) const
{
return strcmp(wd,str.wd) == 0 ? true : false ;
}

bool String :: operator != (const String str) const
{
return strcmp(wd,str.wd) == 0 ? false : true ;
}

bool String :: operator > (const String str) const
{
return strcmp(this->wd,str.wd) > 0 ? true : false ;
}

bool String :: operator < (const String str) const
{
return strcmp(this->wd,str.wd) < 0 ? true : false ;
}

bool String :: operator >= (const String str) const
{
return *this > str?true:*this == str ? true : false;
}

bool String :: operator <= (const String str) const
{
return *this < str?true:*this == str ? true : false;
}

void String :: operator += (const String str)
{
*this = *this + str;
}

String String :: operator + (const String &str)
{
String Temp( Length + str.Length );
strcpy(Temp.wd,wd);
strcat(Temp.wd, str.wd);
return Temp;
}

String operator + (const String &str1,const String &str2)
{
String temp = str1;
return temp.operator+(str2);
}

ostream& operator << (ostream &outputStream,String &str)
{
outputStream << str.wd;
return outputStream;
}

istream& operator >> (istream &inputStream,String &str)
{
char *p;
inputStream >> p;
str = p;
return inputStream;
}

char& String :: operator [] (int n) const
{
return wd[n];
}

/*********ASSIGN FUNCTION************/

String& String:: assign (const String &str,int index,int len)
{
ValidateIndex(index,str.length());
ValidateIndex(len,str.length());
String temp = str;
temp.erase(0,index);
temp[len] = '\0';
*this = temp;
return *this;
}

/********INSERT FUNCTION*************/

String& String:: insert(const String &str,int x)
{
ValidateIndex(x,Length);
if(x==0)
{
*this = str + *this;
return *this;
}
String cpy;
cpy.assign(*this,x,Length);
this->assign(*this,0,x);
*this = *this + str;
*this+=cpy;
return *this;
}

/********APPEND FUNCTIONS************/

String& String:: append (const String &str,int index,int len)
{
ValidateIndex(index,str.length());
ValidateIndex(len,str.length());
int n = index+len;
for ( index; index < n; index++)
*this = *this + str[index];
return *this;
}

String& String:: append (const String &str,int num)
{
for (int i=1;i <= num;i++)
*this = *this + str;
return *this;
}

/*******COMPARE FUNCTIONS************/

int String :: compare (const String &str,int n,int l)
{
String temp;
temp.assign (str,n,l);
return *this < temp?-1:*this > temp?1:0;
}

int String :: compare (int nc,int lc,const String &str,int n,int l)
{
String temp,tempc;
temp.assign (str,n,l);
tempc.assign (*this,nc,lc);
return tempc < temp?-1:tempc > temp?1:0;
}

/********ERASE FUNCTIONS*************/

String& String:: erase (int n,int l)
{
if(*this=="")
{
return *this;
}

if(n < 0)
{
n = 0;
}

if (n+l >= Length)
{
this->assign(*this,0,n);
}
else
{
String temp = *this;
int nd = n + l;
for (n,nd;n < Length;n++,nd++)
temp[n] = temp[nd];
*this = temp;
}
return *this;
}

String& String:: erase_upto (const char &ch)
{
String temp = *this;
for (int n=0;n < Length;n++)
if(temp[n] == ch)
break;
temp.erase(0,n);
if(n < Length)
*this = temp;
return *this;
}

String& String:: erase_all_digits ()
{
String temp;
temp = *this;
for (int n=0;n < Length;n++)
{
if(isdigit(temp[n]))
{
temp.erase(n,1);
n--;
}
}
*this = temp;
return *this;
}

String& String:: erase_all_isspace_chars ()
{
String temp;
temp = *this;
for (int n=0;n < Length;n++)
{
if(isspace(temp[n]))
{
temp.erase(n,1);
n--;
}
}
*this = temp;
return *this;
}

/******SUBSTRING FUNCTIONS***********/

String String:: substr(int index,int num)
{
ValidateIndex(index,Length);
ValidateIndex(num,Length);
String temp;
if( index+num > Length)
temp.assign(*this,index,Length);
else
temp.assign(*this,index,num);
return temp;
}

String String:: substr(char n,char m)
{
String temp;
temp = this->substr(n,m,0);
return temp;
}

String String:: cut_substr(char n,char m)
{
String temp;
temp = this->substr(n,m,1);
return temp;
}

/*********FIND FUNCTIONS*************/

int String:: find(const String &str,int index=0)
{
ValidateIndex(index,Length);
int c,count = 0;
for (index; index < Length ;index++)
{
if (wd[index] == str[0])
{
c = index;
for (int j=0; j < str.length();j++,c++)
if (wd[c] == str[j])
count++;
if (count == str.length())
return index;
count = 0;
}
}
return -1;
}

int String:: find(const String &str,int index,int num)
{
ValidateIndex(index,Length);
ValidateIndex(num,Length);
int c,count=0,flag=-1;
int len_a = str.length();
int nd = index+num-1;
for (index; index <= nd;index++)
{
if (wd[index] == str[0])
{
c = index;
for (int j=0; j < len_a;j++,c++)
if (wd[c] == str[j])
count++;
if (count == len_a)
flag = index;
if (count != len_a)
count = 0;
}
}
return flag;
}

int String:: find_first_of(const String &str,int index=0)
{
ValidateIndex(index,Length);
int count = 0;
for (index; index <= Length-1;index++)
for (int i=0;i <= str.Length-1;i++)
{
if (wd[index] != str.wd[i])
{
count = 0;
break;
}
else
{
int c = index;
for (int j=0; j < str.Length;j++,c++)
if (wd[c] == str.wd[j])
count++;
if(count==str.Length)
return index;
}
}
return -1;
}

int String:: find_first_not_of(const String &str,int index=0)
{
ValidateIndex(index,Length);
int count = 0;
for (index; index <= Length-1;index++)
{
for (int i=0;i <= str.Length-1;i++)
{
if (wd[index] == str.wd[i])
{
count = 0;
break;
}
count++;
}
if(count==str.Length)
return index;
continue;
}
return -1;
}

int String:: find_last_of(const String &str,int index=0)
{
ValidateIndex(index,Length);
int count = 0;
String temp,temp1;
temp = *this;
temp1 = str;
strrev(temp);
strrev(temp1);
for (index; index <= temp.Length-1;index++)
for (int i=0;i <= temp1.Length-1;i++)
{
if (temp.wd[index] != temp1.wd[i])
{
count = 0;
break;
}
else
{
int c = index;
for (int j=0; j < temp1.Length;j++,c++)
if (temp.wd[c] == temp1.wd[j])
count++;
if(count==temp1.Length)
return temp.Length-index-temp1.Length;
}
}
return -1;
}

int String:: find_last_not_of(const String &str,int index=0)
{
ValidateIndex(index,Length);
int count = 0;
String temp,temp1;
temp = *this;
temp1 = str;
strrev(temp);
strrev(temp1);
for (index; index <= temp.Length-1;index++)
{
for (int i=0;i <= str.Length-1;i++)
{
if (temp.wd[index] == str.wd[i])
{
count = 0;
break;
}
count++;
}
if(count==str.Length)
return temp.Length-index;
continue;
}
return -1;
}

/************FIND AND MODIFY FUNCTIONS*********/

String String:: find_first_and_replace (const String &src,const String &dest)
{
int index = 0;
if( ( index = this->find(src) ) == -1)
return *this;
String Pre,Post;
Pre.assign(*this,0,index);
Post.assign(*this,index+src.length(),Length);
*this = Pre + dest + Post;
return *this;
}

String String:: find_and_replace_all (const String &src,const String &dest)
{
if( src == dest )
return *this;

int index = 0;
String temp,temp1 = *this,temp2;
while( (index = temp1.find(src) ) != -1)
{
temp2.assign(temp1,0,index);
temp = temp + temp2 + dest;
temp1.erase(0,index);
temp1.find_first_and_remove(src);
}
if(temp == '\0')
temp = *this;
*this = temp;
return temp;
}

String String:: find_first_and_remove (const String &str)
{
int index = 0;
if( ( index = this->find(str) ) == -1)
return *this;
String Pre = "" , Post = "";
Pre.assign(*this,0,index);
Post.assign(*this,index+str.length(),Length);
*this = Pre + Post;
return *this;
}

String String:: find_and_remove_all (const String &str)
{
while(this->find(str) != -1)
find_first_and_remove(str) ;
return *this;
}

/***************CONVERSION FUNCTIONS***********/

double strtodbl(const String &str)
{
double dbl;
dbl = strtod( str.c_str(), NULL);
return dbl;
}

String dbltostr(double dbl)
{
char *str;
int dec, sign, ndig = 5;
str = fcvt(dbl, ndig, &dec, &sign);
String temp = str;
temp.insert('.',dec);
if(sign == 1)
temp = '-' + temp;
return temp;
}

int strtoint(const String &str)
{
return (int)strtodbl(str);
}

String inttostr(int x)
{
char *str;
int dec, sign, ndig = 5;
str = fcvt((double)(x), ndig, &dec, &sign);
String temp = str;
temp.erase(dec,temp.length());
if(sign == 1)
temp = '-' + temp;
return temp;
}

/***************MISCELLANEOUS FUNCTIONS*********/

void String :: Delete()
{
delete wd;
}

int String :: length() const
{
return Length;
}

char* String :: c_str() const
{
return this->wd;
}

bool String :: IsEmpty()
{
String temp;
return *this==temp?true:false;
}

String& String :: clear()
{
String temp;
*this = temp;
return *this;
}

void String ::swap(String &str)
{
String temp;
temp = *this;
*this = str;
str = temp;
}

String& String:: sort ()
{
for (int i=0; i <= Length-2;i++)
for (int j=i+1; j <= Length-1;j++)
if (wd[i] < wd[j])
{
char temp;
temp = wd[i];
wd[i] = wd[j];
wd[j] = temp;
}
strrev(*this);
return *this;
}

int String :: Copy_to_array (char *ch,int l,int n=0)
{
String temp;
temp.assign (*this,n,l);
strcpy(ch,temp.wd);
return l;
}

String strrev(String &str)
{
strrev(str.wd);
return str;
}

String strupr(String &str)
{
strupr(str.wd);
return str;
}

String strlwr(String &str)
{
strlwr(str.wd);
return str;
}

void gets( String &str )
{
char *cp = new char[100001];
int i = 0;
char key;
while(1)
{
key = getch();
if(key == '\r')
{
cp[i] = '\0';
break;
}
if ( key == '\b' && i != 0)
{
i--;
cout << '\b' << " " << '\b';
}
else
{
cp[i]=key;
i++;
cout << key;
}
}
str = cp;
delete cp;
cout << endl;
}

void puts( String str )
{
cout << str;
}

/******PROTECTED FUNCTIONS***********/

String String:: substr(char n,char m,int cut)
{
String temp;
int start = 0,last = 0;
if( (start = this->find(n,0) ) == -1 )
start = 0;
if( (last = this->find(m,start+1) ) == -1 )
last = Length;
temp = this->substr(start,last-start);
if(cut == 1)
this->erase(start,last-start);
return temp;
}

void String:: ValidateIndex(int &index,int offset = 0)
{
if( index < 0 )
index = 0;
if( index > offset )
index = offset;
}

#endif



/*#################*/

/*End Of J_STRING.H*/
/*#################*/



Using the 'String' Class

To use the class see the code below:-

Output:-


Compiler Used - TURBO C++ Version 3.0

Download the source code and exe files from here.

1 comment:

Diana Rose said...

hey man! Thanks for making a class for String for Turbo C++ ... I mean thank you so much!

I've been searching for this. Now I have to search for sleep in TurboC/C++.

http://prettyitgirl.blogstpot.com/