String manipulator :

You are given a string and a character. The requirement is to remove all instances of the given character from their original positions in this string, and dump all of them at the end of string. For example, if the input string is Sachin Ramesh Tendulkar and the given character is a, then the expected output is Schin Rmesh Tendulkraaa – note that all the three occurrences of the character a are removed from their original positions, and dumped at the end of string. The operation must be case-insensitive. Nothing happens if the character is not found in original string. Develop a program to read a string and a character from the user, and perform the operation described above.
Constraints : (i) Only one pass to be done over the array – that is, all elements of the array can be read only once, and only one loop is allowed. So, O(N) time is needed. (ii) No extra memory is allowed – the original array has to be modified in-place. No extra array can be created. Only O(1) amount of extra memory is permitted for indexing variables for loops etc.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{clrscr();
char ar[50],x;
int i,l,n;
cout<<"enter string : β€œ;
gets(ar);
n=l=strlen(ar);
cout<<”\nenter the character : β€œ;
cin>>x;
for(i=0;i<l;++i)
{
if(ar[i]==x)
{
for(int j=i;j<n-1;++j)
ar[j]=ar[j+1];
ar[j]=x;
}
}
cout<<”\nthe new string is : "<<ar;
getch();
}

1 Like

Could you please make your question title a little more meaningful?

Is this homework?

The question is that u have given a string like sachin ramesh tendulkar.from this string u have to remove all repeated characters,it w’l give u ans like this s chin r mesh tendulk r.now u have to also remvoe blank space then it w’l see as schin rmesh tendulkr.now u have 2 add all repeated characters behind the tendulkr.
so kindly help me how may i add all repeated characters behind the last word tendulkr.

yeah,itz my home work.plz help me…!!!

Thank u very much.i got it.Where i was making mistakes.

Your Welcome…! :slight_smile: