Characters Strings and Utility Libraries Heres what we

Characters, Strings, and Utility Libraries

Here’s what we know about the char data type: • Built into the language • Typically stored in a byte (1 -byte integer type) • Literals delimited by single quotes ‘a’ ‘n’ ‘ ’ • Based on the ASCII standard A-Z : 65 -90 a-z: 97 -122 0 -9: 48 -57

We often need to be able to know what type of character we are working with. • Example: Ask user to input a number – We cannot assume the value is a number • Example: Parsing data streamed in from a socket or file – Find delimiters such as : . , ; – Validating structure before type conversion

Try this… • Using techniques described before this chapter, create a function which verifies a password is – is at least 8 characters long – has at least one capital letter – has at least one digit – has at least one special character – contains no whitespace – contains no double letters

cctype Library – Built in character detection Prototype: int is*(char value) islower() returns true if in a-z; false otherwise isupper() returns true if in A-Z; false otherwise isalpha() returns (islower() || isupper()) cctype. h isdigit() returns true if in 0 -9; false otherwise isalnum() returns (isalpha() || isdigit()) isprint() returns (isalnum() || value ==‘ ‘) ispunct() returns (isprint() && !(isalnum() || ‘ ‘)) isspace() returns true if value is one of ‘ ‘, ‘v’, ‘t’, ‘n’; false otherwise

cctype Library – Conversion to case Prototype: char to*(char value) toupper() cctype. h tolower() Note: These functions are pass-by-value.

Example : Processing User Input char user. Option; cout << “We have reached a good stopping point. ” << endl; do { cout << “Do you wish to save or continue? (S or C)” << endl; cin >> user. Option; user. Option = toupper(user. Option); } while (!(user. Option== ‘S’ || user. Option==‘C’)); switch (user. Option) { case ‘S’ : do. Save(); break; case ‘C’: do. Continue(); break; default: break; }

Here’s what we know about c-strings: • String literal: “What’s up? ” • C-String: Array of char terminated by ‘ ’ W h a t ‘ s u p ?
![Declaring C-Strings • Use arrays char message[50]; char message[50] = “What’s up? ” char Declaring C-Strings • Use arrays char message[50]; char message[50] = “What’s up? ” char](http://slidetodoc.com/presentation_image_h2/1fa8fa570f19cd2ddf0d1be44f2d77ac/image-9.jpg)
Declaring C-Strings • Use arrays char message[50]; char message[50] = “What’s up? ” char message[] = “What’s up? ” • What is the differences between the above?

Consider this… • Using techniques learned before this chapter, determine the length of the string.

Consider this… • Using techniques learned before this chapter, how would you create one big string from these two smaller strings? char first[] = “Tom”; char last[] = “Chong”; • That is, we want a string to store “Tom Chong”

Consider this… • Using techniques learned before this chapter, create a copy of the following string: char name[] = “Tom Chong”;

Consider this… • Using techniques learned before this chapter, how would you determine if a user’s post to a forum contains profanity?

cstring Library : Utilities for manipulating strings size_t strlen(const char * str) returns the length of the string up to but not including char * strcat (char * destination, const char * source); appends the source string to the destination string returns a reference to the destination string cstring. h char * strcpy (char * destination, const char * source); copies the source string to the destination string returns a reference to the destination string. int strcmp (const char * str 1, const char * str 2); compares, lexicographically, str 1 to str 2. 0 : identical <0 : str 1 < str 2 >0 : str 1 > str 2

cstring Library : Utilities for manipulating strings char * strncat (char * destination, const char * source, size_t num); similar to strcat but only appends the first num characters of source cstring. h char * strncpy (char * destination, const char * source, size_t num); similar to strcpy but copies the first num characters num > strlen(source), desitnation is padded char * strncmp (char * str 1, const char * str 2); similar to strcmp but only compares up to num characters or until reached in either string

Consider this… • Using strcpy() or strncpy(), try to copy a string of length n into a string of length k where k < n.

cstring Library : Utilities to look for one thing in another char * strstr (char * str 1, const char * str 2 ); searches str 1 for occurrencesof str 2. returns a reference to the start of the substring in str 1 returns null if it was not found cstring. h size_t strspn (const char * str 1, const char * str 2 ); returns the number of characters defined by str 2 that appear, starting at the beginning of the string, in str 1. for example if char str 1[] = “t t t. This is a comment” char str 2[] = “ ntv”; the function would return 5.

Conisder this… • Using what we have learned, write a function that returns the nth occurrence of a substring

Consider this… • Using what we have learned so far, accept from user input a string of the form – “# op #” – # means an integer – op means one of +, -, *, / – e. g. , “ 2+2” or “ 4*16” • Do the computation and return the value to the user

C-Strings vs. Numbers • Brute force conversion is possible but cumbersome • cstdlib provides functions to perform conversion – From C-String to integers, longs or floating point values – From integers to C-Strings

cstdlib Functions : Conversion utilities int atoi(const char * str); returns the int equivalent of str. double atof (const char * str); returns the double equivalent of str cstdlib. h long int atol (const char * str); returns the long equivalent of str char * itoa (int value, char * str, int base ); returns a c-string equivalent of value as a base number Note! Not always supported as it is not part of the standard

Notes on ato* functions • For successful conversion the c-string must – Start with whitespace – Followed by optional + or – – Followed by a type-appropriate numeric value • Anything that appears afterwards is ignored • If the format is not per above, 0 is returned

string is pretty much a cstring class • append() • erase() • assign() • find() • at() • insert() • back() • length() • compare() • size() • copy() • swap() • empty()
- Slides: 23