Presentation is loading. Please wait.

Presentation is loading. Please wait.

Characters, Strings, and the cstring Library

Similar presentations


Presentation on theme: "Characters, Strings, and the cstring Library"— Presentation transcript:

1 Characters, Strings, and the cstring Library
Andy Wang Object Oriented Programming in C++ COP 3330

2 Recap A C-style string is a char array that ends with the null character Character literals are in single quotes ‘a’, ‘\n’, ‘$’ String literals are in double quotes “Hello world\n” The null character is an implicit part of a string literal

3 Recap The name of an array points to the first element of an array
When an array is passed into a function, the function can access the original array

4 The cctype Library Conversion functions return the ASCII value of a character int touper(int c) returns the uppercase version of c int tolower(int c) returns the lowercase version of c

5 Query functions Return true (non-zero) or false (0) to question posed by the function’s name int isdigit(int c) int isalpha(int c) int isalnum(int c) // a digit or a letter int islower(int c); int isupper(int c);

6 Query functions int isxdigit(int c) // hex digit (0-9, a-f)?
int isspace(int c) // white space? int iscntrl(int c) // control character? int ispunct(int c) // printing character other than space, // letter, digit? int isprint(int c) // printing character (including ‘ ’)? int isgraph(int c) // printing other other than ‘ ‘ (space)?

7 String I/O Insertion (<<) and extraction (>>)
char greeting[20] = “Hello, world”; cout << greeting; // prints “Hello, world” char lastname[20]; cin >> lastname; // reads a string into lastname // adds ‘\0’ automatically The insertion operator << will print the content of a char array, up to the first null character The extraction operator >> will read in a string, and stop at a white space

8 String I/O The cin examples reads one word at a time
How to read a whole sentence into a string?

9 Reading strings: get and getline
Two member functions in class istream (in the iostream library) char *get(char str[], int length, char delimiter = ‘\n’); char *getline(char str[], int length, char delimiter = ‘\n’); Note that the get function with only one parameter can only read a single character from the input stream char ch; ch = cin.get(); // extracts one char and returns it cin.get(ch); // extracts one char and stores in ch

10 get and getline Functions (with Three Parameters)
Read and store a c-style string str is the char array where the data will be stored Note that it is passed by reference length is the size of the array delimiter (optional, with ‘\n’ as default) specifies when to stop reading Automatically appends ‘\0’

11 Example Sample calls char buffer[80]; cin >> buffer; // reads one word into buffer cin.get(buffer, 80, ‘,’); // reads up to the first comma cin.getline(buffer, 80); // reads an entire line (up to endl) So, what is the difference between get and getline? get will leave the delimiter character on the input stream getline will extract and discard the delimiter

12 Another Example Input Hello, world Joe Smith. He says hello. Code
char greeting[15], name[10], other[20]; cin.getline(greeting, 15); // “Hello, world” cin.get(name, 10, ‘.’); // “Joe Smith” cin.getline(other, 20); // “. He says hello.”

13 Example String Calls gs/io1.cpp

14 io1.cpp #include <iostream> using name space std; int main() { char string1[30], string2[30], string3[30], string4[30]; // print some instructions cin >> string1; // get one word, append it with ‘\0’ cin.getline(string2, 30, ‘,’); // get line, drop ‘,’ cin.get(string3, 30, ‘e’); // get up to ‘e’ cin.get(string4, 30); // get 30 chars

15 io1.cpp cout << “\n\nOutputs:\n”; cout << “string1 = “ << string1 << endl; cout << “string2 = “ << string2 << endl; cout << “string3 = “ << string3 << endl; cout << “string4 = “ << string4 << endl; return 0; }

16 Program Execution > To be, or not to be, that is the question.

17 Program Execution > To be, or not to be, that is the question. Outputs: string1 = To string2 = be string3 = or not to b string4 = e, that is the question.

18 Standard C String Library
To use it, #include <cstring> int strlen(const char str[]); Takes a string argument, returns its length (excluding ‘\0’) Sample calls char phrase[30] = “Hello, world”; cout << strlen(“Greetings, Earthling!”); // prints 21 int length = strlen(phrase); // stores 12

19 strcpy char *strcpy(char str1[], const char str2[]);
Takes two string arguments, and copies the contents of the second string into the first string Sample calls char buffer[80], firstname[30], lastname[30] = “Smith”; strcpy(firstname, “Billy Joe Bob”); strcpy(buffer, lastname); cout << firstname; // prints “Billy Joe Bob” cout << buffer; // prints “Smith”

20 strcat char *strcat(char str1[], const char str2[]);
Takes two string arguments, and concatenates the second one onto the first Sample calls char buffer[80] = “Dog”; char word[] = “food”; strcat(buffer, word); // buffer is now Dogfood strcat(buffer, “ breath”); // buffer is now Dogfood breath

21 strcmp int strcmp(const char str1[], const char str2[]);
Takes two string arguments Returns 0 if they are equal Returns a negative number if str1 comes before str2 (based on ASCII code) Returns a positive number if str2 comes before str1

22 strcmp Sample Calls char word1[30] = “apple”; char word2[30] = “apply”; if (strcmp(word1, word2) != 0) cout << “The words are different\n”; strcmp(word1, word2); // < 0, word1 comes first strcmp(word1, “apple”); // 0, words are the same strcmp(“apple”, “Zebra”); // > 0, uppercase first

23 strcmp Note strcmp relies on ‘\0’ at the end of each string
No built-in bound checking in C++

24 strncpy, strncat, strncmp
Same as strcpy, strcat, strcmp Takes an additional integer N as an argument Processes up to ‘\0’ or N characters

25 strncpy, strncat, strncmp
Examples char buffer[80]; char word[11] = “applesause”; char bigword[] = “supercalifragilisticexpialidocious”; strncpy(buffer, word, 5); // buffer = “apple” strncat(buffer, “ piecemeal”, 4); // buffer = “apple pie” strncmp(buffer, “apple”, 5); // returns 0 strncpy(word, bigword, 10); // word = “supercalif”


Download ppt "Characters, Strings, and the cstring Library"

Similar presentations


Ads by Google