Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

CS110 Programming Language I
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Chapter 10.
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
11 Introduction to Object Oriented Programming (Continued) Cats II.
UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
1 Chapter 10 Characters, Strings, and the string class.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
1 Principles of Computer Science I Honors Section Note Set 10 CSE 1341.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Chapter Characters, Strings, and the string class 10.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Characters, Strings, and the cstring Library
LESSON 2 Basic of C++.
Selection Control Structure
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Characters, Strings, and the cstring Library
Introduction to C++ October 2, 2017.
getline() function with companion ignore()
Quiz Next Monday.
Introduction to C++ Programming
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Strings A collection of characters taken as a set:
10.1 Character Testing.
String What it is Why it’s useful
Using string type variables
(Dreaded) Quiz 2 Next Monday.
Strings Skill Area 313 Part C
getline() function with companion ignore()
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Worksheet char my_word[5] ={‘c’, ‘l’, ‘a’, ‘s’, ‘s’}; Is it a character array or a string? Why? How can you display it to the screen? for(i=0; i<5; i++) cout<< my_word[i]<<“ “;

Worksheet char my_word[6] ={‘c’, ‘l’, ‘a’, ‘s’, ‘s’, ‘\0’}; Is it a character array or a string? Why? How can you display it to the screen? cout<< my_word;

Extraction Operator >> (char array) char word[3]; cout<<“ Please enter a 3-letter word”; // “cat” for ( int i=0; i < 3; i++) cin >> word[i]; cout<<“You have entered :” ; for ( i=0; i < 3; i++) cout << word[i]; // ”cat”

Extraction Operator >> (string) char word[4]; cout<<“ Please enter a 3-letter word”; // “cat” cin >> word; cout<<“You have entered :” ; cout << word; // ”cat”

Extraction Operator >> (string) char word[4]; cout<<“ Please enter a 3-letter word”; // “cat” for ( int i=0; i < 3; i++) cin >> word[i]; word[3]=‘\0’; //convert character array into string cout<<“You have entered :” ; cout << word; // ”cat”

Extraction Operator >> (char array) char phrase[10]; cout<<“ Please enter a sentence”;// “This is hard” for ( int i=0; i < 10; i++) cin >> phrase[i]; cout<<“You have entered :” ; for ( i=0; i < 10; i++) cout << phrase[i]; // ”Thisishard”

Extraction Operator >> (string) char phrase[10]; cout<<“ Please enter a sentence”;// “This is hard” cin >> phrase; cout<<“You have entered :” ; cout << phrase; // ”This”

getline( ) char my_string[10]; cin.getline(my_string, 10); //user enters hi my_string[0]=‘h’ my_string[1]=‘i’ my_string[2]=‘\0’ my_string[3]= garbage … my_string[9]= garbage cout<<my_string; Displayed: hi What is the length of this string???

getline( ) char my_string[10]; cin.getline(my_string, 10); //user enters “I am happy” my_string[0]=‘I’ my_string[1]=‘ ’ my_string[2]=‘a’ my_string[3]=‘m’ … my_string[8]=‘p’ my_string[9]=‘\0’ cout<<my_string; Displayed: I am happ

Function get( ) - page 104 cin.get( ) reads in a single character at a time. Will read any character. if you need to read the ENTER key, a space, or the tab key, you cannot use cin. You must use cin.get because cin.get will read any character. Example: char c1; cin.get(c1);

Function get( ) char phrase[12]; cout<<“ Please enter a sentence”;// “This is hard” for ( int i=0; i < 12; i++) cin.get(phrase[i]); cout<<“You have entered :” ; for ( i=0; i < 12; i++) cout << phrase[i]; // ”This is hard”

Initialization of Strings char my_word[6] ={‘c’, ‘l’, ‘a’, ‘s’, ‘s’, ‘\0’}; char my_word[6]=“class”; char my_word[ ]=“very interesting class”;

Worksheet Write a code segment that will prompt the user for his first name and his last name and display to the screen: “Hello, first_name last_name”. I.e. If user enters John Smith, the program should display Hello, John Smith

char first_name[20], last_name[20]; cout<<”\nEnter your first and last name ”; cin>>first_name >> last_name; cout<<”\nHello, “<< first_name <<” “<<last_name; OR char name[50]; cout<<”\nEnter your first and last name ”; cin.getline(name, 50); cout<<”\nHello, “<< name;

Worksheet Write a code segment that will prompt the user for his first name and his last name and display to the screen: “Hello, last_name first_name”. I.e. If user enters John Smith, the program should display Hello, Smith John.

Worksheet Write a function that will accept two strings and return true if they are equal in length and false if they are not.

Worksheet Write a function that will accept an integer array, its size and a “code” integer. The function should find the location of that code integer in the array and return its position (index) in the array or 999 if the code was not found in the array.