Click here to load reader

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming

Embed Size (px)

Citation preview

Introduction to Programming

Dr. Yang, QingXiong(with slides borrowed from Dr. Yuen, Joe)

LT8: Characters and Strings

CS2311 Computer ProgrammingOutline2Characters Declaration and InitializationASCII Input and OutputStrings Declaration and InitializationCopy and compareInput and output

Syntax Summary3KeywordscharPunctuators[],Constant\0, \n

Character data type4Written between single quotes. Examples'A', 'b', '*'In C++ language, a char type is represented by an integer.Therefore, a character can also be treated as an integer

4Character type examples5cout > c1; //enter the string "CS2311"cin >> c2; //get the character 'S' from buffercin >> c3; //get the character '2' from buffer

Printing a character12char c1='A',c2='B';cout > c;if ( )cout > s; //read "world"

How to read a line of characters?

cin.get23#include using namespace std;

void main(){char c;do {cin.get(c);cout >: skipping over whitespace but get() wontSyntax: char c;cin.get(c);

cin.getline25Predefined member function of cin to read a line of text (including space)Two arguments: cstring variable to receive the inputsize of the cstring

#include using namespace std;void main(){char s[20];while (1){cin.getline(s,20);cout