Strings

 ʺaʺ versus ‘a’ --> 'a’ is a single character value (stored in 1 byte) while ʺaʺ is an array with two characters, the first is a, the second is \0 (null character).

char string_1[6] = ʺhelloʺ;                                       char string_2[6] = ʺhelloʺ;                         

But string1 does not equal string2 => (they are stored in different memory locations). 

char str[20] = "endsem";           OR         char str[20] = {'e', 'n', 'd', 's', 'e', 'm', '\0'};  
 output-  endsem (after 6 characters all are assigned '/0' null character)
" " - itself covers the '\0' character at end     NOTE:- minimum size of array needed is 7

some other stuffs
  • string name(5,'n'); => name = nnnnn ('n' as 5 times)   //same like vi v (n,0)
  • s.erase(5, 2) --> removes 2 letters from index 5
    • s.erase() --> removes all
    • s.erase(5) --> removes all from index 5
  • s1= "fam" 
    • s1.find("fa") => returns the index of first letter
      • if(s1.find() = = string :: npos) --> condition if not present
    • s1.insert(1,"jack") => s1 = fjackam - jack inserted at 1st index

  • to_string(int b) => converts any data type (int , float) to string
    • stoll(..) => converts string to long long int

  • min(s1, s2) --> lexicographically smaller one
  • s = s1+ s + s2 --> adds string in given order
  • s = s+ 'a';     // O(s.size()) bcz first make copy of s then add 'a'
  • s += 'a'      // Average time is O(1)

  • To add " " inside the string name ---> use \
    • string name = "this\"is\" highlighted"

  • get(name) => read input upto '\n' and keep '\n' in stream --> consider '\n' as next input
    • getline(cin,name) ; => reads input up to '\n' and stops
    • cin.getline(s1, 100); then cin.get(s2 , 100); --> finally print s1 and s2
      INPUT - 1 2        --> Output - 1 2 3 4
                     3 4
    • cin.get(s1, 100); then cin.getline(s2 , 100); --> finally print s1 and s2
      INPUT - 1 2        --> Output - 1 2 --> bcz 'get' stores '\n' in stream
                     3 4        thus, for getline => instead of 3 4 --> '\n' acts as input

Comments

Popular posts from this blog

Templates (for CP)

DSA Trees + Heap

Modular Basics + Number Theory + Permutations