Header Ads

27 - WAP to Implement own Strtok(), Strstr(), Strcmp() Function's in C

This is a Program to implement own strtok(), strstr(), & strcmp() function.

strtok() - This function is basically used for splitting of a String using a delimiter
char *strtok(char *str, const char *delim)
strstr() - This function finds the first occurrence of the substring needle in the string haystack. The terminating '\0' characters are not compared.
char * strstr ( const char *, const char * );
strcmp() - This Function compares the 2 given strings.
int strcmp(const char *str1, const char *str2)
/* Program to implement your own Strtok, Strcmp, strstr function's.*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//My Strtok function
char *d_strtok(char *array, char *delim)
{
 //Declaring variables
 static char *nxt;
 int idx = 0, jdx = 0, flag = 0, fla = 0;

 //Assigning next address
 if (array == NULL)
 {
  array = nxt;
 }

 //Condition to return
 if (array[idx] == '\0')
 {
  return NULL;
 }
 //Loop to remove delimiters
 for (idx = 0; array[idx] != '\0'; idx++)
 {
  for (jdx = 0; delim[jdx] != '\0'; jdx++)
  {
   if (array[idx] == delim[jdx])
   {
    array[idx] = '\0';
    idx++;
    jdx = -1;
    fla = 1;
   }
   else
   {
    flag = 1;
   }
  }
  //Condition to returning the address
  if (fla && flag)
  {
   nxt = array + idx;
   return array;
  }
 }
 nxt = array + idx;
 return array;
}

//My strstr function
char *my_strstr(char *array, char *delim)
{
 int idx = 0, jdx = 0, flag = 0, flg = 0;

 //Loop to find the same string
 for (idx = 0; array[idx] != '\0'; idx++)
 {
  if (array[idx] == delim[jdx])
  { 
   //Flag to save the starting index
   flag = idx;
   //Loop to check all the characters
   for (jdx = 0; delim[jdx] != '\0'; jdx++)
   { 
    if (array[idx] != delim[jdx])
    {
     flg = 0;
     break;
    }
    if (array[idx] == delim[jdx])
    {
     //Flag to return
     flg = 1;
     idx++;
    }
   }

   if (flg == 1)
   {
   return array + flag;
   }
  }
 }
 return NULL;
}

//My Strcmp function
int my_strcmp(const char *str1, const char *str2)
{
 while (*str1 && *str2 && (*str1 == *str2))  
 { 
  str1++;
  str2++;
 }
 return *str1 - *str2;
}

//Main function
int main()
{
 int choice, cmp;
 char *array = malloc(100), *delim = malloc(20), *ptr, ch;

 while (1)
 {
  printf(" Select a Function\n 1.strtok\n 2.strstr\n 3.strcmp\n Enter your Option : ");
  scanf("%d", &choice);
  getchar();
  switch (choice)
  {
   case 1://My strtok function
    //Readin array from user
    printf(" Enter the String : ");
    scanf("%[^\n]", array);
    getchar();

    printf(" Enter the Delimiters : ");
    scanf("%[^\n]", delim);
    getchar();

    ptr = d_strtok(array, delim);
    while (ptr != NULL)
    {
     printf("%s\n", ptr);
     ptr = d_strtok(NULL, delim);
    }
    printf(" The String is %s\n", array);
    break;
   case 2://My strstr function
    //Readin array from user
    printf("Enter the Original String : ");
    scanf("%[^\n]", array);
    getchar();

    printf("Enter the String to be searched for : ");
    scanf("%s", delim);
    getchar();

    ptr = my_strstr(array, delim);
    if (ptr == NULL)
    {
     printf("%s is not found in %s\n", array, delim);
    }
    else
    {
     printf("%s\n", ptr);
    }
    break;
   case 3://My strcmp function
    //Readin array from user
    printf("Enter the Original String : ");
    scanf("%[^\n]", array);
    getchar();

    printf("Enter the String to be Compared for : ");
    scanf("%s", delim);
    getchar();

    cmp = my_strcmp(array, delim);

    printf("%d\n", cmp);
    break;
  }
    printf(" To continue press 'y' : ");
    ch = getchar();
    if (!(ch == 'y' || ch == 'Y'))
    {
     break;
    }
    getchar();
  }
  free(array);
  free(delim);
  return 0;
 }

Program Output:

 

No comments