HowToProgramC : Lesson 30


Back Home Next



String Search Functions

C provides another set of functions relating to strings, called search functions. With the help of these functions, we can do different types of search in a string. For example, we can find at what position a specific character exists. We can search a character starting from any position in the string. We can find the preceding or proceeding string from a specific position. We can find a string inside another string. These functions are given below.

Function prototype Function description
char *strchr( const char *s, int c ); Locates the first occurrence of character c in string s. If c is found, a pointer to c in s is returned. Otherwise, a NULL pointer is returned.
size_t strcspn( const char *s1, const char *s2 );
Determines and returns the length of the initial segment of string s1 consisting of characters not contained in string s2.
size_t strspn( const char *s1, const char *s2 );
Determines and returns the length of the initial segment of string s1 consisting only of characters contained in string s2.
char *strpbrk( const char *s1, const char *s2 );
Locates the first occurrence in string s1 of any character in string s2. If a character from string s2 is found, a pointer to the character in string s1 is returned. Otherwise, a NULL pointer is returned.
char *strrchr( const char *s, int c );
Locates the last occurrence of c in string s. If c is found, a pointer to c in string s is returned. Otherwise, a NULL pointer is returned.
char *strstr( const char *s1, const char *s2 );
Locates the first occurrence in string s1 of string s2. If the string is found, a pointer to the string in s1 is returned. Otherwise, a NULL pointer is returned.
char *strtok( char *s1, const char *s2 );

A sequence of calls to strtok breaks string s1 into “tokens”—logical pieces such as words in a line of text—separated by characters contained in string
s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A
pointer to the current token is returned by each call. If there are no more tokens when the function is called, NULL is returned.

Example Code

Here is an example, which shows the use of different string search functions. The code of the program is given below.

/*A program which shows string search using <string.h> library*/
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
main()
{
char s1[] = "Welcome to " ;
char s2[] = "the C Tutorial" ;
char s3[] = "Welcome to Karachi" ;
char city[] = "Karachi";
char province[] = "Sind";
char s[80];
char *pc;
int n;
cout << "s1 = " << s1 << endl << "s2 = " << s2 << endl ;
cout << "s3 = " << s3 << endl ;
// function for string length
cout << "The length of s1 = " << strlen(s1) << endl ;
cout << "The length of s2 = " << strlen(s2) << endl ;
cout << "The length of s3 = " << strlen(s3) << endl ;
strcpy(s, "Hyderabad"); // string copy
cout<<"The nearest city to "<< city << " is " <<s<< endl ;
strcat(s, " and "); // string concatenation
strcat(s,city);
strcat(s, " are in ");
strcat(s, province);
strcat(s, ".\n");
cout << s;
if (!(strcmp (s1,s2)))//!is used as zero is returned if s1 & s2 are equal
cout << "s1 and s2 are identical" << endl ;
else
cout << "s1 and s2 are not identical" << endl ;
if (!(strncmp (s1,s3,7))) // ! is used as zero is returned for equality
cout << "First 7 characters of s1 and s3 are identical" << endl ;
else
cout << "First 7 characters of s1 and s3 are not identical" << endl ;
}

Following is the output of the program.
S1 = Welcome to
S2 = the C Tutorial
S3 = Welcome to Karachi
The length of s1 = 11
The length of s2 = 18
The length of s3 = 18
The nearest city to Karachi is Hyderabad
Hyderabad and Karachi are in Sind.
S1 and s2 are not identical
First 7 characters of s1 and s3 are identical



Back Home Next


Google




learntoknow@yahoo.com
© All rights Reserved.