#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cassert>
using namespace std;
int main ()
{
char str[] = {'h', 'e','l', 'l', 'o', '\0'};
//
// Prevent use with an empty array
if (std::size(str) > 1)
{
//
// Only allow null terminated strings.
assert( str[std::size(str) - 1] == '\0' );
//
//
char * pch = strrchr(str,'s');
//
//
std::cout << "Length of the array is " << std::size(str) << "\n";
//
// No character was found, don't attempt to access null.
if (pch != nullptr)
{
auto X = pch - &str[0];
cout<<str<<endl;
cout<<"Last time 's' was found was in position: "<< pch - &str[0] <<endl;
}
else
{
cout<<"The character didn't exist in the string!\n";
}
//
// There skip the last null character and get the last character
// No null character, not compensation needed
cout<<"Last character in this example is "<< str[ std::size(str) - 2 ];
}
else
{
cout << "Cannot process empty string\n";
}
return 0;
}