Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Programming Functions: Passing Parameters by Reference.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Programming Character I/O. COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
C++ Basics Part I Part II. Slide 2 Part I: basics.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS161 Introduction to Computer Science Character Data.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
CHAPTER 8 CHARACTER AND STRINGS
Input & Output: Console
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
Chapter 05 (Part III) Control Statements: Part II.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CSC 270 – Survey of Programming Languages
Chapter 15 Strings as Character Arrays
Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Characters and Strings
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
LESSON 2 Basic of C++.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Constants, Data Types and Variables
LESSON 2 Basic of C++.
Exercise 1 – Datentypen & Variablen
CS31 Discussion 1E Jie(Jay) Wang Week5 Oct.27.
Computing Fundamentals
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Chapter 3: Expressions and Interactivity.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
CS150 Introduction to Computer Science 1
Using string type variables
Std Library of C++.
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Programming Strings.
Presentation transcript:

Characters

COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment resp = 'c'; * Input/Output char a, b, c, d; cin >> a >> b >> c >> d; // user types: 1X3Y // a <- '1', b<-'X', c<-'3', d<-'Y' cout <<a<< ' ' <<b<< ' ' <<c<< ' ' <<d<< endl; // output: 1 X 3 Y

COMP104 Lecture 21 / Slide 3 The ASCII Character Set * 7-bit encoding for 128 possible characters * Some of the 32 non-printing control characters n 0 NUL character (end of string character) n 7 Bell character (makes beep) n 8 Backspace character (\b) n 9Tab character (\t) n 10 Newline character (\n) * space (32) is the first printable ACSII character * '0' - '9' have code values 48 through 57 * 'A' - 'Z' have code values 65 through 90 * 'a' - 'z' have code values 97 through 122 * See Appendix A.1 in book for full ASCII list

COMP104 Lecture 21 / Slide 4 The ASCII Character Set * l Use 1: perform arithmetics l Use 2: perform comparison

COMP104 Lecture 21 / Slide 5 Characters: Example 3 // program to output a random character #include using namespace std; void main(){ char c; int r; srand(time(0)); r = rand()%26; c = 'a' + r;// addition with characters!! cout << "random character: " << c << endl; }

COMP104 Lecture 21 / Slide 6 Relational char Operators * '0' - '9' have code values 48 through 57 '0' < '1' <... < '9' * 'A' - 'Z' have code values 65 through 90 'A' < 'B' <... < 'Z' * 'a' - 'z' have code values 97 through 122 'a' < 'b' <... < 'z' * (Upper case is always < lower case)

COMP104 Lecture 21 / Slide 7 Character Functions in  isupper(c) returns nonzero if c is an uppercase letter  islower(c) returns nonzero if c is a lowercase letter  isalpha(c) returns nonzero if either islower(c) or isupper(c) returns nonzero  isdigit(c) returns nonzero if c is a digit character  isalnum(c) returns nonzero if either isalpha(c) or isdigit(c) returns nonzero  isspace(c) returns nonzero if c is a space, newline, or tab  tolower(c) if c is uppercase it returns lowercase; otherwise it returns c  toupper(c) if c is lowercase it returns uppercase; otherwise it returns c

COMP104 Lecture 21 / Slide 8 Characters: Example 1 // islower.cpp #include using namespace std; int islower(char c){ if(c>='a' && c <='z') return 1; else return 0; } void main(){ char c; cout << "Enter a character: "; cin >> c; if(islower(c)) cout << c << " is a lower case letter" << endl; else cout << c << " is not a lower case letter\n"; }

COMP104 Lecture 21 / Slide 9 Characters: Example 2 // program to beep at user #include using namespace std; void main(){ cout << '\7';// print ASCII code 7 cout << '7';// print character '7' }

COMP104 Lecture 21 / Slide 10 White Space  cin >> c skips any white space (spaces, tabs, newlines) * Example: char c = ' '; while(c != '\n') cin >> c; cout << c; } * Input: a b c d * Output: abcd

COMP104 Lecture 21 / Slide 11 Difference between cin.get(ch) and >> * How to keep white spaces?  Use cin.get(ch): char ch = ' '; while(ch != '\n') cin.get(ch); // ch is passed by reference // ch is set to the input value cout.put(ch); // ch is passed by value // just print it out; // same as: cout << ch; } * Input:a b c d * Output: a b c d * cin.get(ch) reads white space just like other characters!!

COMP104 Lecture 21 / Slide 12 cin.get(c) cin.get (char&) read a single character cout.put (char) write a single character  Let's say the user inputs a word, 'hello'. cin.get(c) will return the letter 'h'. The rest of the word is not lost, but stays in the stream. * If we perform another cin.get operation, we will get the next letter, 'e'.  cout.put(c) simply outputs one letter at a time (same as cout << c ).

COMP104 Lecture 21 / Slide 13 cin.get(c): Example 1 // Program to count the number of input blanks // (Program outputs characters without blanks) #include using namespace std; void main() { char next; int count = 0; do{ cin.get(next); if(next == ' ') count++; else cout.put(next); }while(next != '\n'); cout << "Number of blanks = " << count << endl; }

COMP104 Lecture 21 / Slide 14 cin.get(c): Example 1 Input: a Output: a Number of blanks = 0 Input: ab cd Output: abcd Number of blanks = 1 Input: Output: Number of blanks = 8

COMP104 Lecture 21 / Slide 15 cin.get(c): Example 2 /* Reads a string of characters and converts the digits in the string to int type */ #include using namespace std; int read_int(); void main() { int number; cout << "Enter a line of digits followed by enter: "; number = read_int(); cout << "The numerical value of the digits" << " in the line is: \n" << number << endl; }

COMP104 Lecture 21 / Slide 16 cin.get(c): Example 2 int read_int(){ const char nwln = '\n'; char next; int digit; int value = 0; do{ cin.get(next); if(isdigit(next)){ digit = int(next) - int('0'); value = 10*value + digit; } }while(next != nwln); return value; }

COMP104 Lecture 21 / Slide 17 cin.get(c): Example 2 Enter a line of digits followed by enter: 1a234 The numerical value of the digits in the line is: 1234 Enter a line of digits followed by enter: The numerical value of the digits in the line is: Enter a line of digits followed by enter: The numerical value of the digits in the line is: (overflows at about billion with 32-bit integers)

Strings

COMP104 Lecture 21 / Slide 19 Strings * Strings are a special data type used to store a sequence of characters * Include the library to use strings  Compare strings with the <, ==, and != operations * Concatenate strings with the + operation  Use s.substr(position, size) to get a substring from a string s starting from position, and of length size  Use s.find(subs) to find where a substring subs begins in string s

COMP104 Lecture 21 / Slide 20 Strings: Example 1 #include #include // string library using namespace std; int main(){ string name; cout << "Enter name (without spaces): "; cin >> name; cout << "Name: " << name << endl; } * example input/output: Enter name (without spaces): ChanTaiMan Name: ChanTaiMan

COMP104 Lecture 21 / Slide 21 Strings: Example 2 #include #include // string library using namespace std; int main(){ string s = "Top "; string t = "ten "; string w; string x = "Top 10 Uses for Strings"; w = s + t; cout << "s: " << s << endl; cout << "t: " << t << endl; cout << "w: " << w << endl; cout << "w[5]: " << w[5] << endl;

COMP104 Lecture 21 / Slide 22 Strings: Example 2 if(s < t) cout << "s alphabetically less than t" << endl; else cout << "t alphabetically less than s" << endl; if(s+t == w) cout << "s+t = w" << endl; else cout << "s+t != w" << endl; cout << "substring where: " << x.find("Uses") << endl; cout << "substring at position 12 with 7 characters: " << x.substr(12, 7) << endl; return 0; }

COMP104 Lecture 21 / Slide 23 Strings: Example 2 * Example output: s: Top t: ten w: Top ten w[5]: e s alphabetically less than t s+t = w substring where: 7 substring at position 12 with 7 characters: for Str