Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Touch and Gestures with Xamarin Forms
Telerik School Academy ASP.NET MVC.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Asya Georgieva Telerik Software Academy academy.telerik.com QA Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Web Applications in Hatch Tran Anh Tuan Edit from Telerik Academy
Accessing SQL Server and MySQL – Live Demo Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
NoSQL Concepts, Redis, MongoDB, CouchDB Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Course Introduction Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Telerik Software Academy End-to-end JavaScript Applications.
What is a Database, MySQL Specifics Trần Anh Tuấn Edit from Telerik Software Academy
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
Language enhancements and additions Learning & Development Team Telerik Software Academy.
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Subroutines in Computer Programming Telerik School Academy C# Fundamentals – Part 1.
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Telerik Software Academy Databases.
Integer, Floating-Point, Text Data, Variables, Literals Telerik Corporation
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
Creating and Initializing Arrays, Accessing Elements, Multiple Dimensions Learning & Development Team Telerik Software Academy.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Definition, Constructors, Methods, Access Modifiers, Static/Instance members, Learning & Development Team Telerik Software Academy.
Presentation transcript:

Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy

1. What is string? 2. String implementations in C++ 3. Creating and Using Strings  Declaring, Creating, Reading and Printing 4. String functions  Append, Insert, Erase, Find, Substring 5. Stringstream for converting strings 2

 Strings are sequences of characters  Each character is a Unicode symbol  Represented by the char[] or string  Example: string s = "Hello, C++!"; char c[] = "Hello, C++!"; char g[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; 4

 string comes from C++ and char[] is plain old C  String objects contain an mutable (read and write access) sequence of characters  Strings use Unicode to support multiple languages and alphabets  Strings are stored in the dynamic memory (managed heap)  string is reference type 5

 String objects are like arrays of characters ( char[] )  Have fixed length –  Have fixed length – size()  Elements can be accessed directly by index  The index is in the range [ 0... Length-1 ] string s = "Hello!"; int len = s.size(); // len = 6 char ch = s[1]; // ch = 'e' Hello! index = s[index] = 6

string s; char c[100]; cin >> s >> c; cout << s << " " << c << endl; int len = strlen(c); cout << len; for(int i = 0; i < len; i++) { cout << endl << c[i]; cout << endl << c[i];} cout << endl << s.size() << endl; for(int i = 0; i < s.size(); i++) { cout << s[i]; cout << s[i];} 7

Live Demo

Declaring, Creating, Reading and Printing

 Several ways of declaring string variables:  Using the keyword string  Using a char array  The above three declarations are equivalent string str1; char[length] str2; char g[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; char g[] = "Hello"; string str = "Hello"; 10

 Before initializing a string variable has null value  Strings can be initialized by:  Assigning a string literal to the string variable  Assigning the value of another string variable  Assigning the result of operation of type string  The same can be done on char array 11

 Not initialized variables has value of null  Assigning a string literal  Assigning from another string variable  Assigning from the result of string operation string s; // s is equal to null string s = "I am a string literal!"; string s2 = s; string s = "I am " + name + "!"; 12

 Several ways to create a string  string ()  string () - creates empty string - “”  string (other_string) - creates a string identical to other_string  string (other_string, position, count ) - creates a string that contains count characters  string (count, character) - create a string containing character repeated count times 13

 Reading strings from the console  Include iostream  Use cin string s; cin >> s; cout << s << s[i];  Printing strings to the console  Use cout 14

Live Demo

 These do not modify the string  const char * data () returns char array  const char * data () - returns char array  unsigned int length () - returns the length of the string  unsigned int size () - returns the length of the string  bool empty () - returns true if the string is empty, otherwise returns false 17

Live Demo

 These are available string operators   Assign =   Append +=   Indexing []   Concatenate +   Equality ==   Inequality !=   Comparison, = 20

Live Demo

 These are most common string functions   void swap ( other_string )   string & append ( other_string )   string & insert ( position, other_string )   string & erase ( position, count )   unsigned int find ( other_string, position )   string substr ( position, count ) 23

Live Demo

 String converting is done through stringstream  Include ssstream  It is used as cin and cout 26 stringstream ss; ss << 567; int a; ss >> a; // a = 567 string str = ss.str(); cout << str << endl; // str = “567”

Live Demo

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

1. Describe the strings in C#. What is typical for the string data type? Describe the most important methods of the String class. 2. Write a program that reads a string, reverses it and prints the result at the console. Example: "sample"  "elpmas". 3. Write a program to check if in a given expression the brackets are put correctly. Example of correct expression: ((a+b)/5-d). Example of incorrect expression: )(a+b)). 29

4. Write a program that finds how many times a substring is contained in a given text (perform case insensitive search). Example: The target substring is " in ". The text is as follows: The result is: 9. We are living in an yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days. 30

5. You are given a text. Write a program that changes the text in all regions surrounded by the tags and to uppercase. The tags cannot be nested. Example: The expected result: We are living in a yellow submarine. We don't have anything else. We are living in a YELLOW SUBMARINE. We don't have ANYTHING else. 31

6. Write a program that reads from the console a string of maximum 20 characters. If the length of the string is less than 20, the rest of the characters should be filled with '*'. Print the result string into the console. 7. Write a program that encodes and decodes a string using given encryption key (cipher). The key consists of a sequence of characters. The encoding/decoding is done by performing XOR (exclusive or) operation over the first letter of the string with the first of the key, the second – with the second, etc. When the last key character is reached, the next is the first. 32

8. Write a program that extracts from a given text all sentences containing given word. Example: The word is " in ". The text is: The expected result is: Consider that the sentences are separated by ". " and the words – by non-letter symbols. We are living in a yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days. We are living in a yellow submarine. We will move out of it in 5 days. 33

9. We are given a string containing a list of forbidden words and a text containing some of these words. Write a program that replaces the forbidden words with asterisks. Example: Words: "PHP, CLR, Microsoft" The expected result: Microsoft announced its next generation PHP compiler today. It is based on.NET Framework 4.0 and is implemented as a dynamic language in CLR. ********* announced its next generation *** compiler today. It is based on.NET Framework 4.0 and is implemented as a dynamic language in ***. 34

10. Write a program that parses an URL address given in the format: and extracts from it the [protocol], [server] and [resource] elements. For example from the URL the following information should be extracted: [protocol] = "http" [server] = " [resource] = "/forum/index.php" [protocol]://[server]/[resource] 35