Friday, 13 March 2015

Searching in strings

Searching in strings with examples: 

C++ strings supports finding the required string content from the total string.

Here is the list available find functions.

1. find ( ) : It searches a string for a specified character or group of characters and returns the starting position of the first occurrence found or npos if no match is found.
2. find_first_of ( ): This function searches a target string and returns the position of the first match of any character in a specified group. If it finds no such element then it returns npos.
3. find_last_of ( ): This function searches a target string and returns the position of the last match of any character in a specified group. If it finds no such element then it returns npos.
4. find_first_not_of ( ) : This function searches a target string and returns the position of the first element that doesn’t match any character in a specified group. If it finds no such element then it returns npos.
5. find_last_not_of ( ): This function searches a target string and returns the position of the element with the largest subscript that doesn’t match of any character in a specified group. If it finds no such element then it returns npos.
6. rfind ( ): This function searches a string from end to beginning for a specified character or group of characters and returns the  starting position of the match if one is found. If it finds no such element then it returns npos.

String searching member functions and their general uses with examples:

 See the below example helps to understand the usage of the string searching functions usage

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
{
string MyString (50, 'S');
MyString.replace (0, 2, "NN");
for (int iIdex = 2; iIdex <= (MyString.size () / 2) - 1; iIdex++)
for (int factor = 2; factor * iIdex < MyString.size();factor++)
   MyString[factor * iIdex] = 'N';
 cout << "Prime Number:" << endl;
int iIndex = MyString.find ('S');
while (iIndex != MyString.npos) {
cout << iIndex << " ";
iIndex++;
iIndex = MyString.find ('S', iIndex);
}
cout << "\n Not a prime Number:" << endl;
iIndex= MyString.find_first_not_of ('S');
while (iIndex != MyString.npos) {
cout << iIndex << " ";
iIndex++;
iIndex = MyString.find_first_not_of('S', iIndex);
}
getchar ();
return 0;
}

The output from above program is
Prime Number:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

 Not a prime Number:

0 1 4 6 8 9 10 12 14 15 16 18 20 21 22 24 25 26 27 28 30 32 33 34 35 36 38 39 40

 42 44 45 46 48 49


find( ) allows you to walk forward through a string, detecting multiple occurrences of a
character or group of characters, while find_first_not_of( ) allows you to test for the absence
of a character or group.
The find member is also useful for detecting the occurrence of a sequence of characters in a
string:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
{
string InStr("This, is, my, FIRST, string, function, using, find");
int iIndex = InStr.find ("i");
while(iIndex != string::npos) {
cout << iIndex << endl;
iIndex++;
iIndex = InStr.find("i", iIndex);
}
getchar ();
return 0;
}

The output of the above program is
2

6

24

34

41

47


The above program performs no case sensitive search. That’s why it is not considered the I in the FIRST
Let us see the example that performs a case insensitive search:

// TestWifi.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
 string MakeUpper (string& StrIn)
 {
 char* pszBuff = new char[StrIn.length()];
StrIn.copy (pszBuff, StrIn.length ());
for(int iIndex = 0; iIndex < StrIn.length(); iIndex++)
pszBuff[iIndex] = toupper(pszBuff[iIndex]);
string strResult(pszBuff, StrIn.length());
delete pszBuff;
return strResult;
}
string MakeLower(string& StrIn)
{
  char* pszBuff = new char[StrIn.length()];
  StrIn.copy (pszBuff, StrIn.length ());
  for (int iIndex = 0; iIndex < StrIn.length(); iIndex++)
  pszBuff[iIndex] = tolower(pszBuff[iIndex]);
  string strResult (pszBuff, StrIn.length());
  delete pszBuff;
  return strResult;
}
int _tmain (int argc, _TCHAR* argv[])
{
string StrInput ("This, is, my, FIRST, string, functIon, using, find");
cout << StrInput << endl;
cout << MakeUpper (StrInput) << endl;
cout << MakeLower (StrInput) << endl;
int iIndex = StrInput.find ("i");
while (iIndex != string::npos) {
  cout << iIndex << endl;
  iIndex++;
  iIndex = StrInput.find ("i", iIndex);
}
  string lcase = MakeLower (StrInput);
  cout << lcase << endl;
  iIndex = lcase.find ("i");
while (iIndex != lcase.npos) {
  cout << iIndex << endl;
  iIndex++;
  iIndex = lcase.find ("i", iIndex);
}
string ucase = MakeUpper (StrInput);
cout << ucase << endl;
iIndex = ucase.find ("I");
while (iIndex != ucase.npos) {
cout << iIndex << endl;
iIndex++;
iIndex = ucase.find ("I", iIndex);
}
getchar ();
return 0;
}

Both the MakeUpper ( ) and MakeLower ( ) functions follow the same form: they allocate
storage to hold the data in the argument string, copy the data and change the case. Then they
create a new string with the new data, release the buffer and return the result string. The
c_str( ) function cannot be used to produce a pointer to directly manipulate the data in the
string because c_str( ) returns a pointer to const. That is, you’re not allowed to manipulate
string data with a pointer, only with member functions.


No comments: