Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Write a function to perform string operation i.e. split, join, length, substring using pointer without using any string function

Posted By: Lorelei Schmidt     Category: C Programming     Views: 5690

Write a function to perform string operation i.e. split, join, length, substring using pointer without using any string function.

Code for Write a function to perform string operation i.e. split, join, length, substring using pointer without using any string function in C Programming

#include <stdio.h>
#include <conio.h>
#include "valid.c"void getString(char *);

int my_split(char *str, int pos, char *new_str1, char *new_str2);
int my_join(char *str1, char *str2 , char *dest_str);
int my_strlen(char *str);
int my_substr(char *source_str, char *search_str);

char *str1;
char *str2;
char *dest_str;
char *source_str;
char *source_str1;
char *source_str2;
char *source_str3;
char *findstr;

void main()
{
  int val=0;
  fflush(NULL);
   source_str = (char *) malloc(sizeof(char)*10);
   source_str1 = (char *) malloc(sizeof(char)*10);
   source_str2 = (char *) malloc(sizeof(char)*10);
   source_str3 = (char *) malloc(sizeof(char)*10);
   findstr = (char *) malloc(sizeof(char)*10);
   dest_str = (char *) malloc(sizeof(char)*10);
   str1 = (char *) malloc(sizeof(char)*10);
   str2 = (char *) malloc(sizeof(char)*10);

  clrscr();
  fflush(NULL);

   printf("\nEnter a string of maximun 10 character :");
   getString(source_str);
   printf("length : %d", my_strlen(source_str));

   my_split(source_str,4, str1, str2);
   printf("\n%s \t %s ", str1, str2);


   printf("\nEnter a First string of maximun 10 character :");
   getString(source_str1);
   printf("\nEnter a Second string of maximun 10 character :");
   getString(source_str2);

   my_join(source_str1, source_str2, dest_str);
   printf("\n joining the string is : %s", dest_str);

   printf("\nEnter a string of maximun 10 character :");
   getString(source_str3);

   printf("\nEnter a search string of maximun 10 character to find :");
   getString(findstr);

   val = my_substr(source_str3, findstr);

   if(val)
     printf("\nsearch string found");
   else
     printf("\nsearch string not found");

   getch();
}

   int my_substr(char *src_str, char *search_str)
   {
      char *tmpstr = src_str;
      char *search = search_str;
      int _flag = 0;
      int count = 0;
      int _searchlen = my_strlen(search_str);

      while( *tmpstr != '\0')
      {
     if( *tmpstr == *search)
     {
       for(count = 0; count < _searchlen-1; count++)
       {
          search++;
          tmpstr++;
          if ( *search != *tmpstr )
          {  _flag = 0;
          break;
          }
          else _flag=1;
       }
     }
     search = search_str;
     tmpstr++;
      }
      return _flag;
   }

    void getString(char *str)
    {
    int count=0;
    char *getstr;
    char readch='\0';
    fflush(NULL);
       getstr = str;
       while(1)
       {
        scanf("%c", &readch);
        if( readch == '\n')
           break;
        *getstr = readch;

        //if( ( str = (char *)realloc(str, sizeof(char)) ) == '\0')//   break;
        count++;
        getstr++;
        if( count >= 10)
          break;
       }
       *getstr='\0';
    }

   int my_split(char *_sourcestr, int pos, char *new_str1, char *new_str2)
   {
      int _tmpval = 0;
      char *str = _sourcestr;
      char *tmpchr1 = new_str1;
      char *tmpchr2 = new_str2;
      int _strlen =  my_strlen(str);

      if(*str != '\0')
      {
      if( pos < _strlen )
      {
           for(_tmpval=0; _tmpval < _strlen; _tmpval++)
           {
          if( _tmpval < pos)
          {
             *tmpchr1 = *str;
             tmpchr1++;
          }
          else
           {
             *tmpchr2 = *str;
             tmpchr2++;
           }
             str++;
           }
           *tmpchr1='\0';
           *tmpchr2='\0';
      }
      elsereturn _tmpval;
      }

      return _tmpval;
   }

   int my_join(char *str1, char *str2 , char *dest_str)
   {
      char *destr = dest_str;
      int _val = 0;

    if(my_strlen(str1) > 0 )
    {
          while( *str1 != '\0')
          {
         *destr = *str1;
         destr++;
         str1++;
         _val++;
          }
    }
    if(my_strlen(str2) > 0 )
    {
          while( *str2 != '\0')
          {
         *destr = *str2;
         destr++;
         str2++;
         _val++;
          }
    }
      *destr = '\0';
      return _val;
   }

   int my_strlen(char *str)
   {
      int _tmpval = 0;
      char *cstr = str;

      if(*cstr != '\0')
      {
     while(*cstr !='\0')
     {
        _tmpval++;
        cstr++;
     }
      }
      return _tmpval;
   }

     /* ****************** Input / Output ******************
I/P Enter a string of maximun 10 character : space-bar

O/P length : 9

I/P Splitting at position 4
O/P spac e-bar

I/P Enter a First string of maximun 10 character : space
I/P Enter a Second string of maximun 10 character : bar

O/P joining the string is : spacebar

I/P Enter a string of maximun 10 character : space-bar
I/P Enter a search string of maximun 10 character to find : bar

O/P search string found

*******************************************************/
  
Share: 



Lorelei Schmidt
Lorelei Schmidt author of Write a function to perform string operation i.e. split, join, length, substring using pointer without using any string function is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!