Loading, please wait...

Search Functions

This section presents the functions of the string-handling library used to search strings for characters and other strings.

 

The functions are summarized in the following table.

 

Function Prototype

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.

 

 

Function strchr

Function strchr searches for the first occurrence of a character in a string. If the character is found, strchr returns a pointer to the character in the string; otherwise, strchr returns NULL. Following example uses strchr to search for the first occurrences of 'a' and 'z' in the string "This is Vaishnavi Sharma".

 

Example

/* how to use strchr */

#include <stdio.h>
#include <string.h>

int main( void )
{
const char *string = "This is vaishnavi sharma "; /* initialize char pointer */
char character1 = 'a'; /* initialize character1 */
char character2 = 'z'; /* initialize character2 */

/* if character1 was found in string */
if ( strchr( string, character1 ) != NULL ) {
printf( "\'%c\' was found in \"%s\".\n",
character1, string );
} /* end if */
else { /* if character1 was not found */
printf( "\'%c\' was not found in \"%s\".\n",
character1, string );
} /* end else */

/* if character2 was found in string */
if ( strchr( string, character2 ) != NULL ) {
printf( "\'%c\' was found in \"%s\".\n",
character2, string );
} /* end if */
else { /* if character2 was not found */
printf( "\'%c\' was not found in \"%s\".\n",
character2, string );
} /* end else */

return 0;
}

Output:

'a' was found in "This is vaishnavi sharma".

'z' was not found in "This is vaishnavi sharma".

 

 

Function strcspn

Function strcspn determines the length of the initial part of the string in its first argument that does not contain any characters from the string in its second argument. In following Example The function returns the length of the segment.

 

Example

/* how to use strcspn */

#include <stdio.h>
#include <string.h>

int main( void )
{
/* initialize two char pointers */
const char *string1 = "The value is 3.14159";
const char *string2 = "1234567890";

printf( "%s%s\n%s%s\n\n%s\n%s%u\n",
"string1 = ", string1, "string2 = ", string2,
"The length of the initial segment of string1",
"containing no characters from string2 = ",
strcspn( string1, string2 ) );

return 0;
}

Output:

string1 = The value is 3.14159

string2 = 1234567890



The length of the initial segment of string1

containing no characters from string2 = 13

 

 

Function strpbrk

Function strpbrk searches its first string argument for the first occurrence of any character in its second string argument. If a character from the second argument is found, strpbrk returns a pointer to the character in the first argument; otherwise, strpbrk returns NULL. Example shows a program that locates the first occurrence in string1 of any character from string2.

 

Example

/* how to use strpbrk */

#include <stdio.h>
#include <string.h>

int main( void )
{
const char *string1 = "This is vaishnavi sharma"; /* initialize char pointer */
const char *string2 = "who you"; /* initialize char pointer */

printf( "%s\"%s\"\n'%c'%s\n\"%s\"\n",
"Of the characters in ", string2,
*strpbrk( string1, string2 ),
" appears earliest in ", string1 );

return 0;
}

Output:

Of the characters in "who you"

'a' appears earliest in

"This is vaishnavi sharma"

 

 

Function strrchr

Function strrchr searches for the last occurrence of the specified character in a string. If the character is found, strrchr returns a pointer to the character in the string; otherwise, strrchr returns NULL. Example shows a program that searches for the last occurrence of the character 'z' in the string "A zoo has many animals including zebras."

 

Example:

/*how to use strrchr */
#include <stdio.h>
#include <string.h>

int main( void )
{
/* initialize char pointer */
const char *string1 = "A zoo has many animals including zebras";

int c = 'z'; /* character to search for */

printf( "%s\n%s'%c'%s\"%s\"\n",
"The remainder of string1 beginning with the",
"last occurrence of character ", c,
“is: ”, strrchr( string1, c ) );

return 0;
}

Output:

The remainder of string1 beginning with the

last occurrence of character 'z' is: "zebras"

 

 

Function strspn

Function strspn determines the length of the initial part of the string in its first argument that contains only characters from the string in its second argument. The function returns the length of the segment.

 

Example:

/* how to use strspn */

#include <stdio.h>
#include <string.h>

int main( void )
{
/* initialize two char pointers */
const char *string1 = "The value is 3.14159";
const char *string2 = "mark steve";

printf( "%s%s\n%s%s\n\n%s\n%s%u\n",
"string1 = ", string1, "string2 = ", string2,
"The length of the initial segment of string1",
"containing only characters from string2 = ",
strspn( string1, string2 ) );

return 0;
}

Output:

string1 = The value is 3.14159

string2 = mark steve



The length of the initial segment of string1

containing only characters from string2 = 13

 

 

Function strstr

Function strstr searches for the first occurrence of its second string argument in its first string argument. If the second string is found in the first string, a pointer to the location of the string in the first argument is returned. The example uses strstr to find the string "def" in the string "abcdefabcdef".

 

Example:

/*how to use strstr */

#include <stdio.h>
#include <string.h>
.
int main( void )
{
const char *string1 = "abcdefabcdef"; /* string to search */
const char *string2 = "def"; /* string to search for */
.
printf( "%s%s\n%s%s\n\n%s\n%s%s\n",
"string1 = ", string1, "string2 = ", string2,
"The remainder of string1 beginning with the",
"first occurrence of string2 is: ",
strstr( string1, string2 ) );

return 0;
}

Output:

string1 = abcdefabcdef

string2 = def

The remainder of string1 beginning with the

first occurrence of string2 is: defabcdef

 

 

Function strtok

Function strtok  is used to break a string into a series of tokens. A token is a sequence of characters separated by delimiters (usually spaces or punctuation marks, but a delimiter can be any character). For example, in a line of text, each word can be considered a token, and the spaces and punctuation separating the words can be considered delimiters.

 

Example:

/*how to use strtok*/

#include <stdio.h>
#include <string.h>

int main( void )
{
/* initialize array string */
char string[] = "This is a sentence with 7 tokens";
char *tokenPtr; /* create char pointer */
.
printf( "%s\n%s\n\n%s\n",
"The string to be tokenized is:", string,
"The tokens are:" );
.
tokenPtr = strtok( string, " " ); /* begin tokenizing sentence */
/* continue tokenizing sentence until tokenPtr becomes NULL */
while ( tokenPtr != NULL ) {
printf( "%s\n", tokenPtr );
tokenPtr = strtok( NULL, " " ); /* get next token */
}
.
return 0;
}

Output:

 The string to be tokenized is:

This is a sentence with 7 tokens

The tokens are:

This

is

a

sentence

with

7

Tokens