18strings_3014

18strings_3014 - Lecture File 18 COP 3014 January 16, 2008...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture File 18 COP 3014 January 16, 2008 A. Ford Tyson 1 Using Strings in C++ COP 3014 Lecture File 18 1 C-style strings the C++ string class Copyright 1997-present, Ann Ford Tyson Recap: the C++ Language core language data types like int, float, struct, class, arrays, etc. 2 C-style strings, a 1-D array of char terminated by the NULL character ('\0') standard libraries functions like sqrt, objects like cin, etc. the string class Why are C-strings so important? still very widely used in C and C++ programming; current textbooks typically cover both types of strings e.g. C++ file "open" function requires a C- string paramete 3 string parameter if you write a string like "hello" in a program, it is stored as a C-string understanding of C-strings underlies understanding of C++ string class C-strings are often more efficient for some tasks Lecture File 18 COP 3014 January 16, 2008 A. Ford Tyson 2 C-Style Strings a string is a 1-dimensional array of char terminated by the null character '\0' used to represent words, lines of text, etc. 4 size : maximum number of characters which can be stored length : actual number of characters before the '\0' the number of characters you attempt to store cannot exceed the string's size or errors occur // set size 8, 1 space reserved for '\0' const int WORD_SIZE = 8; // declare a data type name typedef char WordString [WORD SIZE] Declaring String Variables 5 typedef char WordString [WORD_SIZE]; // declare two variables WordString word1, word2; // declare a third and initialize it WordString word3 = "HOCKEY"; O C K E Y \0 \0 H Declaring String Variables p.2 6 word3 == &word3 [0] (address of 1st char) word3 is type const char* (const pointer to char) word3 [3] == 'K' and is type char Lecture File 18 COP 3014 January 16, 2008 A. Ford Tyson 3 Declaring String Constants const char* OK_MESSAGE = "Everything is ok!"; const char ERROR_MESSAGE [ ] = "Something is wrong here!"; bool disaster; 7 bool disaster; if (disaster) cout << ERROR_MESSAGE; else cout << OK_MESSAGE; const char A_WORD [WORD_SIZE] = "mystery"; Output of Strings cout << word3; prints HOCKEY field width 6 (default is length of string) 8 cout << setw(15) << word3; prints bbbbbbbbb HOCKEY right-justified in field of 15 Output of Strings p.2 const char* BLANKS = " b "; cout << setw (8) << BLANKS; prints 8 blanks useful for spacing output 9 cout << setw(3) << word3; prints HOCKEY minimum field needed is used Lecture File 18 COP 3014 January 16, 2008 A. Ford Tyson 4 char charlist [ 7 ] = "hockey"; cout << charlist; Consider the following What is printed in each case? 10 int intlist [ 3 ] = { 2,3,6 }; cout << intlist; char charlist2 [ 3 ] = { 'a', 'b', 'c' }; cout << charlist2; Input of Strings let's say the user types in this sentence when prompted, then we use one of the 12 reading operations shown below Dogs and cats are our friends.<eoln> Using the input operator >> leading whitespace...
View Full Document

Page1 / 17

18strings_3014 - Lecture File 18 COP 3014 January 16, 2008...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online