Difference between Basic and Advanced String Searching in C

Question: What is Difference Between Basic and Advanced String Searching in C?

Answer: ANSI C provides several string functions for locating characters, any of a group of characters or locating and extracting individual sub-strings in a given string. Let’s consider, firstly, basic string searching functions,

Finding a character: To locate a single character with its leftmost or rightmost occurrence in a given string, strchr and strrchr functions are prototyped below:

    char *strchr(const char *str, int ch);
    char *strrchr(const char *str, intch);

strchr returns pointer to leftmost occurrence of character if it’s found otherwise retuns NULL. strrchr returns location of rightmost occurrence of character if it’s found else returns NULL. For example:

    char *loc1;
    char *loc2;
 
    -----
    loc = strchr("how's your C programming project.", 'p');
    loc = strrchr("how's your C programming project.", 'p');

‘loc1’ now points to character ‘p’ in programming in first argument while ‘loc2’ points to character ‘p’ in project in first argument.

advertisement
advertisement

Finding any of several characters: In order to locate any of several characters in the given string from the group string, function strpbrk is prototyped below:

    char *strpbrk(const char *, const char *);

For example:

/*
 * strpbrk.c -- strpbrk locates first occurrence of any of several 
 * characters from group string in a given string otherwise returns NULL
 */
#include <stdio.h>
#include <string.h>
int main(void)
{
    char *str = "how's your C programming project?";
    char *gstr = "interesting";
    char *loc;
 
    loc = strpbrk(str, gstr);
    printf("\nptr-to-char \"loc\" points to first occurrence of character "
           "\'%c\'\nat %d location in the string \"%s\"\n\n",
           *loc, loc - str, str );
 
    return 0;
}

Output of above program

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
ptr-to-char "loc" points to first occurrence of character "s"
at 4 location in the string "how's your C programming project?"

Now, here’s arising naturally a question on how to find the rightmost occurrence of any of several characters from the group string into a given string? In fact, we haven’t any such string function in C library. But we can write one using strchar function. Wouldn’t it be interesting you give a try to it yourself?

Finding a sub-string in a given string: strstr function searches for leftmost occurrence of the given sub-string in its entirety into the given string and returns pointer to it once found otherwise returns NULL. For example:

/*
 * strstr.c -- function strstr returns leftmost location of sub-string in a
 * given 1st argument else NULL
 */
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char str[100];
    char substr[50];
    char *loc;
 
    puts("*****Program returns location of leftmost occurrence of "
         "sub-string in the given string else returns NULL*****");
    puts("");
 
    puts("User, type in a string...");
    gets(str);
    puts("");
 
    puts("now, type in a sub-string...");
    gets(substr);
    puts("");
 
    /* Let's find sub-string  */
    loc = strstr(str, substr);
    if (loc != NULL)
        printf("leftmost occurrence of sub-string in a given string is at "
               "location %d.\n\n", (loc - str) + 1);
    else
        printf("sub-string not found.\n\n");
    return 0;
}

Output is as follows:

advertisement
*****Program returns location of leftmost occurrence of sub-string in the
given string else returns NULL*****
 
User, type in a string...
twinkle twinkle little star                       
 
now, type in a sub-string...
twinkle
 
leftmost occurrence of sub-string in a given string is at location 1.
 
User, type in a string...
twinkle twinkle little star
 
now, type in a sub-string...
wonder
 
sub-string not found.

Notice that strstr returns location of leftmost occurrence of sub-string in a given string else returns NULL. Then, what about rightmost occurrence of sub-string in a given string? That is strrstr! We don’t have such in C library. Won’t you like to try this?

Let’s now turn to take more advanced string searching functions and explore them here. We prototype two such functions below:

    size_t strspn(const char *str, const char *gstr);
    size_t strcspn(const char *str, const char *gstr);

Notice the type of two functions which is type ‘size_t’ i.e. each function returns an unsigned value. Let’s differentiate them. For example,

advertisement
/*
 * strspn.c -- program returns no. of characters in the beginning of 1st 
 * argument that match any of characters in the 2nd argument
 */
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char str[100];
    char group[100];
    size_t char_count;
 
    puts("****Program returns no. of characters at the beginning of string "
         "that match\n\tany of characters in group string.****");
    puts("");
 
    puts("User, type in a string...");
    gets(str);
    puts("");
 
    puts("now, type in group string...");
    gets(group);
    puts("");
 
    /* Let's count characters */
    char_count = strspn(str, group);
    printf("%d characters at the beginning of string matched.\n",
            char_count);
    puts("");
    return 0;
}

Output of the strspn.c is below:

****Program returns no. of characters at the beginning of string that match
	any of characters in group string.****
 
User, type in a string...
C requires very strong pointer concepts.
 
now, type in group string...
C is just pointers
 
4 characters at the beginning of string matched.
 
User, type in a string...
then how you feel with pointers? Are you okay with them?
 
now, type in group string...
Hmmm!
 
0 characters at the beginning of string matched.

Notice that strspn returns no. of characters from the start of the string which match any of the characters in group string. When 1st character of string doesn’t match any character in the group, function returns 0.

Let’s consider example of strcspn function below:

/* 
 * strcspn.c -- program returns max length of initial segment of first 
 * argument which comprises of characters not entirely from 2nd argument
 */
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char str[100];
    char gstr[100];
    size_t len_ini_seg;
 
    puts("\n*****Program returns max initial segment of string without any "
         "characters from group string*****");
    puts("");
 
    puts("User, type in a string...");
    gets(str);
    puts("");
 
    puts("now, type in group string...");
    gets(gstr);
    puts("");
 
    /* Let's call strcspn */
    len_ini_seg = strcspn(str, gstr);
    printf("%d characters at the beginning of string NOT matched any of "
           "characters in group string.\n", len_ini_seg);
    return 0;
}

Output is as under:

*****Program returns max initial segment of string without any characters
from group string*****
 
User, type in a string...
where are you going this time?
 
now, type in group string...
xyz
 
10 characters at the beginning of the string NOT matched any of characters
in group string.
 
User, type in a string...
i don't understand what do you mean...
 
now, type in group string...
i, i, hmmm...
 
0 characters at the beginning of the string NOT matched any of characters
in group string.

Notice that strcspn function behaves opposite to strspn function. It counts on characters from the start of the string which don’t match any characters in the group string.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.