Monday, 11 April 2011

C++ String Class

In C++ String Class is provided for manipulation of strings . It is more user friendly .The functions of this class can be accessed using a  (".") operators.
To include this class we have to use 
using namespace std
------------------------------------------------------------
To initialize a String
------------------------------------------------------------ 
string a="This is my code";
------------------------------------------------------------------------
Functions Provided In String class

find()-  This function returns the position of a particular character .
substr()- This function returns the desired string with in a string.
length()-This Function returns the number of character in the given string.
compare()- This function compare two string and return a bool result.
find_first_of()- This functions find the first occurring of a character in the string
find_last_of()This functions find the first occurring of a character in the string


////////////////////////////////////////////////////////////////////////////////////////////////////////
Program to find the number of vowels in a string
*******************************************************************

long numvowel(string a1)
{
int count=0;
string str1;
string::iterator it;
for(it=a1.begin() ; it < a1.end();it++)
{
str1=*it;
if(str1.compare("a")==0 || str1.compare("A")==0|| str1.compare("e")==0|| str1.compare("E")==0|| str1.compare("i")==0|| str1.compare("I")==0|| str1.compare("A")==0|| str1.compare("o")==0|| str1.compare("O")==0|| str1.compare("u")==0|| str1.compare("U")==0)
count++;

}
else 
count;
}
return count;

}
****************************************************************************
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function to reverse a string 
********************************************************************************

string reverse(string a1)
{
string temp;
string str1;
string::reverse_iterator rit;
int i=0;
for ( rit=a1.rbegin() ; rit < a1.rend(); rit++ )

{
  temp=*rit;
  str1.append(temp);
  i++;

}


return str1;
}
**********************************************************************************
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


To check if a given string is palindrome or not


*************************************************************************

string palindrome(string a1)
{
string temp;
string str1;
int i=0;
string::reverse_iterator rit;
str1=reverse(a1);

i=str1.compare(a1);
if(i==0)
{
return "TRUE";
}
else

return "False";
}
*************************************************************************
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Function to find number of blank space in a string
***********************************************************************

int blankspace(string a1)
{
string str1;
int count=0;
string::iterator it;
for(it=a1.begin() ; it < a1.end();it++)
{
str1=*it;


if(str1.compare(" ")==0)
{
count++;
}
else
count;
}

return count;

}
**************************************************************************


Function prototype
long numchar(string);
int blankspace(string);
long numwords(string);
long numvowel(string);
string reverse(string);
string palindrome(string);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


You must have to include using namespace std;

 main function which call all this function
**************************************************************************
void main()
{
string a="This code is primarily developed to understanding from basic";
string str1,str2;
string::iterator i;
printf("The number of charactres in this string is:%d \n",numchar(a));
printf("The number of blankspace in this string is:%d \n",blankspace(a));
printf("The number of vowels in this string is:%d \n",numvowel(a));
str1=reverse(a);
for(i=str1.begin();i<str1.end();i++)
{
printf("%c",*i);


}
str2=palindrome(a);
printf("\n Given String is palindrome  :");
for(i=str2.begin();i<str2.end();i++)
{
printf("%c",*i);


}
_getch();
}
****************************************************************************
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



No comments:

Post a Comment