Given a string S, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*".

PROBLEM
:-
Pair star
Send Feedback

Given a string S, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*".

Input format :
String S
Output format :
Modified string
Constraints :
0 <= |S| <= 1000
where |S| represents length of string S.
Sample Input 1 :
hello
Sample Output 1:
hel*lo
Sample Input 2 :
aaaa
Sample Output 2 :
a*a*a*a
SOLUTION:-
#include <iostream>
using namespace std;

// Change in the given string itself. So no need to return or print the changed string.
#include<string.h>
void pairStar(char input[]) {
    // Write your code here
    if(strlen(input)==1)
        return;
    pairStar(input+1);
    if(input[0]==input[1])
    {
        int i=strlen(input);
        while(i>=1)
        {
            input[i+1]=input[i];
            i--;
        }
        input[i+1]='*';
    }

}


int main() {
   char input[100];
   cin.getline(input, 100);
   pairStar(input);
   cout << input << endl;
}

Previous
Next Post »

If you have any doubts then please let me know... ConversionConversion EmoticonEmoticon