4. Strings #

Created Wednesday 01 January 2020

Chuck the primary strings A. Use B if talking about stings, substrings. In this section, we are going to deal with strings. Using our basic null terminated character array is too redundant.

Things to remember:

  1. We can use dynamic memory to declare strings. Obviously, we can use static allocationa s well.
  2. We can use array notation to change the characters, i.e strings are mutable in C++.
  3. We can take input using getline(cin, string_name);
  4. string_name is just a character_array_name. i.e cout << string_name displays the whole string.
  5. concatenation is done using + operator. += is also allowed. Append and prepend dealt with easily. You can append character to a string too.
  6. Don’t forget to use the -> if you are using string.

Why are we switching to strings view? If we had to work with basic strings, we would’ve needed a 2D matrix and all.

  1. For knowing the size, string_name.size(); We can also use .length.
  2. **Substring function: **like splicing , substr(i): return the string from index i to the end.(incl) Does not change the original string.
  3. Modified substring: **substr(**starting_index, length_required). Does not change the original string.
  4. Find, s.find(key): returns the index correpsonding to the first occurrence of the key. returns -1 if not found. Can be used to seatch substrings.
  5. To check if string s is empty: s.empty(), returns true for empty string, false otherwise.

**Catch: ** Using substr to splice from index i to index j (excluding). Just do substr(i, j-i+1); No headers are required for any of these.