Lecture 24: 12/3/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 24: 12/3/2002
CS149D Fall Outline Character Data
Lecture 24: 12/3/2002CS149D Fall 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
Lecture 24: 12/3/2002CS149D Fall 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;
Lecture 24: 12/3/2002CS149D Fall 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)
Lecture 24: 12/3/2002CS149D Fall 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