M ANIPULATING S TRINGS Resources [1] Object Oriented Programming with C++ (3 rd Edition) E Balagurusamy [2] Teach Yourself C++ (3 rd Edition) H Schildt.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
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.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
AU/MITM/1.6 By Mohammed A. Saleh 1. Introducing the string Class  Instead of using a character array to hold a string, you can use a type string variable.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
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:
Strings in C++ 04/29/11. Quiz Arrays  Section 7.1 & 7.2  Array basics  Partially filled arrays  Passing individual elements Mon. 05/02/11.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
CS Nov 2006 C-strings.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Arrays and String Using array Initialized arrays of different.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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.
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.
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++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
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.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CSC 270 – Survey of Programming Languages
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 15 Strings as Character Arrays
Sahar Mosleh California State University San MarcosPage 1 Character String.
 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 Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter Characters, Strings, and the string class 10.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
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.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
JavaScript Objects.
Chapter 7: Strings and Characters
C Stuff CS 2308.
Null-Terminated Character Arrays
Programming with ANSI C ++
String class and its objects
Engineering Problem Solving with C++, Etter
C++ Programming Lecture 20 Strings
Modifying Objects Assignment operation Assignment conversions
Programming Strings.
Unit-2 Objects and Classes
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

M ANIPULATING S TRINGS Resources [1] Object Oriented Programming with C++ (3 rd Edition) E Balagurusamy [2] Teach Yourself C++ (3 rd Edition) H Schildt

I NTRODUCTION A string is a sequence of character. We have used null terminated arrays (C- strings or C-style strings) to store and manipulate strings. ANSI C++ provides a class called string. We must include in our program.

A VAILABLE O PERATIONS Creating string objects. Reading string objects from keyboard. Displaying string objects to the screen. Finding a substring from a string. Modifying string objects. Adding string objects. Accessing characters in a string. Obtaining the size of string. And many more.

C OMMONLY U SED S TRING C ONSTRUCTORS String(); // For creating an empty string. String(const char *str); // For creating a string object from a null-terminated string. String(const string &str); // For creating a string object from other string object.

C REATING S TRING O BJECTS string s1, s3; // Using constructor with no arguments. string s2(“xyz”);// Using one-argument constructor. s1 = s2;// Assigning string objects s3 = “abc” + s2;// Concatenating strings cin >> s1; // Reading from keyboard (one word) cout << s2;// Display the content of s2 getline(cin, s1) // Reading from keyboard a line of text s3 += s1;// s3 = s3 + s1; s3 += “abc”;// s3 = s3 + “abc”;

M ANIPULATING S TRING O BJECTS string s1(“12345”); string s2(“abcde”); s1.insert(4, s2);// s1 = 1234abcde5 s1.erase(4, 5);// s1 = s2.replace(1, 3, s1);// s2 = a12345e

M ANIPULATING S TRING O BJECTS insert() erase() replace() append()

R ELATIONAL O PERATIONS string s1(“ABC”); string s2(“XYZ”); int x = s1. compare (s2); x == 0 if s1 == s2 x > 0 if s1 > s2 x < 0 if s1 < s2 OperatorMeaning ==Equality !=Inequality <Less than <=Less than or equal >Greater than >=Greater than or equal

S TRING C HARACTERISTICS void display(string &str) { cout << “Size = ” << str.size() << endl; cout << “Length = ” << str.length() << endl; cout << “Capacity = ” << str.capacity() << endl; cout << “Max Size = ” << str.max_size() << endl; cout << “Empty: ” << (str.empty() ? “yes” : “no”) << endl; cout << endl << endl; }

S TRING C HARACTERISTICS FunctionTask size()Number of elements currently stored length()Number of elements currently stored capacity()Total elements that can be stored max_size()Maximum size of a string object that a system can support emply()Return true or 1 if the string is empty otherwise returns false or 0 resize()Used to resize a string object (effects only size and length)

A CCESSING C HARACTERS IN S TRINGS FunctionTask at()For accessing individual characters substr()For retrieving a substring find()For finding a specific substring find_first_of()For finding the location of first occurrence of the specific character(s) find_last_of()For finding the location of first occurrence of the specific character(s) [] operatorFor accessing individual character. Makes the string object to look like an array.

C OMPARING AND S WAPPING There is another overloaded version of compare int compare(int start_1, int length_1, string s_2, int start_2, int length_2) string s1, s2; int x = s1.compare(0, 2, s2, 2, 2); s1.swap(s2) Exchanges the content of string s1 and s2

L ECTURE C ONTENTS [1] Object Oriented Programming with C++ (3 rd Edition) E Balagurusamy Chapter 15 (Full) [2] Teach Yourself C++ (3 rd Edition) H Schildt Examples only Study the examples and exercise from both books carefully