I hope this enables newbies to understand how to use classes and and member functions, destructors etc;
This String class is itself fully equipped to handle string functions like search, substring, wordcount, length. You can modify it to make it more efficient.
Copy this to a cpp file and execute it
Thanks
Vijay
Code:/* Function : This demonstrates the use of class String with basic utility functions * Date : 2/21/2004 * Author :Copyright © 2004 Vijay S * Version : Version : 1.0.0 * Compiler : Preferably BCC 5.5.1 * Email : vbexpert@hotpop.com */ #include <iostream> #include<string> using namespace std; class String { private: char* str;char* tmp; //Declare string variables long length; long l; public: //Empty constructor; String(){str="";length=0;tmp=new char[1];} //tmp is created as new as to delete in Destructor //String Constructor // Assign the string passed String(char* s){length=strlen(s); l=length+1 ;str=new char[l];strcpy(str,s);*(str+length)='\0'; tmp=new char[1];} //Destructor ~String(){delete str;delete tmp; } //Length of the string long Length(){length=strlen(str);return length;} //like in Java, toString returns the entire string char* toString(){ return str;}; //Get the substring providing start and length parameters char* subString(long stt,long len); //Get the wordcount "default delimiter is spaces and CRLF long WordCount(); //Get the location of a particular string or character inside the actual string long getCharLoc(char*,long); //set this string overriding the initial void setString(char* s) { delete str; length=strlen(s); l=length+1 ;str=new char[l];strcpy(str,s);*(str+length)='\0'; tmp=new char[1]; } }; long String::getCharLoc(char* sch,long stt) { long i,j; long schlen=strlen(sch); if (stt< 1 || stt>length) return 0; else { for(j=0;j<schlen;j++) { for(i=stt;i<length;i++) { if (*(str+i-1) == *(sch+j)) { stt=i+1; break; } else continue; } if (i>=length) break; } if(j==schlen ) return (stt-schlen); else return 0; } } char* String::subString(long stt,long len){ long a,b; if( stt>length || len > length || (length-stt+1)<len || stt<=0 || len <=0 ) return ""; else { a=len+1; tmp=new char[a]; for(a=0;a<len;a++) *(tmp+a)=*(str+stt-1+a); *(tmp+len)='\0'; return tmp; } } long String::WordCount() { long a,b=0; for(a=0;a<=length;a++) if ((*(str+a)== ' ' || a==length || *(str+a)=='\n') && !(*(str+a)==' ' && *(str+a-1)==' ') && !(*(str+a)=='\n' && *(str+a-1)=='\n') && !(*(str+a-1)==' ' && *(str+a)=='\n') && !(*(str+a-1)=='\n' && *(str+a)==' ') && !(*(str+a-1)=='\n' && a==length) && !(*(str+a-1)==' ' && a==length)) b++; return b; } void main() { //Define default string String x("Hello world This is new C++"); //show the string to check cout<<x.toString()<<endl; //get its length cout <<endl<<"The string length is " <<x.Length()<<endl; //Get the location of the word "world" cout<<"\n'world' is found at " <<x.getCharLoc("world",1)<<endl; //Get the loc of 'new' cout<<"'new' is found at "<<x.getCharLoc("new",1) <<endl; //Change the string x.setString("Bull ****"); //show the string to check cout<<x.toString()<<endl; //get its length cout <<endl<<"The new string length is " <<x.Length()<<endl; system("pause"); }


Reply With Quote