Art_of_Programming_Contest_Part4

Art_of_Programming_Contest_Part4 - CHAPTER 3 PROGRAMMING IN...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 3 PROGRAMMING IN C: A TUTORIAL 63 The strchr() Function The strchr() function finds the first occurrence of a specified character in a string. The prototype is char *strchr(char *str, int ch); The function strchr() searches str from left to right until the character ch is found or the terminating null character is found. If ch is found, a pointer to it is returned. If not, NULL is returned. When strchr() finds the character, it returns a pointer to that character. Knowing that str is a pointer to the first character in the string, you can obtain the position of the found character by subtracting str from the pointer value returned by strchr(). Following Listing illustrates this. Remember that the first character in a string is at position 0. Like many of C's string functions, strchr() is case-sensitive. For example, it would report that the character F isn't found in the string raffle. Using strchr() to search a string for a single character. /* Searching for a single character with strchr(). */ #include <stdio.h> #include <string.h> main() { char *loc, buf[80]; int ch; /* Input the string and the character. */ printf("Enter the string to be searched: "); gets(buf); printf("Enter the character to search for: "); ch = getchar(); /* Perform the search. */ loc = strchr(buf, ch); if ( loc == NULL ) printf("The character %c was not found.", ch); else printf("The character %c was found at position %d.\n", ch, loc-buf); return(0); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CHAPTER 3 PROGRAMMING IN C: A TUTORIAL 64 Enter the string to be searched: How now Brown Cow? Enter the character to search for: C The character C was found at position 14. The strcspn() Function The library function strcspn() searches one string for the first occurrence of any of the characters in a second string. Its prototype is size_t strcspn(char *str1, char *str2); The function strcspn() starts searching at the first character of str1, looking for any of the individual characters contained in str2. This is important to remember. The function doesn't look for the string str2, but only the characters it contains. If the function finds a match, it returns the offset from the beginning of str1, where the matching character is located. If it finds no match, strcspn() returns the value of strlen(str1). This indicates that the first match was the null character terminating the string Searching for a set of characters with strcspn(). /* Searching with strcspn(). */ #include <stdio.h> #include <string.h> main() { char buf1[80], buf2[80]; size_t loc; /* Input the strings. */ printf("Enter the string to be searched: "); gets(buf1); printf("Enter the string containing target characters: "); gets(buf2); /* Perform the search. */ loc = strcspn(buf1, buf2); if ( loc == strlen(buf1) ) printf("No match was found."); else printf("The first match was found at position %d.\n", loc); return(0); }
Background image of page 2
CHAPTER 3 PROGRAMMING IN C: A TUTORIAL 65 Enter the string to be searched: How now Brown Cow?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/10/2011.

Page1 / 20

Art_of_Programming_Contest_Part4 - CHAPTER 3 PROGRAMMING IN...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online