Customizing the Initial Control

Chapter 9 - Arrays

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

strcpy( ), strcat( ), strncmp( ), and strlen( )
All of the functions discussed in this section are predefined in the STRING.H header file. When using these functions, make certain to include the header file in your program. Remember, all of the string functions prototyped in STRING.H expect null-terminated string parameters. The following program demonstrates how to use the strcpy( ) function:
/*
*   strcpy.c
*   A C program using the strcpy function
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

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

#define iSIZE 20

main( )
{
 char szsource_string[iSIZE]="Initialized String!",
      szdestination_string[iSIZE];

 strcpy(szdestination_string,"String Constant");
 printf(“%s\n”,szdestination_string);

 strcpy(szdestination_string,szsource_string);
 printf(“%s\n”,szdestination_string);

 return(0);
}
The function strcpy( ) copies the contents of one string, szsource_string, into a second string, szdestination_string. The preceding program initializes szsource_string with the message, “Initialized String!” The first strcpy( ) function call actually copies “String Constant” into the szdestination_string, while the second call to the strcpy( ) function copies szsource_string into szdestination_string variable. The program outputs this message:
String Constant
Initialized String!
The following example is an equivalent C++ program.
//
//  strcpy.cpp
//   A C++ program using the strcpy function
//   Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <string.h>

#define iSIZE 20

main( )
{
 char szsource_string[iSIZE]="Initialized String!",
      szdestination_string[iSIZE];

 strcpy(szdestination_string,"String Constant");
 cout << “\n” << szdestination_string;

 strcpy(szdestination_string,szsource_string);
 cout << “\n” << szdestination_string;

 return(0);
}
The strcat( ) function appends two separate strings. Both strings must be null-terminated and the result itself is null terminated. The following program builds on your understanding of the strcpy( ) function and introduces strcat( ):
/*
*   strcat.c
*    A C program demonstrating how to use the strcat function
*    Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

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

#define iSTRING_SIZE 35

main( )
{
 char szgreeting[] = “Good morning”,
      szname[] =" Carolyn, “,
      szmessage[iSTRING_SIZE];

 strcpy(szmessage,szgreeting);
 strcat(szmessage,szname);
 strcat(szmessage,”how are you?");
 printf(“%s\n”,szmessage);

 return(0);
}
In this example, both szgreeting and szname are initialized, while szmessage is not. The first thing the program does is to use the function strcpy( ) to copy the szgreeting into szmessage. Next, the strcat( ) function is used to concatenate szname (“ Carolyn, ”) to “Good morning,” which is stored in szmessage. The last strcat( ) function call demonstrates how a string constant can be concatenated to a string. Here, “how are you?” is concatenated to the now current contents of szmessage (“Good morning Carolyn, ”). The program outputs the following:
Good morning Carolyn, how are you?
The next program demonstrates how to use strncmp( ) to decide if two strings
are identical:
/*
*   srncmp.c
*    A C program that uses strncmp to compare two strings with
*    the aid of the strlen function
*    Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

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

main( )
{
 char szstringA[]="Adam", szstringB[]="Abel";
 int istringA_length,iresult=0;

 istringA_length=strlen(szstringA);
 if (strlen(szstringB) >= strlen(szstringA))
   iresult = strncmp(szstringA,szstringB,istringA_length);
 printf(“The string %s found”, iresult == 0 ? “was” : “wasn’t”);

 return(0);
}
The strlen( ) function is very useful; it returns the number of characters, not including the null-terminator, in the string pointed to. In the preceding program it is used in two different forms just to give you additional exposure to its use. The first call to the function assigns the length of szstringA to the variable istringA_length. The second invocation of the function is actually encountered within the if condition. Remember, all test conditions must evaluate to a TRUE (not 0 or !0) or FALSE (0). The if test takes the results returned from the two calls to strlen( ) and then asks the relational question >=. If the length of szstringB is >= to that of szstringA, the strncmp( ) function is invoked.
Why is the program using a >= test instead of an = =? To know the answer you need a further explanation of how strncmp( ) works. The function strncmp( ) compares two strings, starting with the first character in each string. If both strings are identical, the function returns a value of zero. However, if the two strings aren’t identical, strncmp( ) will return a value less than zero if szstringA is less than szstringB, or a value greater than zero when szstringA is greater than szstringB. The relational test >= was used in case you wanted to modify the code to include a report of equality, greater than, or less than for the compared strings.
The program terminates by using the value returned by iresult, along with the conditional operator (?:), to determine which string message is printed. For this example, the program output is
The string wasn’t found
Before moving on to the next chapter, remind yourself that two of the most frequent causes for irregular program behavior deal with exceeding array boundaries and forgetting that character arrays, used as strings, must end with \0, a null-string terminator. Both errors can sit dormant for months until that one user enters a response one character too long.

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