Programming Strings.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

1 Objectives Understand streamed input and output.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
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.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
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.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
String as Arrays, Array Sorting C++ Programming Technologies.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
LESSON 2 Basic of C++.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
Strings. Using strings as abstract value A string is a sequence of characters e.g. “Hello, World!” Early version of C++ followed the older C language.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Problems With Character Arrays
Command Line Arguments
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++ October 2, 2017.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
Quiz Next Monday.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 2 Elementary Programming
Random Number Generation
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Learning Objectives String Class.
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.
מחרוזות-String בשפת C++ ישנו תפקיד מיוחד למערך מסוג char רצף של תווים הנמצאים במערך מסוג char המסתיימת בתו אפס (הכוונה לאפס ממש '0\' , ולא לתו '0')
Strings A collection of characters taken as a set:
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Code::Block vs Visual C++
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
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.
Wednesday 09/23/13.
Chapter 4: Control Structures I (Selection)
Jeff West - Quiz Section 4
CS1201: Programming Language 2
Arrays of Two-Dimensions
Let’s all Repeat Together
Reading from and Writing to Files
Using string type variables
Pointers & Functions.
(Dreaded) Quiz 2 Next Monday.
Introduction to Functions
Functions Divide and Conquer
CS31 Discussion 1D Winter19: week 4
An Introduction to STL.
Reading from and Writing to Files
CSE Module 1 A Programming Primer
Presentation transcript:

Programming Strings

Strings Strings are a special data type used to store a sequence of characters Include the <string> 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

Strings: Example 1 example input/output: #include <iostream> #include <string> // 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): AndrewHorner Name: AndrewHorner

Strings: Example 2 #include <iostream> #include <string> // 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;

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; 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; }

Strings: Example 2 Example output: What about x.find("None")? 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 What about x.find("None")? If the substring is not found, find returns a number that is larger than any legal position within the string.