Name
strcat, strchr, strcmpi, strcpy, strcspn, strdup, stricmp -
Perform operations on strings.
Syntax
#include <string.h>
char *strcat(string1, string2)
char *string1;
const char *string2;
char *strchr(string, c)
const char *string;
int c;
int strcmp(string1, string2)
const char *string1;
const char *string2;
int strcmpi(string1, string2)
const char *string1;
const char *string2;
char *strcpy(string1, string2)
char *string1;
const char *string2;
size_t strcspn(string1, string2)
const char *string1;
const char *string2;
char *strdup(string)
const char *string;
int stricmp(string1, string2)
const char *string1;
const char *string2;
Description
The strcat, strchr, strcmp, strcmpi, strcpy, strcspn,
strdup, and stricmp functions operate on null-terminated
strings. The string arguments to these functions are
expected to contain a null character (\0) marking the end of
the string. No overflow checking is performed when strings
are copied or appended.
The strcat function appends string2 to string1, terminates
the resulting string with a null character, and returns a
pointer to the concatenated string (string1).
The strchr function returns a pointer to the first
occurrence of c in string. The character c may be the null
character (\0); the terminating null character of string is
included in the search. The function returns NULL if the
character is not found.
The strcmp function compares string1 and string2
lexicographically and returns a value indicating their
relationship, as follows:
Value Meaning
< 0 string1 less than string2
= 0 string1 identical to string2
> 0 string1 greater than string2
The strcmpi and stricmp functions are case-insensitive
versions of strcmp.
The strcpy function copies string2, including the
terminating null character, to the location specified by
string1, and returns string1.
The strcspn function returns the index of the first
character in string1 that belongs to the set of characters
specified by string2. This value is equivalent to the length
of the initial substring of string1 that consists entirely
of characters not in string2. Terminating null characters
are not considered in the search. If string1 begins with a
character from string2, strcspn returns 0.
The strdup function allocates storage space (with a call to
malloc) for a copy of string and returns a pointer to the
storage space containing the copied string. The function
returns NULL if storage cannot be allocated.
Notes
The strcmpi, strdup,and stricmp, functions are not part of
the ANSI definition but are instead Microsoft extensions to
it. They should not be used where ANSI portability is
desired.
Return Value
The return values for these functions are described above.
See Also
strncat(DOS), strncmp(DOS), strncpy(DOS), strnicmp(DOS),
strrchr(S), strspn(S)
Example
#include <string.h> #include <stdio.h>
char string[100] = "XYZabbc This is a string!"; char
template[100] = "XYZabbc This is A STRING!"; char
*newstring; char *result; int numresult;
main()
{
/* Construct computer program
** using "strcpy" and "strcat" */
strcpy(string, "computer");
result = strcat(string, " program");
printf("Result = %s\n", result);
/* Find the first occurrence of 'a': */
result = strchr(string,'a');
printf("String after an \"a\" is %s\n", result);
/* Compare one string against another */
/* and report whether less than, */
/* greater than or equal to: */
numresult = strcmp(string,template);
printf( "\"%s\" is %s \"%s\"\n", string, numresult ?
(numresult > 0 ?
"greater than" : "less than") :
"equal to", template );
/* Compare string with regard to case */
numresult = strcmpi("hello", "HELLO");
printf("\"%s\" is %s \"%s\"\n", "hello",
numresult ? ( numresult > 0 ?
"greater than" : "less than" ) :
"equal to", "HELLO" );
/* Make a copy of a string */
printf("\"%s\" \"%s\"\n", template, string);
result = strcpy(template,string);
printf("\"%s\" \"%s\"\n", template, string);
/* Search for a's, b's, or c's */
strcpy(string, "xyzabc" );
numresult = strcspn(string,"abc");
printf( "The location of the first a, b, ");
printf( "or c is %d\n", numresult );
/* Make newstring point to a
duplicate of string: */
newstring = strdup(string);
printf("The new string is %s\n", newstring);
}
This program demonstrates the uses of the strcat, strchr,
strcmp, strcmpi, strcpy, strcspn, and strdup functions.
(printed 6/18/89)