Consecutive elements
PROBLEM :
For a given string delete the elements which are appearing more than once consecutively. All letters are of lowercase.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, a string will be inserted.
Output:
In each seperate line the modified string should be output.
Constraints:
1<=T<=31
1<=length(string)<=100
Example:
Input:
1
aababbccd
Output:
ababcd
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
string Consecutive_elements(string ) ;
int main()
{
int t ;
string str ;
cin>>t ;
while(t--)
{
cin>>str ;
str=Consecutive_elements(str) ;
cout<<str<<endl ;
}
return 0;
}
string Consecutive_elements(string str)
{
int l,i=0 ;
l=str.length() ;
while(str[i]!='\0')
{
if(str[i]==str[i+1])
str.erase(i,1) ;
else
i++ ;
}
return str ;
}
---------------------------------------------------------------------------------
For a given string delete the elements which are appearing more than once consecutively. All letters are of lowercase.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, a string will be inserted.
Output:
In each seperate line the modified string should be output.
Constraints:
1<=T<=31
1<=length(string)<=100
Example:
Input:
1
aababbccd
Output:
ababcd
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
string Consecutive_elements(string ) ;
int main()
{
int t ;
string str ;
cin>>t ;
while(t--)
{
cin>>str ;
str=Consecutive_elements(str) ;
cout<<str<<endl ;
}
return 0;
}
string Consecutive_elements(string str)
{
int l,i=0 ;
l=str.length() ;
while(str[i]!='\0')
{
if(str[i]==str[i+1])
str.erase(i,1) ;
else
i++ ;
}
return str ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment