My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program to remove the first and last occurance of a character in the string

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{  clrscr();
   char s[500],c;
   int l,i=0,p=0;
   cout<<"enter the string: \n";
   gets(s);
   cout<<"enter the character: ";
   cin>>c;
   l=strlen(s);
   while(s[i]!='\0')
   {  if(s[i]==c)
      {  for(int j=i;j<l;j++)
     { s[j]=s[j+1]; }
     p++;
     break;
      }
     i++;
   }
   i=l;
   while(i>=0)
   {  if(s[i]==c)
      {   for(int j=i;j<l;j++)
      {  s[j]=s[j+1];  }
      p++;
      break;
      }
      i--;
    }
    if(p==0)
    { cout<<"there is no occurance in the string \n";
      getch();
      exit(0);
    }
    if(p==1)
    cout<<"there is only one occurance at the begning which is removed \n";

    cout<<"string after romoving the occurance is: \n"<<s;
    getch();
    }

0 comments: