C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.

Slides:



Advertisements
Similar presentations
Test 2 Jason Jenkins Lauren Sears Aaron Stricklin Brandon Wolfe Julie Alexander.
Advertisements

CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Lecture 2 Introduction to C Programming
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
1 Week 1304/07/2005Course ISM3230Dr. Simon Qiu  Learn how to create and manipulate enumeration type  Become aware of the typedef statement  Learn about.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 9: Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Chapter 9: Arrays and Strings
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
C++ Programming:. Program Design Including
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Programming: Basic Elements of C++.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CHAPTER 8 USER-DEFINED SIMPLE DATA TYPES, NAMESPACES, AND THE string TYPE.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 2: Basic Elements of C++
Objectives In this chapter, you will:
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Java Programming: From Problem Analysis to Program Design, 4e
User-Defined Functions
Chapter 14: Exception Handling
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of Java
Chapter 8: The string Type
Engineering Problem Solving with C++, Etter
Objectives In this chapter, you will:
Presentation transcript:

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type

Objectives In this chapter you will: Become familiar with the typedef statement Learn about the namespace mechanism Explore the string data type, and learn how to use the various string functions to manipulate strings

typedef Statement You can create synonyms or aliases to a previously defined data type by using the typedef statement The syntax of the typedef statement is: typedef does not create any new data types typedef creates an alias to an existing data type

ANSI/ISO Standard C++ ANSI/ISO standard C++ was officially approved in July 1998 Most of the recent compilers are also compatible with ANSI/ISO standard C++ For the most part, standard C++ and ANSI/ISO standard C++ are the same, but −ANSI/ISO Standard C++ has some features not available in Standard C++

Namespaces When a header file, such as iostream, is included in a program −Global identifiers in the header file also become global identifiers in the program If a global identifier in a program has the same name as one of the global identifiers in the header file −The compiler will generate a syntax error (such as identifier redefined) The same problem can occur if a program uses third party libraries

Namespaces (continued) To overcome this problem, third party vendors begin their global identifiers with a special symbol Because compiler vendors begin their global identifier with _ (underscore) −To avoid linking errors, do not begin identifiers in your program with _ ANSI/ISO standard C++ attempts to solve this problem of overlapping global identifier names with the namespace mechanism

Syntax: namespace The syntax of the statement namespace is: where a member is usually a variable declaration, a named constant, a function, or another namespace

Accessing a namespace Member The scope of a namespace member is local to the namespace Usually two ways a namespace member can be accessed outside the namespace One way is to use the syntax: namespace_name::identifier To access the member rate of the namespace globalType, the following statement is required: globalType::RATE

Accessing a namespace Member (continued) To access the function printResult, the following statement is required: globalType::printResult(); To simplify the accessing of all namespace members: using namespace namespace_name; To simplify the accessing of a specific namespace member: using namespace_name::identifier;

The using Statement After the using statement −Not necessary to precede the namespace_name and the scope resolution operator before the namespace member If a namespace member and a global identifier or a block identifier have the same name − namespace_name and scope resolution operator must precede the namespace member

The string Type To use the data type string, the program must include the header file The statement: string name = "William Jacob"; declares name to be a string variable and also initializes name to "William Jacob" The first character, 'W', in name is in position 0; the second character, 'i', is in position 1, and so on

The string Type (continued) The variable name is capable of storing any size string Binary operator + (to allow the string concatenation operation), and the array subscript operator [], have been defined for the data type string For example, If str1 = "Sunny", the statement stores the string "Sunny Day" into str2 : str2 = str1 + " Day";

length Function Length returns the number of characters currently in the string The syntax to call the length function is: strVar.length() where strVar is variable of the type string length has no arguments length returns an unsigned integer The value returned can be stored in an integer variable

size Function The function size is same as the function length Both functions return the same value The syntax to call the function size is: strVar.size() where strVar is variable of the type string As in the case of the function length, the function size has no arguments

find Function find searches a string for the first occurrence of a particular substring Returns an unsigned integer value of type string::size_type giving the result of the search The syntax to call the function find is: strVar.find(strExp) where strVar is a string variable and strExp is a string expression evaluating to a string The string expression, strExp, can also be a character

find Function (continued) If successful, find returns the position in strVar where the match begins For the search to be successful the match must be exact If unsuccessful, find returns the special value string::npos (“not a position within the string”)

substr Function substr returns a particular substring of a string The syntax to call the function substr is: strVar.substr(expr1,expr2) where expr1 and expr2 are expressions evaluating to unsigned integers

substr Function (continued) The expression expr1 specifies a position within the string (starting position of the substring) The expression expr2 specifies the length of the substring to be returned

swap Function swap interchanges the contents of two string variables The syntax to use the function swap is strVar1.swap(strVar2); where strVar1 and strVar2 are string variables Suppose you have the following statements: string str1 = "Warm"; string str2 = "Cold"; After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "War"

Programming Example: Pig Latin Strings Program prompts user to input a string −Then outputs the string in the pig Latin form The rules for converting a string into pig Latin form are as follows: 1.If the string begins with a vowel, add the string "-way" at the end of the string −For example, the pig Latin form of the string "eye" is "eye-way"

Pig Latin Strings (continued) 2.If the string does not begin with a vowel, first add "-" at the end of the string −Then move the first character of the string to the end of the string until the first character of the string becomes a vowel −Next, add the string "ay" at the end − For example, the pig Latin form of the string "There" is "ere-Thay"

Pig Latin Strings (continued) 3.Strings such as "by" contain no vowels −In cases like this, the letter y can be considered a vowel −For this program the vowels are a, e, i, o, u, y, A, E, I, O, U, and Y the pig Latin form of "by" is "y-bay " 4.Strings such as "1234" contain no vowels −The pig Latin form of a string that has no vowels in it is the string followed by the string "-way " −For example, the pig Latin form of the string "1234" is " 1234-way "

Problem Analysis If str denotes a string −Check the first character, str[0], of str −If str[0] is a vowel, add "-way" at the end of str −If the first character of str, str[0], is not a vowel First add "-" at the end of the string Remove the first character of str from str and put it at end of str Now the second character of str becomes the first character of str

Problem Analysis (continued) −This process is repeated until either The first character of str is a vowel All characters of str are processed, in which case str does not contain any vowels

Algorithm Design The program contains the following functions: − isVowel - to determine whether a character is a vowel − rotate - to move first character of str to the end of str − pigLatinString - to find the pig Latin form of str Steps in the Algorithm: 1.Get str 2.Use the function pigLatinString to find the pig Latin form of str 3.Output the pig Latin form of str

Function rotate Takes a string as a parameter Removes the first character of the string −Places it at end of the string by extracting the substring starting at position 1 until the end of the string, then adding the first character of the string

Function rotate (continued)

Function pigLatinString If pStr[0] is a vowel, add "-way" at end of pStr If pStr[0] is not a vowel −Move the first character of pStr to the end of pStr −The second character of pStr becomes the first character of pStr Now pStr may or may not contain a vowel −Use a bool variable, foundVowel, which is set to true if pStr contains a vowel and false otherwise −Initialize foundVowel to false

Function pigLatinString (continued) −If pStr[0] is not a vowel, move str[0] to the end of pStr by calling the function rotate −Repeat third step until either the first character of pStr becomes a vowel or all characters of pStr have been checked Convert pStr into the pig Latin form Return pStr

Main Algorithm 1.Get the string 2.Call the function pigLatinString to find the pig Latin form of the string 3.Output the pig Latin form of the string

Summary (continued) The keyword namespace must appear in the using statement A string is a sequence of zero or more characters Strings in C++ are enclosed in double quotation marks In C++, [] is called the array subscript operator The function length returns the number of characters currently in the string

Summary (continued) The function size returns the number of characters currently in the string The function find searches a string to locate the first occurrence of a particular substring The function substr returns a particular substring of a string The function swap is used to swap the contents of two string variables