Author Topic: C++ String class sample all operations  (Read 1785 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
C++ String class sample all operations
« on: November 16, 2011, 01:43:55 pm »
 Make your own Mystring.h having following behaviors in it.
1. toUpperCase();   // It will convert all characters to uppercase.
2. int equalsIgnoreCase(char *n);
// it will return ‘1’  if the Strings are equal and ‘0’ if not. Note that this will ignore the case difference in characters. i.e. it will return ‘1’ if PaKiTaN is compared with paKistAn.
3. int endsWith(char *suffix);
// Checks if the String ends with the specified suffix. Return ‘1’ for true and ‘0’ for false.
4. int startsWith(char *prefix);
// Check if the string starts with the specified prefix. Return ‘1’ for true and ‘0’  for false.
5. int firstIndexOf(char ch)
// return the first index of the specified character in the string. If the character is not present in the string then it will return -1.
6. int lastIndexOf(char ch)
// return last index of the specified character in the string. If the character is not present in the given string then it will return -1.



7. int totalOccurences(char *s)
// return total no of times the passing string is present as a substring.
e.g. MyString s(“Lahore is one of the best city of Pakistan”);
      cout<<s.totalOccurences(“of”);   // Output: 2
8. int substring(char *stg)
// return the starting index of the passing string if it is present as a substring in the calling string object. Return ‘0’ if not present.
e.g. MyString s(“Pakistan”);
       cout<<s.substring(“kist”);    // Output: 2
9. int toHashcode ();
// it will return hash code for the calling string object. The formula to calculate hash code is given below.
int hashcode=sum of all ASCII values for the characters present in String
e.g.  ABC
    hash code = A + B + C
                  = 65 + 66 + 67
            = 198
10. trim();
// Remove any leading or trailing whitespaces from the string.
11. char * split(int start_index, int end_index);
// return string within the given index range. Return “Error” if indexes are out of range.

12. int compareTo(char *n);
// compare two strings lexicographically. i.e. return ‘1’ if call string is lexicographically higher and return ‘-1’ if calling string is lexicographically lower otherwise return ‘0’ which means both strings are equal.

Code:
//Source Code Listing

#include <iostream.h>

class MyString
{
private:
    char *m_pString;
 
public:
    MyString();
    ~MyString();
    MyString(const char *p);
    MyString(const MyString &other);
    const MyString& operator = (const MyString &other);
    operator const char*();
 
    const char* ToUpperCase();
    int EqualsIgnoreCase(char *dest);
   
    int BeginsWith(char *prefix);
    int EndsWith(char *suffix);

    int FirstIndexOf(char ch);
    int LastIndexOf(char ch);

    int TotalOccurences(char *str);
    int SubString(char *key);

    int ToHashCode();
    void Trim();
    char *Split(int startIndex, int endIndex);
    int CompareTo(const char *key);

};

MyString::MyString()
{
     cout << "\nCalling Default Constructor";
     m_pString = NULL;
}
 
MyString::~MyString()
{
     if( this->m_pString != NULL)
     {
         cout << "\nCalling Destructor";
         delete this->m_pString;
         this->m_pString = NULL;
     }
}
 
MyString::MyString(const char *p)
{
     cout << "\nCalling Parameterized Constructor";
     int len = strlen(p);
     m_pString = new char [len + 1];
     strcpy(m_pString, p);
}
 
MyString::MyString(const MyString &other)
{
     cout << "\nCalling Copy Constructor";
     m_pString =  other.m_pString;

     //int len = strlen(other.m_pString);
     //m_pString = new char [len + 1];
     //strcpy(m_pString, other.m_pString);
}
 
const MyString& MyString::operator = (const MyString &other)
{
     cout << "\nCalling assignment operator";
     int len = strlen(other.m_pString);
     m_pString = new char [len + 1];
     strcpy(m_pString, other.m_pString);
     return *this;
}
 
 
MyString::operator const char* ()
{
     return this->m_pString;
}

const char* MyString::ToUpperCase()
{
      char * p = m_pString;
      int len = strlen(p);
      for(int i = 0; i < len; i++)
      {
         p[i] = toupper(p);
      }
      return m_pString;
}

int MyString::EqualsIgnoreCase(char *dest)
{
    char *src = m_pString;
    int len = strlen(src);
    int destlen = strlen(dest);
    if(len != destlen)
       return 0;
    
    for(int i = 0; i < len; i++)
    {
       if( toupper(src) != toupper(dest))
          return 0;
    }
    return 1;
}

int MyString::BeginsWith(char *prefix)
{
    char *src = m_pString;
    int len = strlen(src);
    int destlen = strlen(prefix);
    if(destlen > len)
      return 0;
    for(int i = 0; i < destlen; i++)
    {
       if(src != prefix)
          return 0;
    }
    return 1;
}

int MyString::EndsWith(char *suffix)
{
    char *src = m_pString;
    int len = strlen(src);
    int destlen = strlen(suffix);
    if(destlen > len)
      return 0;
    for(int i = 0, j = len - destlen; i < destlen; i++, j++)
    {
       if(src[j] != suffix)
          return 0;
    }
    return 1;
}

int MyString::FirstIndexOf(char ch)
{
    char *src = m_pString;
    int len = strlen(src);
    for(int i = 0; i < len; i++)
    {
       if(src == ch)
          return i;
    }
    return -1;
}

int MyString::LastIndexOf(char ch)
{
    char *src = m_pString;
    int len = strlen(src);
    for(int i = len - 1; i >= 0; i--)
    {
       if(src == ch)
          return i;
    }
    return -1;
}

int MyString::TotalOccurences(char *key)
{
    char *val = m_pString;
    int count = 0;

    while(1)
    {
      char *p = strstr(val, key);
      if(p == 0)
         break;
      val = &p[strlen(key)];
      count++;
    }
    
    return count;
}

int MyString::SubString(char *key)
{
    char *val = m_pString;
    int count = 0;

   char *p = strstr(val, key);
   if(p == 0)
      return -1;
   return (p - val);
}

int MyString::ToHashCode()
{
   int code = 0;
   char *p = m_pString;
   int len = strlen(p);
   for(int i = 0; i < len; i++)
   {
      code += p;
   }
   return code;
}

void MyString::Trim()
{
   int startIndex = 0, endIndex = 0;
   char *p = m_pString;
   int len = strlen(p);
   for(int i = 0; i < len; i++)
   {
      if(p == ' ' || p == '\t')
         continue;
      else
      {
         startIndex = i;
         break;
      }
   }
   
   for(int i = len - 1; i >= 0; i--)
   {
      if(p == ' ' || p == '\t')
         continue;
      else
      {
         endIndex = i;
         break;
      }
   }
   MyString dest = Split(startIndex, endIndex);
   *this = dest;
}

char* MyString::Split(int startIndex, int endIndex)
{
   int len = strlen(m_pString);
   if(startIndex < 0 || endIndex >= len) 
      return "";
   if(endIndex < 0 || endIndex >= len) 
      return "";
   int destlen = endIndex - startIndex + 1;
   if(destlen <= 0)
      return "";

   char *p = new char[destlen + 1];
   strncpy(p, &m_pString[startIndex], destlen);
   p[destlen] = '\0';
   return p;
}

int MyString::CompareTo(const char *key)
{
   return strcmp(m_pString, key);
}


void main()
{
   MyString m1("welcome to software and finance. Thank you for visiting software and finance.");

   cout << "\nToUpperCase returned: " << m1.ToUpperCase();
   cout << "\nEqualsIgnoreCase returned:  " << m1.EqualsIgnoreCase("kathir");

   cout << "\nBeginsWith('WELCOME') returned: " << m1.BeginsWith("WELCOME");
   cout << "\nBeginsWith('PP') returned: " << m1.BeginsWith("PP");

   cout << "\nEndsWith('FINANCE.') returned: " << m1.EndsWith("FINANCE.");
   cout << "\nEndsWith('PP') returned: " << m1.EndsWith("PP");

   cout << "\nFirstIndexOf('F') returned: " << m1.FirstIndexOf('F');
   cout << "\nFirstIndexOf('P') returned: " << m1.FirstIndexOf('P');

   cout << "\nLastIndexOf('F') returned: " << m1.LastIndexOf('F');
   cout << "\nLastIndexOf('P') returned: " << m1.LastIndexOf('P');

   cout << "\nTotalOccurences('SOFTWARE'): " <<  m1.TotalOccurences("SOFTWARE");
   
   cout << "\nSubString('FINANCE'): " <<  m1.SubString("FINANCE");

   cout << "\nToHashCode(): " << m1.ToHashCode();

   cout << "\n\n";

   MyString m2(" Welcome   ");
   m2.Trim();
   cout << "\nTrim(): " << (const char*) m2;

   cout << "\nSplit(3,7): " << m2.Split(3, 6);

   cout << "\nCompareTo('ZZ'): " << m2.CompareTo("ZZ");
   cout << "\nCompareTo('AA'): " << m2.CompareTo("AA");
   cout << "\nCompareTo('Welcome'): " << m2.CompareTo("Welcome");
   cout << "\n\n";
}
« Last Edit: November 16, 2011, 04:31:18 pm by kathir »

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1