Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy."— Presentation transcript:

1 Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team http://academy.telerik.com Telerik Software Academy

2 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

3

4  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

5  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

6  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' 012345Hello! index = s[index] = 6

7 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

8 Live Demo

9 Declaring, Creating, Reading and Printing

10  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

11  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

12  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

13  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

14  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

15 Live Demo

16

17  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

18 Live Demo

19

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

21 Live Demo

22

23  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

24 Live Demo

25

26  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”

27 Live Demo

28 форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен 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# Николай Костов - блог за програмиране http://algoacademy.telerik.com

29 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

30 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

31 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

32 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

33 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

34 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

35 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 http://www.devbg.org/forum/index.php the following information should be extracted: [protocol] = "http" [server] = "www.devbg.org" [resource] = "/forum/index.php" [protocol]://[server]/[resource] 35


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

Similar presentations


Ads by Google