RSS

Write an algorithm that deletes consecutive 1, 2, and 3 and so on recurrences of characters

Fri, Mar 5, 2010

Algorithm

For example if aabbabc is given as input after first iteration, it should be ababc and after that it should become abc. if aabbcabc is given it should give abcabc after first interation, no change in second iteration and abc after third iteration.
#include
#include
using namespace std;
      
int strcomp(char *p,int i,int j,int  k)
{
         int loop;
         for(loop=0;loop          {
           if(p[loop+i]!=p[loop+j])
             return 0;
         }
         return 1;
}
void strcopy(char *p,int r,int k,int  j)
{
         int loop;
         for(loop=0;loop          {
           p[r++]=p[j++];
         }
         return;
}
void recremove(char *p,int k)
{
         int n=strlen(p);
         int i,j,r;
         if(k>n/2)
           return;
         i=0;j=k;r=k;
         while(i+2*k<=n)
         {
           if(!strcomp(p,i,j,k))
           {
             strcopy(p,r,k,j);
             r++;
             i++;
             j++;
           }
           else
           {
             i=i+k;
             j=i+k;
           }
         }
         while(j            p[r++]=p[j++];
 
 
         p[r]=’\0?;
         if(k==1)
           cout<<”\nString after first iteration is=”<<(char *)p;
         else if(k==2)
           cout<<”\nString after second iteration is=”<<(char *)p;
         else if(k==3)
           cout<<”\nString after third  iteration is=”<<(char *)p;
         else
           cout<<”\nString after “<          k=k+1;
         recremove(p,k);
}
      

int main()
{
         char p[100];
         cout<<”\nEnter the string=”;
         cin.getline(p, 100);
         recremove(p,1);
         return 0;
}

Sharing ~ Helping Other:
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • Slashdot
  • YahooMyWeb
  • StumbleUpon
  • Live
  • IndianPad
  • DotNetKicks
  • Technorati

Other Posts:

This post was written by:

eXclusiveMinds - who has written 500 posts on eXclusiveMinds.


Contact the author

Leave a Reply

You must be logged in to post a comment.