Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-17-characters-strings

 ics212-17-characters-strings

Avatar for William Albritton

William Albritton

October 19, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  Character Review  Character Handling Library 

    String Initialization  String Input  String Library Functions
  2. ASCII Character Codes  '0' 48 dec 0x30 0011 0000

     '9' 57 dec 0x39 0011 1001  'A' 65 dec 0x41 0100 0001  'Z' 90 dec 0x5A 0101 1010  'a‘ 97 dec 0x61 0110 0001  'z‘ 122 dec 0x7A 0111 1010
  3. ASCII Chart  For more details, see the link on

    the class webpage to the ASCII chart
  4. Character Handling Library  Use header file <ctype.h>  Basic

    functions include:  Conversion between upper and lower case  Test for upper or lower case  Test for letters (and digits and alphanumeric)  Test for blank spaces
  5. Covert to Uppercase or Lowercase  int tolower(int)  If

    uppercase letter (A-Z), converts to lowercase (a-z)  Otherwise, returns the same character  int toupper(int)  If lowercase letter (a-z), converts to uppercase (A-Z)  Otherwise, returns the same character
  6. Tests for Uppercase or Lowercase  int islower(int)  If

    lowercase letter (a-z), returns a non-zero integer  Returns 0 if is not lowercase letter  int isupper(int)  If uppercase letter (A-Z), returns a non-zero integer  Returns 0 if not an uppercase letter
  7. Tests for Letters and/or Digits  int isalpha(int)  If

    letter (a-z, A-Z), returns non-zero, otherwise zero  int isdigit(char)  If digit (0-9), returns non-zero, otherwise zero  int isalnum(int)  Is letter or digit (0-9, a-z, A-Z), returns non-zero, otherwise zero
  8. Tests for Punctuation and Spaces  int ispunct(int)  If

    it is a punctuation character, returns non-zero  If alphanumeric, space, tab, or newline character, returns zero  int isspace(int)  If space, tab, or newline, returns non-zero  Otherwise, returns zero
  9. Example Program  See example code at: charfunctions.c  Shows

    the output of the character functions from the past few slides  Reminder: the newline (pressing the “Enter” key, written in our programs as '\n') is also a character!! % cp charfunctions.c program.c % make -f makefile-program % ./program
  10. What is a String?  In the C language, a

    string is an array of characters which ends with the end of string character, called the null character ('\0'), which has the numerical value of zero (0)  You can initialize strings in several ways, but you always need to include the null character ('\0') at the end of the string
  11. String Initialization Examples  Stores 7 null characters ('\0' =

    0x0)  Stores one 's' + null character ('\0' = 0x0) char string1[SIZE] = {‘\0’}; char string2[SIZE] = {‘s’};
  12. More String Initialization Examples  These two strings both store

    6 characters, and end with the null character ('\0') char string3[SIZE] = {‘s’,‘t’,‘r’,‘i’,‘n’,‘g’,‘\0’}; char string4[SIZE] = “string”;
  13. String Display  Use the format character %s to display

    a string  Will display all characters, up to the null character printf(“%s\n”, string3); //output: string string3[3] = ‘\0’; //output: str %s
  14. Example Code  See example program: string.c  Shows how

    to initialize strings  Shows content of strings, including null character ('\0')  Shows what NOT to do with strings, which is write over the null character % cp string.c program.c % make -f makefile-program % ./program
  15. Array of Pointers to Constant Strings  Can change address,

    but NOT any characters char *array1[] = {“zero”, “one”}; printf(“%s”,array1[0]); \\zero printf(“%c”,array1[0][0]); \\z array1[0][0]=‘A’;//Segmentation fault array1[0]=array1[1]; //change address
  16. Two-dimensional Array of Characters  Can change characters, but NOT

    any addresses char array2[2][5] = {“zero”, “one”}; printf(“%s”,array2[0]); \\zero printf(“%c”,array2[0][0]); \\z array2[0][0]=‘A’;//change character array2[0]=array2[1]; //incompatible types
  17. Two-dimensional Array of Characters II  Can change characters, but

    NOT any addresses char array3[2][5]={‘z’,‘e’,‘r’,‘o’, ‘\0’,‘o’,‘n’,‘e’,‘\0’,‘\0’}; printf(“%s”,array2[0]); \\zero printf(“%c”,array2[0][0]); \\z array2[0][0]='A';//change character array2[0]=array2[1]; //incompatible
  18. Example Program  See example code at: string2.c  The

    commented out code does not compile and has runtime errors % cp string2.c program.c % make -f makefile-program % ./program
  19. Safer String Input  For secure string input, we have

    to be careful that the user does not enter too many characters 1. If the number of characters is greater than the length of our string, it can write over other parts of memory and/or crash our program 2.Also, we need to think about the input stream: Do we want to leave extra characters on input stream or remove the extra character from the input stream?
  20. Function getstring()  Function getstring() reads characters until it encounters

    end of file (EOF), a newline ('\n'), blank space (' '), or tab ('\t')  If the user enters too many characters, these are left on the input stream  Make sure the size of the string is correct, otherwise your program may crash and/or overwrite memory
  21. Function getline()  Function getline() reads characters until it encounters

    end of file (EOF) or a newline ('\n'), so it reads one line of input  If the user enters too many characters, these are taken off the input stream  Make sure the size of the string is correct, otherwise your program may crash and/or overwrite memory
  22. Example Code  See these files:  input.c: has getstring()

    & getline() function calls  stringinput.c: getstring() & getline() function definitions  stringinput.h: getline() & getline() function prototypes  makefile-string: compile & link these files % make -f makefile-string % ./program
  23. Functions for String Conversions  Requires the #include<stdlib.h> header 

    Converts string to an integer:  int atoi(const char *str);  Converts string to a float:  double atof(const char *str);
  24. String Conversion Function Calls  Make sure that the parameters

    are strings, and not characters! int i=atoi(“1234”); printf(“i = %d\n”, i); //i = 1234 double f=atof(“1.23”); printf(“f = %f\n”, f); //f = 1.23 //int x=atoi(‘5’); //character!
  25. Example Code  Example program: conversion.c  Also see: getdouble.c

     Has the function atof() to convert a string to a float
  26. Functions for Manipulating Strings  Requires the #include<string.h> header 

    Basic functions include:  Copying and concatenating strings  Comparing strings  Searching strings  Determining length of strings  Tokenizing strings
  27. Copying Strings  Function prototype: char *strncpy(char *str1, const char

    *str2, size_t n);  Copies up to n characters from the string str2 to str1  Copying stops when n characters are copied or when reaches the null character in str2  Returns address of str1 strncpy(string1, string2, MAX);
  28. Concatenating Strings  Function prototype: char *strncat(char *str1, const char

    *str2, size_t n);  Appends string str2 to end of string str1 up to n characters  Copying stops after n characters or null character  Returns str1 strncat(string1, string2, MAX);
  29. Comparing Strings  Function prototype: int strcmp(const char *str1, const

    char *str2);  Compares the string str1 to the string str2  Returns zero if str1 and str2 are equal  Returns negative integer if str1 < str2  Returns positive integer if str1 > str2 int result = strcmp(string1, string2);
  30. Searching Strings for Characters  Function prototype: char *strchr(const char

    *str, int c);  Searches for the first occurrence of the character c in the string str  Returns a pointer pointing to the first matching character, or null if no match was found char *pointer = strchr(string, character);
  31. Searching Strings for Substrings  Function prototype: char *strstr(const char

    *str1, const char *str2);  Finds the first occurrence of the entire string str2 which appears in the string str1  Returns a pointer to the first occurrence of str2 in str1, or null if no match was found char *pointer = strstr(string1, string2);
  32. String Length  Function prototype: size_t strlen(const char *str); 

    Returns the number of characters in the string. int length = strlen(string);
  33. String Tokenizer  Function prototype: char *strtok(char *str1, const char

    *str2);  Used to break a string into tokens, a sequence of characters separated by delimiting characters (usually spaces or punctuation marks)  str1 is the string to be tokenized  str2 is the set of characters separating the tokens
  34. String Tokenizer  First function call: has str1 as first

    argument  Following function calls: have NULL as first argument  Returns a pointer to current token  If no more tokens are found, returns a null pointer char *pointer = strtok(string, delimiter); pointer = strtok(NULL, delimiter);
  35. Example Program  See example code at: stringfunctions.c  Also

    need stringinput.c, so use these commands to compile, link, and run the program % gcc stringfunctions.c stringinput.c % ./a.out
  36. Don’t Use These String Functions  Because of potential buffer

    overflow attack, unchecked input using strings (or any array) can crash your program or overwrite memory  char *gets(char *str);  int scanf(const char *format, ...);  char *strcpy(char *str1, const char *str2);  char *strcat(char *str1, const char *str2);
  37. Memory Management  Character Review  Character Handling Library 

    String Initialization  String Input  String Library Functions