The string.h include file includes prototypes for the following library functions
Copy a string from one area to another. The use of this function was illustrated earlier.
Copy a string up to a maximum number of characters or end of source string. This avoids the problems associated with strcpy() The function has three parameters which specify the destination address, the source address and the maximum number of bytes to copy. A typical use is illustrated by the following code fragment
char inbuf[512];
char wkbuf[20];
.
.
strncpy(wkbuf,inbuf,19);
.
This copies at most 19 characters from
inbuf
to
wkbuf
The number of bytes to copy is set to 19, one less than the size of
wkbuf
because copying of long strings is terminated by the exhaustion of number of characters to copy not the need to store a NUL in the destination space. The use of strncpy() is generally preferable to the use of strcpy()
Concatenate strings The use of this function was illustrated earlier.
Concatenate strings with limit on size. This, like strncpy(), imposes a restriction on the maximum number of characters concatenated. It has three parameters specifying the destination and source addresses and the maximum number of characters to append. A typical use is shown below
int l;
char dirpath[80];
char file[256];
.
.
l = strlen(pname); /* how much space already used ?? */
strncat(pname,"/",80-l-1); /* append a slash */
strncat(pname,fname,80-l-2); /* and the file name to give
full path name (if poss) */
.
.
This code first determines the length of the string already in the destination area then appends as much as possible of the string in input. Compare strings. The use of this function was illustrated earlier. The return value of this function is positive or negative depending on the difference of the first pair of characters that differ that are found in the strings being compared.
Compares strings in a manner dependent on the current locale. The value returned by strcmp() is dependent on the host system's character set. This function allows the user control the return value to give a non-standard ordering of strings.
Compares strings with a limit on size
Transforms a string in a manner dependent on the current locale. This can be used instead of strcoll() to generate a transformed version of a string that can be used with strcmp() to give the effect of a non-standard ordering sequence. A separate transformed string is stored explicitly.
Search a string for a particular character. This function takes two parameters the first is the string to be searched and the second is the character to be searched for. The return value is a pointer to the first occurence of the character within the string or NULL if the character was not found.
The following example program called findsl shows the function in use.
#include <string.h>
main()
{
int input[256];
char *cptr;
printf("Enter a string ");
gets(input);
cptr = strchr(input,'/');
if(cptr)
{
printf("String from first / is %s\n",cptr);
}
else
printf("No / in input\n");
}
A typical dialogue is $ findsl Enter a string http://www.wlv.ac.uk/ String from first / is //www.wlv.ac.uk/ $ findsl Enter a string the cat sat on the mat No / in input
Computes length of initial substring of a string comprising characters NOT from a specified set of characters. It is the opposite of strspn()
Similar to strchr() only a set of characters may be specified. Here's an example program called showbrk.
#include <string.h>
main()
{
char inbuf[256];
char *cptr;
printf("Enter string ");
gets(inbuf);
cptr = strpbrk(inbuf,"abcdefghijklmnopqrstuvwxyz");
if(cptr)
printf("First lower case letter at position %d\n",cptr-inbuf);
else
printf("No lower case letters\n");
}
and a typical dialogue $ showbrk Enter string ABCDEfGHIHJKlm First lower case letter at position 5 $ showbrk Enter string ABCDEFG No lower case letters
Similar to strchr() only works from end of string. Here's an example program called findlast that demonstrates its use.
#include <string.h>
main()
{
char *cptr;
char input[256];
printf("Enter full path name ");
gets(input);
cptr = strrchr(input,'/');
if(cptr)
printf("File name %s\n",cptr+1);
else
printf("Not a full path name\n");
}
Note the use of "cptr+1" in the
printf()
parameter list, since
strrchr()
returned the address of the '/'. Computes length of initial substring of a string comprising characters from a specified set of characters. It is closely realted to strpbrk() as will be seen from the example of that function.
Looks for a string as a sub-string of another string.
Breaks string into sequence of tokens. This function has two parameters the first is a string to be examined and the second is a string consisting of the token separators. The function will be used in a loop, on the first call, with the first parameter set to point to the string, the function makes a private note of the parameters and where it has got to in breaking the string into tokens. On subsequent calls the first parameter should be NULL. The return value is a pointer to the token start address or NULL if there are no more tokens.
It is important to realise that strtok() operates by converting token separators into NULLs in the input string.
Here's an example of it in use.
#include <string.h>
main()
{
char inbuf[256];
char *cptr;
int count = 0;
printf("Enter path name ");
gets(inbuf);
cptr = strtok(inbuf,"/");
while(cptr)
{
printf("Component %d = %s\n",++count,cptr);
cptr = strtok(NULL,"/");
}
}
and the dialogue Enter path name home/staff/acad/jphb/cbook/new/chap8/chap8.txt Component 1 = home Component 2 = staff Component 3 = acad Component 4 = jphb Component 5 = cbook Component 6 = new Component 7 = chap8 Component 8 = chap8.txt
Generates an error message. This is used in conjunction with the standard global variable errno to give standard error messages describing the reason for failure of library routines or system calls. The actual messages are, of course, host system dependant. It takes a single integer parameter and returns a pointer to the relevant error message.
Determines the length of a string The use of this function was illustrated earlier
Fills a memory area with a particular character.
memset(buff,'\0',256); could be used to fill 256 bytes starting at buff with zeroes.
This copies characters from one memory area to another. Unlike strcpy() it does not stop when a NUL is encountered in the data being copied.
This is similar to memcpy() except that it works via an intermediate area giving defined behaviour if the areas overlap.
Compare areas of memory
Search memory for a particular character.