Top Banner

Click here to load reader

of 51

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

Dec 30, 2015

Download

Documents

Roy Park
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

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