charat in c++ code example

Example 1: index string c++

#include <string>
#include <iostream>

int main(){
  //index string by using brackets []
  std::string string = "Hello, World!";
  //assign variable to string index
  char stringindex = string[2];
  
}

Example 2: c++ string element access

// string::operator[]
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  for (int i=0; i<str.length(); ++i)
  {
    std::cout << str[i];
  }
  return 0;
}

Example 3: c++ char define

// syntax: 
// char <variable-name>[] = { '<1st-char>',  '<2nd-char>', ... , '<Nth-char>', '\0'}; 

// example (to store 'Hello' in the YourVar variable): 
char YourVar[] = {'H','e','l','l','o','\0'}; // NOTE: the \0 marks the end of the char array

Example 4: c++ declare char

// syntax: 
// char <variable-name>[] = "<string/char-you-want-to-store>"; 

// example (to store 'Hello!' in the YourVar variable): 
char YourVar[] = "Hello!";

Example 5: stoi in c++

stoi()
//a function used to convert string to integer!

Example 6: how to access the element of string in c++

string myString = "Hello";
cout << myString[0];
// Outputs H