Given a string with elements having lowercase letters and question marks. Replace each question mark with lowercase letter such that the element is not equal to the element before it and the element after it. Ex:- Input String:- abcab??bac? Output:- abcabacbaca (There can be a lot of different outputs, anyone of them was accepted) Input String:- ???????? Output:- abcdefgh
SOLUTION:
abcab??bac?
OUTPUT:
- using System;
- using System.Linq;
- public class RemoveQMark
- {
- public string getcleantext(string text)
- {
- char[] textarray = text.ToCharArray();
- int n = text.Length;
- char[] cleantextarray = new char[n];
- int j=0;
- for(int i=0;i<n;i++)
- {
- if(textarray[i] != '?')
- {
- cleantextarray[j]= textarray[i];
- j++;
- }
- }
- string cleantext =new String(cleantextarray.Where(c => !char.IsWhiteSpace(c)).Distinct().ToArray());
- string clean =cleantext.Trim();
- return clean;
- }
- }
- public class GetNewString
- {
- public void getNewText(string cleantext,string text)
- {
- char[] cleantextarray = cleantext.ToCharArray();
- char[] originaltext = text.ToCharArray();
- int n = text.Length;
- int m=cleantext.Length;
- char[] endtextarray = new char[text.Length];
- for(int i=0;i<n;i++)
- {
- if(originaltext[i] == '?')
- {
- for(int j=0;j<m-1;j++)
- {
- if (i!=0 && i!=(n-1) && (cleantextarray[j]!=originaltext[i-1]) && (cleantextarray[j]!=originaltext[i+1]))
- {
- if(endtextarray[i-1]!=cleantextarray[j] && endtextarray[i+1]!=cleantextarray[j])
- {
- endtextarray[i] = cleantextarray[j];
- }
- else
- {
- break;
- }
- }
- else if(i==0 && (cleantextarray[j]!=originaltext[i+1]))
- {
- if(endtextarray[i+1]!=cleantextarray[j])
- {
- endtextarray[i] = cleantextarray[j];
- }
- else
- {
- break;
- }
- }
- else if(i==(n-1) && (cleantextarray[j]!=originaltext[i-1]))
- {
- if(endtextarray[i-1]!=cleantextarray[j])
- {
- endtextarray[i] = cleantextarray[j];
- }
- else
- {
- break;
- }
- }
- }
- }
- else
- {
- if(i!=0 && i<n)
- {
- if(endtextarray[i-1]!=originaltext[i])
- {
- endtextarray[i]=originaltext[i];
- }
- }
- else if(i==0)
- {
- if(endtextarray[i+1]!=originaltext[i])
- {
- endtextarray[i]=originaltext[i];
- }
- }
- else if(i==n-1)
- {
- if(endtextarray[i-1]!=originaltext[i])
- {
- endtextarray[i]=originaltext[i];
- }
- }
- }
- }
- string endtext = new String(endtextarray.ToArray());
- Console.WriteLine(endtext);
- }
- }
- public class Test
- {
- public static void Main()
- {
- Console.WriteLine("Please Enter text:");
- var text = Console.ReadLine();
- RemoveQMark rqm = new RemoveQMark();
- string cleantext = rqm.getcleantext(text);
- GetNewString gns = new GetNewString();
- gns.getNewText(cleantext,text);
- }
- }
abcab??bac?
OUTPUT:
Please Enter text: abcabcabacb
Comments
Post a Comment