Overview

strncmp()

Purpose:

Use the strncmp() function to compare at most the first n characters of the null-terminated strings string1 and string2.

Example use :

#include <string.h>
if (strncmp(command,"quit",4)==0)
 QuitProgram();
This should be #include <cstring>
using namespace std;

Returns :

An integer greater than, equal or less than 0 depending on whether the first n characters of string1 are greater than, equal or less than string2.

strncat()

Purpose :

Use the strncat() function to append the first n characters of string2 to string1 and terminate the resulting string with a null character. The terminating null of the first string is removed and string1 becomes the concatenation of the old string1 and the first n characters of string2. This function does not check the array size. Refer to the previous warning in the strcat section.

Example use :

#include <string.h>
char id[6] = "ID";
strncat(id, name, 10);	// id is the first 10 characters of name
#include <cstring>


EXERCISE 3

Of course, these are really just C exercises, but then it is indicative of the poor course material as a whole that the C methods are being taught over and above the C++ methods (which are never even touched upon - let alone have the C methods explained in terms of the C handlers in the C++ string methods

It goes without saying that you can have a go, but in preference to the C versions do it as if it was a C++ exercise!

  • Write a program that declares and initialises four strings "Roses are red", "violets are blue", "Sugar is sweet" and "And so are you". Be careful to type the strings exactly as shown including all capitalisation.
  • Write code to output each string to the screen, each one followed by a carriage return.
  • Use the strlen() function to print the length of each string on the line following the string, again followed by a carriage return.
  • Run the program. Observe the reported length of the strings and write them down.
  • Declare two more strings called line1 and line2. line1 must be made large enough to hold string1 and string2. Remember to add one for the null character. line2 must be large enough to hold the other two strings.
  • Using the strcpy() function to copy string1 to line1 and string3 to line2.
  • Use the strcat() function to append string2 to line1 and string4 to line2. Print line1 and line2 to the screen.
  • Use the strcmp() function to compare string3 to string4. Code an if else block in such a way as to print the two strings to the screen in alphabetical (ASCII) order. Hint : you will need an int variable to store the numeric result of the comparison. Print this numeric value to the screen. Finally lookup up the strncpy() function in the help files to note the difference between strncpy() and strcpy().


USE OF THE typedef KEYWORD

The typedef keyword can be used to give a new name to an existing data type. This can improve readability of a program as well as making declarations easier to type.

The syntax is : typedef ExistingType NewName;

Example use :

typedef int whole;	// while is a new integer data type

We can then say

whole value = 123; // integer value 123

EXERCISE 4

Write a program that by using typedef, create a new data type.