Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created February 8, 2016 07:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehzawad/55104fe4589744a3fd6c to your computer and use it in GitHub Desktop.
Save ehzawad/55104fe4589744a3fd6c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
// prototype in function
char* myStrCpy(char* dest, const char* src);
bool matching(const char* str1, const char* str2);
static char* myStrCat(char* buffer, const char* readOnlyStr);
int positionIndex(const char* fullStr, const char* portionString);
static const char* myStrStr(const char* s1, const char* s2);
void myStrPosLen(const char* s1, int position, int length);
static char* myStrChr(const char* buffer, int firstOccurence);
size_t myStrLength(const char* buffer);
void insertString(const char* aa,const char* bb, int pos);
int main(void)
{
char chunk[150] = "American International";
const char* str = " University Bangladesh";
std::cout << myStrCat(chunk, str) << std::endl;
myStrPosLen("Hello This is Crucial Platform !", 13, 6);
const char* fullString = "I am a student of AIUB";
const char* porString = "of";
int index = positionIndex(fullString, porString);
if (index != -1) {
std::cout << "the substring is found in the position of " << index << std::endl;
}
else {
std::cout << "the substring is not there\n";
}
insertString("hello, This is a lab and very crucial one!"," data structure", 16);
}
static char* myStrCat(char* buffer, const char* readOnlyStr)
{
char* crawler = buffer;
// loop through the fixed given string
while (*crawler != '\0') {
crawler++;
}
// dereferencing the readOnlyStr back to first string
while ((*crawler = *readOnlyStr) != '\0') {
crawler++;
readOnlyStr++;
}
return buffer;
}
int positionIndex(const char* fullStr, const char* portionString)
{
const char* crawler = fullStr;
int counter = 1;
while (*crawler != '\0') {
crawler++;
counter++;
if (*crawler == *portionString && matching(crawler, portionString)) {
return counter;
break;
}
}
return -1;
}
static const char* myStrStr(const char* s1, const char* s2)
{
if (*s2 == '\0')
return ((char*)s1);
for (; (s1 = myStrChr(s1, *s2)) != NULL; ++s1) {
const char* sc1;
const char* sc2;
for (sc1 = s1, sc2 = s2;;) {
if (*++sc2 == '\0')
return ((char*)s1);
else if (*++sc1 != *sc2)
break;
}
}
return NULL;
}
bool matching(const char* str1, const char* str2)
{
if (myStrStr(str1, str2)) {
return true;
}
else {
return false;
}
}
static char* myStrChr(const char* buffer, int firstOccurence)
{
while (*buffer != (char)firstOccurence) {
if (*buffer == '\0') {
return NULL;
}
else {
buffer++;
}
}
return (char*)buffer;
}
char* myStrCpy(char* dest, const char* src)
{
char* returnval = dest;
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}
*dest = *src;
return returnval;
}
void myStrPosLen(const char* s1, int position, int length)
{
char dest[50] = "";
myStrCpy(dest, s1);
for (int i = position; i < length + position; i++) {
std::cout << dest[i];
}
std::cout << std::endl;
}
void insertString(const char* aa, const char* bb, int pos)
{
size_t srclen = myStrLength(bb);
size_t dstlen = myStrLength(aa);
// char* m = (char*)malloc(srclen + dstlen + 1);
char dest[50] = "";
char* temp = myStrCpy(dest, bb);
std::cout << std::endl;
for (int i = 0; i < 16; i++)
{
std::cout << aa[i];
}
std::cout << temp ;
for( size_t i = 0 ; i < srclen + dstlen; i++)
{
std::cout << aa[i + 16];
}
}
size_t myStrLength(const char* buffer)
{
static size_t length = 0;
while (*buffer != '\0') {
buffer++;
length++;
}
return length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment