OLE Features and Specifications

Chapter 15 - Power Programming: Tapping Important C and C++ Libraries

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

The String Functions (STRING.H)
Strings in C and C++ are usually considered one-dimensional character arrays terminated with a null character. The string functions, prototyped in STRING.H, typically use pointer arguments and return pointer or integer values. You can study the syntax of each command in the next section or, in more detail, from the help facility provided with the Visual C++ compiler. Additionally, buffer-manipulation functions such as memccpy( ) and memset( ) are also prototyped in STRING.H. The functions shown in Table 15-8 are the most popular ones in this group.
Table 15-8: Popular String Functions
Function
Description
memccpy( )
Copies from source to destination
memchr( )
Searches buffer for first ch
memcmp( )
Compares n characters in buf1 and bufs
memcpy( )
Copies n characters from source to destination
memicmp( )
Same as memcmp( ), except case sensitive
memmove( )
Moves one buffer to another
memset( )
Copies ch into n character positions in buf
strcat( )
Appends a string to another string
strchr( )
Locates first occurrence of a ch in a string
strcmp( )
Compares two strings
strcmpi( )
Compares two strings (case insensitive)
strcoll( )
Compares two strings (local specific)
strcpy( )
Copies string to another string
strcspn( )
Locates first occurrence of a character in string from a given character set
strdup( )
Replicates the string
strerror( )
System-error message saved
stricmp( )
Same as strcmpi( )
strlen( )
Length of string
strlwr( )
String converted to lowercase
strncat( )
Characters of string appended
strncmp( )
Characters of two strings compared
strncpy( )
Characters of a string copied to another
strnicmp( )
Characters of two strings compared (case insensitive)
strnset( )
String characters set to a given character
strpbrk( )
First occurrence of character from one string in another string
strrchr( )
Last occurrence of character in string
strrev( )
Reverses characters in a string
strset( )
All characters in string set to given character
strspn( )
Locates first substring from given character set in string
strstr( )
Locates one string in another string
strtok( )
Locates tokens within a string
strupr( )
Converts string to uppercase
strxfrm( )
Transforms local-specific string
The memory and string functions provide flexible programming power to C and C++ programmers.
Working with Memory Functions
The memory functions, discussed in the previous section, are accessed with the syntax shown in the following listing:
void *memccpy(void *dest,void *source,int ch,unsigned count)

void *memchr(void *buf,int
ch,unsigned count)

int memcmp(void *buf1,void *buf2,unsigned
count)

void *memcpy(void *dest,void *source,unsigned
count)

int memicmp(void *buf1,void *buf2,unsigned
count)

void *memmove(void *dest,void *source,unsigned
count)

void *memset(void *dest,int
ch,unsigned count)
Here, *buf, *buf1, *buf2, *dest, and *source are pointers to the appropriate string buffer. The integer ch points to a character value. The unsigned count holds the character count for the function.
The next section includes a number of examples that show the use of many of these functions.
Find a Character in a String
In this example, the buffer is searched for the occurrence of the lowercase character “f,” using the memchr( ) function:
/*
*   memchr.c
*   Demonstrating the use of the memchr( ) function.
*   Finding a character in a buffer.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <string.h>
#include <stdio.h>

char buf[35];
char *ptr;

main( )
{
 strcpy(buf,"This is a fine day for a search." );
 ptr=(char *)memchr(buf,’f’,35);
 if (ptr != NULL)
   printf(“character found at location: %d\n”,
          ptr-buf+1);
 else
   printf(“character not found.\n”);
 return (0);
}
For this example, if a lowercase “f” is in the string, the memchr( ) function will report the “character found at location: 11.”
Compare Characters in Strings
This example highlights the memicmp( ) function. This function compares two strings contained in buf1 and buf2. This function is insensitive to the case of the string characters.
/*
*   memcmp.c
*   Demonstrating the use of the memicmp( ) function
*   to compare two string buffers.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <string.h>

char buf1[40],
    buf2[40];

main( )
{
 strcpy(buf1,"Well, are they identical or not?");
 strcpy(buf2,"Well, are they identicle or not?");
 /* 0 - identical strings except for case */
 /* x - any integer, means not identical */

 printf(“%d\n”,memicmp(buf1,buf2,40));
 /* returns a nonzero value */
 return (0);
}
If it weren’t for the fact that “identical” (or is it “identicle”?) was spelled incorrectly in the second string, both strings would have been the same. A nonzero value, -1, is returned by memicmp( ) for this example.
Loading the Buffer with memset( )
Often, it is necessary to load or clear a buffer with a predefined character. In those cases, you might consider using the memset( ) function, shown here:
/*
*   memset.c
*   Demonstrating the use of the memset( ) function
*   to set the contents of a string buffer.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <string.h>

char buf[20];

main( )
{
 printf(“The contents of buf: %s”,memset(buf,’+’,15));
 buf[15] = ‘\0’;
 return (0);
}
In this example, the buffer is loaded with 15 + characters and a null character. The program will print 15 + characters to the screen.
Working with String Functions
The prototypes for using several string-manipulating functions contained in STRING.H are shown in Table 15-9.
Table 15-9: String Manipulating Functions
Function
Description
int strcmp(const char * s1, const char *s2)
Compares 2 strings
size_t strcspn(const char * s1, const
char * s2)
Finds a substring in a string
char * strcpy(char * s1, const char * s2)
Copies a string
char * strerror(int errnum)
ANSI-supplied number
char * _strerror(char * s)
User-supplied message
size_t strlen(const char * s)
Null-terminated string
char * strlwr(char * s)
String to lowercase
char * strncat(char * s1, const char *s2, size_t n)
Appends n char s2 to s1
int strncmp(const char * s1, char * s2,
size_t n)
Compares first n characters of two strings
int strnicmp(const char * s1, const char * s2, size_t n)
Compares first n characters of two strings (case insensitive)
char * strncpy(char * s1, const char * s2, size_t n)
Copies n characters of s2 to s1
char * strnset(char * s, int ch, size_t n)
Sets first n characters of string to char setting
char * strpbrk(const char * s1, const
char * s2)
Locates character from const s2 in s1
char * strrchr(const char * s, int ch)
Locates last occurrence of ch in string
char * strrev(char * s)
Converts string to reverse
char * strset(char * s, int ch)
String to be set with ch
size_t strspn(const char * s1, const char * s2)
Searches s1 with char set in s2
char * strstr(const char * s1, const char * s2)
Searches s1 with s2
char * strtok(char * s1, char * s2)
Finds token in s1. S1 contains token(s), s2 contains the delimiters
char * strupr(char * s)
Converts string to uppercase
Here, *s is a pointer to a string, while *s1 and *s2 are pointers to two strings. Usually, *s1 points to the string to be manipulated and *s2 points to the string doing the manipulation. ch is a character value.
Comparing the Contents of Two Strings
The following program uses the strcmp( ) function and reports how one string compares to another.
/*
*   strcmp.c
*   Demonstrating the use of the strcmp( ) function
*   to compare two strings.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <string.h>

char s1[45] = “A group of characters makes a good string.”;
char s2[45] = “A group of characters makes a good string?”;
int answer;
main( )
{
 answer = strcmp(s1,s2);
 if (answer>0) printf(“s1 is greater than s2");
   else if (answer==0) printf(”s1 is equal to s2");
     else printf(“s1 is less than s2");
 return (0);
}
Can you predict which of the preceding strings would be greater? Can you do it without running the program? The answer is that s1 is less than s2.
Searching for Several Characters in a String
The next program searches a string for the first occurrence of one or more characters:
/*
*   strspn.c
*   Demonstrating the use of the strcspn( ) function to find
*   the occurrence of one of a group of characters.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <string.h>

char s1[35];
int answer;

main( )
{
 strcpy(s1,"We are looking for great strings." );
 answer=strcspn(s1,"abc");
 printf(“The first a,b,c appeared at position %d\n”,
        answer+1);
 return (0);
}
This program will report the position of the first occurrence of an “a,” a “b,” or a “c.” A 1 is added to the answer since the first character is at index position zero. This program reports an “a” at position 4.
The First Occurrence of a Single Character in a String
Have you ever wanted to check a sentence for the occurrence of a particular character? You might consider using the strchr( ) function. The following application looks for the first blank or space character in the string.
/*
*   strchr.c
*   Demonstrating the use of the strchr( ) function to
*   locate the first occurrence of a character in a string.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <string.h>

char s1[20] = “What is a friend?”;
char *answer;

main( )
{
 answer=strchr(s1,’ ‘);
 printf(“After the first blank: %s\n”,answer);
 return (0);
}
What is your prediction on the outcome after execution? Run the program and see.
Finding the Length of a String
The strlen( ) function reports the length of any given string. Here is a simple example:
/*
*   strlen.c
*   Demonstrating the use of the strlen( ) function to
*   determine the length of a string.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <string.h>

char *s1="String length is measured in characters!";

main( )
{
 printf(“The string length is %d”,strlen(s1));
 return (0);
}
In this example, the strlen( ) function reports on the total number of characters contained in the string. In this example, there are 40 characters.
Locating One String in Another String
The strstr( ) function searches a given string within a group (a string) of characters, as shown here:
/*
*   strstr.c
*   Demonstrating the use of the strstr( ) function to
*   locate a string within a string.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <string.h>

main( )
{
 char *s1="There is always something you miss.";
 char *s2="way";

 printf(“%s\n”,strstr(s1,s2));
 return (0);
}
This program sends the remainder of the string to the printf( ) function after the first occurrence of “way”. The string printed to the screen is “ways something you miss.”
Converting Characters to Uppercase
A handy function to have in a case-sensitive language is one that can convert the characters in a string to another case. The strupr( ) function converts lowercase characters to uppercase, as shown here:
/*
*   strupr.c
*   Demonstrating the use of the strupr( ) function to
*   convert lowercase letters to uppercase.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <string.h>

char s1[]="Uppercase characters are easier to read.";
char *s2;

main( )
{
 s2=strupr(s1);
 printf(“The results: %s”,s2);
 return (0);
}
This program converts each lowercase character to uppercase. Note that only lowercase letters will be changed.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net