Skip to main content

BEGINNER's REMOVE QUESTION MARKS AND PRINT STRING

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:
  1. using System;
  2. using System.Linq;
  3. public class RemoveQMark
  4. {
  5. public string getcleantext(string text)
  6. {
  7. char[] textarray = text.ToCharArray();
  8. int n = text.Length;
  9. char[] cleantextarray = new char[n];
  10. int j=0;
  11. for(int i=0;i<n;i++)
  12. {
  13. if(textarray[i] != '?')
  14. {
  15. cleantextarray[j]= textarray[i];
  16. j++;
  17. }
  18. }
  19. string cleantext =new String(cleantextarray.Where(c => !char.IsWhiteSpace(c)).Distinct().ToArray());
  20. string clean =cleantext.Trim();
  21. return clean;
  22. }
  23. }
  24. public class GetNewString
  25. {
  26. public void getNewText(string cleantext,string text)
  27. {
  28. char[] cleantextarray = cleantext.ToCharArray();
  29. char[] originaltext = text.ToCharArray();
  30. int n = text.Length;
  31. int m=cleantext.Length;
  32. char[] endtextarray = new char[text.Length];
  33. for(int i=0;i<n;i++)
  34. {
  35. if(originaltext[i] == '?')
  36. {
  37. for(int j=0;j<m-1;j++)
  38. {
  39. if (i!=0 && i!=(n-1) && (cleantextarray[j]!=originaltext[i-1]) && (cleantextarray[j]!=originaltext[i+1]))
  40. {
  41. if(endtextarray[i-1]!=cleantextarray[j] && endtextarray[i+1]!=cleantextarray[j])
  42. {
  43. endtextarray[i] = cleantextarray[j];
  44. }
  45. else
  46. {
  47. break;
  48. }
  49. }
  50. else if(i==0 && (cleantextarray[j]!=originaltext[i+1]))
  51. {
  52. if(endtextarray[i+1]!=cleantextarray[j])
  53. {
  54. endtextarray[i] = cleantextarray[j];
  55. }
  56. else
  57. {
  58. break;
  59. }
  60. }
  61. else if(i==(n-1) && (cleantextarray[j]!=originaltext[i-1]))
  62. {
  63. if(endtextarray[i-1]!=cleantextarray[j])
  64. {
  65. endtextarray[i] = cleantextarray[j];
  66. }
  67. else
  68. {
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. else
  75. {
  76. if(i!=0 && i<n)
  77. {
  78. if(endtextarray[i-1]!=originaltext[i])
  79. {
  80. endtextarray[i]=originaltext[i];
  81. }
  82. }
  83. else if(i==0)
  84. {
  85. if(endtextarray[i+1]!=originaltext[i])
  86. {
  87. endtextarray[i]=originaltext[i];
  88. }
  89. }
  90. else if(i==n-1)
  91. {
  92. if(endtextarray[i-1]!=originaltext[i])
  93. {
  94. endtextarray[i]=originaltext[i];
  95. }
  96. }
  97. }
  98. }
  99. string endtext = new String(endtextarray.ToArray());
  100. Console.WriteLine(endtext);
  101. }
  102. }
  103. public class Test
  104. {
  105. public static void Main()
  106. {
  107. Console.WriteLine("Please Enter text:");
  108. var text = Console.ReadLine();
  109. RemoveQMark rqm = new RemoveQMark();
  110. string cleantext = rqm.getcleantext(text);
  111. GetNewString gns = new GetNewString();
  112. gns.getNewText(cleantext,text);
  113. }
  114. }
INPUT:
abcab??bac?
OUTPUT:
Please Enter text:
abcabcabacb

Comments

Popular posts from this blog

Print word pattern - odd letters - beginner's code

1. Print the word with odd letters as P M R A O R G O R R A P M Solution: using System; public class Test { public static void Main() { string text = Console.ReadLine(); int n=text.Length; Pattern print = new Pattern (); print.XPattern(text,n); } } public class Pattern { public void XPattern(string text,int n) { char[] textarray = text.ToCharArray(); int reverseprinter = n-1; int middleLetter = n/2; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j && j!=middleLetter) { Console.Write(textarray[j]); } else if(j==reverseprinter) { Console.Write(textarray[reverseprinter]); reverseprinter--; } else { Console.Write("*"); } } Console.WriteLine(); } } } OUTPUT Please Enter text: INPUT : program p m r a o r g o r r a p m

GRANDSON PROBLEM

https://www.geeksforgeeks.org/zoho-interview-set-1-campus/ Given a two dimensional array of string like <”luke”, “shaw”> <”wayne”, “rooney”> <”rooney”, “ronaldo”> <”shaw”, “rooney”> Where the first string is “child”, second string is “Father”. And given “ronaldo” we have to find his no of grandchildren Here “ronaldo” has 2 grandchildren. So our output should be 2. SOLUTION using System; public class Test { public static void Main() { var noOfInputs = Console.ReadLine(); int noOfInputLines = Convert.ToInt32(noOfInputs); string[,] FatherSonArray= new String[noOfInputLines,noOfInputLines]; for(int father=0;father<noOfInputLines;father++) { for(int son=0;son<2;son++) { FatherSonArray[father,son] = Console.ReadLine(); } }     Console.WriteLine("No of grandchildrens:");     string GrandFather = Console.ReadLine();     int NoOfGrandSons=0;     Console.WriteLine(GrandFather);     s

BEGINNER's ALPHABET PALINDROME

PRINT ALPHABET PALINDROMIC PYRAMID USING C# using System; public class Test {       public static void Main()       {             var number = Console.ReadLine();             int n = Convert.ToInt32(number);             Pattern print = new Pattern();             print.PrintPalindromePattern(n);       } } public class Pattern {       public void PrintPalindromePattern(int n)       {             int dummy = 1;             char letter = 'A';             int reversepalindrome;             char letteraftereachIteration ='A';             char reverseletter ='A';             for(int i=1;i<=n;i++)             {                     reversepalindrome = i-1;                   for(int j=1;j<=dummy;j++)                   {                         if(j<=i)                         {                         Console.Write(letter++);                         }                         else                         {