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.
- We will be using the string class, it does the same, just in a more intuitive manner.
Things to remember:
- We can use dynamic memory to declare strings. Obviously, we can use static allocationa s well.
- We can use array notation to change the characters, i.e strings are mutable in C++.
- We can take input using getline(cin, string_name);
- string_name is just a character_array_name. i.e cout << string_name displays the whole string.
- concatenation is done using + operator. += is also allowed. Append and prepend dealt with easily. You can append character to a string too.
- 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.
- This is better than python, coz our strings are mutable(unitwise-changeable).
- For knowing the size, string_name.size(); We can also use .length.
- **Substring function: **like splicing , substr(i): return the string from index i to the end.(incl) Does not change the original string.
- Modified substring: **substr(**starting_index, length_required). Does not change the original string.
- 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.
- 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.
- Dont’t think of unneccessary details.