Download presentation
Presentation is loading. Please wait.
Published byPhyllis Freeman Modified over 8 years ago
1
Lecture 24: 12/3/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 24: 12/3/2002
2
CS149D Fall 20022 Outline Character Data
3
Lecture 24: 12/3/2002CS149D Fall 20023 Character Data Character data consists of alphabetic characters, digits, and special characters. Each character corresponds to a a binary code value Recall ASCII (see Appendix A) ‘a’ is 97 in ASCII, “A” is 65 in ASCII Declaration char c = ‘A’; Single Character I/O cout.put(c);//output cin.get(c);//input, read any character even space See ascii_test.cpp
4
Lecture 24: 12/3/2002CS149D Fall 20024 String 1/3 Dealing with variable length character data (instead of a single character) Include #include Note we omitted the “.h”. The header files without the “.h” are the new header files in the new C++ standard Declaration string name = “Ayman”; string TITLE = “Mr. “; string astring = TITLE+name;//string concatenation Output cout << TITLE<<name;
5
Lecture 24: 12/3/2002CS149D Fall 20025 Strings 2/3 Input cin >> name; >> operator skips any leading white-space characters such as blanks and newlines, then reads successive characters into the variable stopping at the first trailing white-space character See string_test.cpp String operations name.length()//returns number of characters in string name = “The cat and the dog”;name.find(“the”); The function returns 12 //the first occurrence of a particular substring, if found. If not found, the //function returns the special value string::npos (A very large value)
6
Lecture 24: 12/3/2002CS149D Fall 20026 Strings 3/3 substr function myString.substr(a,b); //returns a new string extracted from myString starting at character a and for a length of b characters myString = “Programming and Problem Solving”; myString.substr(0,7)“Program” myString.substr(7,8)“ming and” myString(10,0)“” myString(24,40)“Solving” myString(40,24)Program terminates with an execution error message See section 6.3 for a listing of character functions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.