Danny's Tech Musings



String Operations II

posted under , , , , by Anonymous


#include <cctype>
#include <string>

#include <cstring>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstdlib>

//Returns whether the given string starts with the specified string

bool startsWith( const std::string &s, const std::string &start )
{
size_t sSize = s.length(),

startSize = start.length();

bool starts = sSize >= startSize;

if( starts )

for( size_t i = 0; i < startSize; ++i )
if( start[i] != s[i] )
{
starts = false;

break;
}

return starts;

}


//Returns whether the given string ends with the specified string
bool endsWith( const std::string &s, const std::string &end )
{

size_t sSize = s.length(),
endSize = end.length();

bool ends = sSize >= endSize;

if( ends )

{
for( size_t i = endSize-1; i >= 0; --i )
if( end[i] != s[i] )
{

ends = false;
break;
}
}


return ends;
}

//Returns whether the given string starts with the specified string
//(Ignores case)
bool istartsWith( const std::string &s, const std::string &start )

{
size_t sSize = s.length(),
startSize = start.length();

bool starts = sSize >= startSize;


if( starts )
for( size_t i = 0; i < startSize; ++i )
if( tolower(start[i]) != tolower(s[i]) )
{

starts = false;
break;
}

return starts;


}


//Returns whether the given string starts with the specified string
//(Ignores case)
bool iendsWith( const std::string &s, const std::string &end )

{
size_t sSize = s.length(),
endSize = end.length();

bool ends = sSize >= endSize;


if( ends )
{
for( size_t i = endSize-1; i >= 0; --i )
if( tolower(end[i]) != tolower(s[i]) )

{
ends = false;
break;
}
}


return ends;
}

0 comments

Make A Comment
top