1 / 6

CS149D Elements of Computer Science

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.

thopper
Télécharger la présentation

CS149D Elements of Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 24: 12/3/2002 CS149D Fall 2002

  2. Outline • Character Data CS149D Fall 2002

  3. 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

  4. 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

  5. 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

  6. 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

More Related