Você está na página 1de 6

UNIVERSITY OF NOTRE DAME DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING CSE 232 Advanced Programming Fall 02

String Manipulation Functions


Below is a list of the prototypes of some of the string manipulation functions available in ANSI C. A description of each function follows. The functions require a: #include <string.h> 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. char * strcat (char *, const char *); char * strchr (const char *, int); int strcmp (const char *, const char *); char * strcpy (char *, const char *); int strcspn (const char *, const char *); int strlen (const char *); char * strncat (char *, const char *, int); int strncmp (const char *, const char *, int); char * strncpy (char *, const char *, int); char * strpbrk (const char *, const char *); char * strrchr (const char *, int); int strspn (const char *, const char *); char * strstr (const char *, const char *); char * strtok (char *, const char *);

1 strcat
1.1 Purpose
use strcat to concatenate (append) one string to another

1.2 Syntax
char *strcat (char *s1, const char *s2); s1: destination string s2: string to be appended to s1

1.3 Returns
strcat returns a pointer to the concatenated string (s1)

2 strchr
2.1 Purpose
use strchr to locate the rst occurrence of a particular character in a given string.

2.2 Syntax
char *strchr (const char *s, int c); s: string to be searched c: character to be located

2.3 Returns
if c is found, strchr returns a pointer to the rst occurrence of c in s; if the search fails, strchr returns a NULL

3 strcmp
3.1 Purpose
use strcmp to compare one string to another. The comparison is case sensitive.

3.2 Syntax
int strcmp (const char *s1, const char *s2); s1: rst string s2: second string

3.3 Returns
strcmp returns a negative number if the lexicographic order of s1 is smaller than s2, a positive number if it is larger, and a 0 if the two strings are identical

4 strcpy
4.1 Purpose
use strcpy to copy one string to another.

4.2 Syntax
char *strcpy (char *s1, const char *s2); s1: destination string s2: string to be copied

4.3 Returns
strcpy returns a pointer to the copied string (s1)

5 strcspn
5.1 Purpose
use strcspn to locate the position of the rst occurrence in a string of any character from another string.

5.2 Syntax
int strcspn (const char *s1, const char *s2); s1: string to be searched s2: string with set of characters to be located

5.3 Returns
if successful, strcspn returns the index of the rst character in s1 that belongs to the set of characters in s2. Therefore, it is the length of the initial substring of s1 that consists of characters not in s2. if s1 begins with a character from s2, strcspn returns 0. if s1 is devoid of any character from s2 (i.e. s1 and s2 have no characters in common), strcspn returns the length of s1.

6 strlen
6.1 Purpose
use strlen to nd the length of a string in bytes, not counting the terminating null character.

6.2 Syntax
int strlen (const char *s); s: string whose length is to be returned

6.3 Returns
strlen returns the length in bytes of the string s.

7 strncat
7.1 Purpose
use strncat to concatenate a specied number of characters of one string to another

7.2 Syntax
char *strncat (char *s1, const char *s2, int n); s1: destination string s2: string whose rst n characters are to be appended to s1 n: number of characters of s2 to be appended to s1

7.3 Returns
strncat returns a pointer to the concatenated string (s1)

8 strncmp
8.1 Purpose
use strncmp to compare a specied number of characters of two strings to one another. The comparison is case sensitive.

8.2 Syntax
int strncmp (const char *s1, const char *s2, int n); s1: rst string s2: second string n: number of characters from each string to be compared

8.3 Returns
strncmp returns a negative number if the lexicographic order of the rst n characters of s1 is smaller than the rst n characters of s2, a positive number if it is larger, and a 0 if the rst n characters of the two strings are identical

9 strncpy
9.1 Purpose
use strncpy to copy a specied number of characters of one string to another.

9.2 Syntax
char *strncpy (char *s1, const char *s2, int n); s1: destination string s2: string whose rst n characters are to be copied to s1 n: number of characters of s2 to be copied to s1

9.3 Returns
strncpy returns a pointer to the copied string (s1)

10 strpbrk
10.1 Purpose
use strpbrk to locate the position of the rst occurrence in a string of any character from another string.

10.2 Syntax
char *strpbrk (const char *s1, const char *s2); s1: string to be searched s2: string with set of characters to be located

10.3 Returns
if successful, strpbrk returns a pointer to the rst character in s1 that belongs to the set of characters in s2. if s1 is devoid of characters from s2 (i.e. s1 and s2 have no characters in common), strpbrk returns a NULL.

11 strrchr
11.1 Purpose
use strrchr to locate the last occurrence of a particular character in a given string.

11.2 Syntax
char *strrchr (const char *s, int c); s: string to be searched c: character to be located

11.3 Returns
if c is found, strrchr returns a pointer to the last occurrence of c in s; if the search fails, strrchr returns a NULL

12 strspn
12.1 Purpose
use strspn to locate the position of the rst character in a string that does not belong to another string.

12.2 Syntax
int strspn (const char *s1, const char *s2); s1: string to be searched s2: string with set of characters

12.3 Returns
if successful, strspn returns the index of the rst character in s1 that does not belong to the set of characters in s2. Therefore, it is the length of the initial substring of s1 that consists entirely of characters in s2. if s1 begins with a character that does not appear in s2, strspn returns 0. if s1 only contains characters from s2, strspn returns the length of s1.

13 strstr
13.1 Purpose
use strstr to locate the rst occurrence of one string in another.

13.2 Syntax
char *strstr (const char *s1, const char *s2); s1: string to be searched s2: string to be located

13.3 Returns
if s2 is a substring of s1, strstr returns a pointer to the rst occurrence of s2 in s1; if the search fails, strstr returns a NULL

14 strtok
14.1 Purpose
use strtok to locate the next token, or substring, in a string delimited by any character from a second string.

14.2 Syntax
char *strtok (char *s1, const char *s2); s1: string from which tokens are returned s2: string describing set of characters that delimit tokens

14.3 Returns
The rst call to strtok with the argument s1 returns a pointer to the rst token. Subsequent calls with a NULL as the rst argument will return the next tokens. When there are no tokens left, strtok returns a NULL. String s2 may be changed after each call.

Você também pode gostar