C++ Programming Basics

Slides:



Advertisements
Similar presentations
Chapter 9 Strings. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives An Array Type for Strings C-Strings Character.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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:
Chapter 10.
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.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 9: Arrays and Strings
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
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.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Chapter 15 Strings as Character Arrays
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Sahar Mosleh California State University San MarcosPage 1 Character String.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Strings, Slide Fundamental Programming Strings.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Topic Pre-processor cout To output a message.
String class and its objects
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
Arrays Arrays A few types Structures of related data items
Presentation transcript:

C++ Programming Basics Arrays

Array Array – data structure with homogeneous data type a set of data represented by a single variable name Element – individual data contained in an array Declaration of array – type name [ index # ] ; Index number (or subscript) is used to identify each element – index number begins with 0, not 1 char array – single quote ( ‘ ) is used Text string – double quote ( “ ) Initialization with text string entails \0 (end mark) at the end

char Grades[5]; // Grades with 5 elements Array example char Grades[5]; // Grades with 5 elements Grades[0] = ‘A’ ; // first element Grades[1] = ‘B’ ; Grades[2] = ‘C’ ; Grades[3] = ‘D’ ; Grades[4] = ‘F’ ; // 5th element Array declaration with initialization char Grades[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘F’} ; char Grades[] = {‘A’, ‘B’, ‘C’, ‘D’, ‘F’} ; char Grades[5] = “ABCDF” ; // error char Grades[] = “ABCDF” ;

String Variable Declaration syntax char string_name [size] ; Empty string variable with character size of [size] char string_name [size] = “Hello” ; String variable with initial value of “Hello” In this case, size can be omitted Text string can be assigned to string variable only when it is declared. Once declared, text string can not be assigned to the string variable with a normal assignment operator ( = ). 4

String Variable How to display a string variable char strName[20] = “Smith” ; cout << strName << endl ; cout << strName[4] << endl; Even though string variable is a char array, it can be used as a normal variable, ignoring any array features it can be used like a char array, too 5

String Manipulation strcat(dest_variable, string) – append one string to another strchr(string, character) – find the first occurrence of a specified character in a string strcmp(string1, string2) – compare two string strcpy(dest_variable, string) – replace the contents of one string with the contents of another strcspn(string, char_set) – find the first occurrence in a string of a specified character within a specified character set strlen(string) – return the length of a string 6

strncat(dest_variable, source_char, source_max_size) – append characters to a string strncmp(string1, string2, max_size) – compare first max_size characters within two strings strncpy(dest_variable, string, max_size) – copy first max_size characters from one string to another strpbrk(string, char_set) – find the first occurrence in a string of a specified character set in another string strrchr(string, char) – find the last occurrence in a string of a specified character strspn(string, char_set) – find the first occurrence of not-specified char in character set strstr(string, source_string) – find the first occurrence of source string in another specified string 7

String Pointer Variable String pointer – use of a pointer that indirectly points to a series of characters String pointer is not a fixed size, so any size of text string can be assigned with an assignment operator Syntax char *strPointer ; strPointer = “Hello”; 8

String Object Variable String object – string class is not a normal data type; it can hold any size of text string Requires <string> header String object is also a char array, so it can be used like an array Syntax string strObject ; strObject = “Hello” ; cout << strObject << endl; cout << strObject[4] << endl; 9