Other Useful String Functions

FunctionDescription
strlenGet the length of the string
strcatAppend to a string
strcpyCopy into a string
struprConvert all characters to uppercase
strlwrConvert all characters to lowercase
strcmpCompare two strings lexicographically
#include <string.h>    // for all string functions
#include <iostream.h>  // for input/output

void main(void)
{
  char string1[20];
  char string2[20] = "Hello"; 
  int len, diff;

  strcpy(string1, "Celsius"); // string1  "Celsius"
  len = strlen("Celsius");    // len = 7
  len = strlen(string1);      // len = 7

  strcat(string1, " Temp");   // string1 += " Temp"
  len = strlen(string1);      // len = 12

  strupr(string1);         // string1 = "CELSIUS TEMP"
  strlwr(string1);         // string1 = "celsius temp"

  diff = strcmp(string1, string2);  // compare

  if (diff == 0)
    cout << "The strings are the same";
  else if (diff < 0)
    cout << "string1 comes BEFORE string2";
  else
    cout << "string1 comes AFTER string2";
}
Previous page
Next page

Back to Lesson 12 Index
Back to Outline