CS149D Elements of Computer Science
60 likes | 83 Views
Learn about character data in computer science, including alphabetic characters, digits, and special characters. Understand how characters are represented as binary code values and explore operations with character data.
CS149D Elements of Computer Science
E N D
Presentation Transcript
CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 24: 12/3/2002 CS149D Fall 2002
Outline • Character Data CS149D Fall 2002
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 CS149D Fall 2002
String1/3 • Dealing with variable length character data (instead of a single character) • Include • #include <iostream> • #include<string> • 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; CS149D Fall 2002
Strings2/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) CS149D Fall 2002
Strings3/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 CS149D Fall 2002