Remove the forbidden strings | C # program to remove forbidden strings ; number of forbidden strings ; original string ; forbidden strings ; to store original string as character array ; letter to replace and occur max number of times ; pre [ ] keeps record of the characters of w that need to be changed ; Function to check if the particular substring is present in w at position ; If length of substring from this position is greater than length of w then return ; n and n1 are used to check for substring without considering the case of the letters in w by comparing the difference of ASCII values ; If same == true then it means a substring was found starting at position therefore all characters from position to length of substring found need to be changed therefore they need to be marked ; Function performing calculations . ; To verify if any substring is starting from index i ; Modifying the string w according to th rules ; This condition checks if w [ i ] = upper ( letter ) ; This condition checks if w [ i ] is any lowercase letter apart from letter . If true replace it with letter ; This condition checks if w [ i ] is any uppercase letter apart from letter . If true then replace it with upper ( letter ) . ; Driver Code
using System ; class GFG { public static int n ; public static string z ; public static string [ ] s = new string [ 100 ] ; public static char [ ] w ; public static char letter ; public static bool [ ] pre = new bool [ 100 ] ; public static void verify ( int position , int index ) { int l = z . Length ; int k = s [ index ] . Length ; if ( position + k > l ) { return ; } bool same = true ; for ( int i = position ; i < position + k ; i ++ ) { int n , n1 ; char ch = w [ i ] ; char ch1 = s [ index ] [ i - position ] ; if ( ch >= ' a ' && ch <= ' z ' ) { n = ch - ' a ' ; } else { n = ch - ' A ' ; } if ( ch1 >= ' a ' && ch1 <= ' z ' ) { n1 = ch1 - ' a ' ; } else { n1 = ch1 - ' A ' ; } if ( n != n1 ) { same = false ; } } if ( same == true ) { for ( int i = position ; i < position + k ; i ++ ) { pre [ i ] = true ; } return ; } } public static void solve ( ) { w = z . ToCharArray ( ) ; letter = ' d ' ; int l = z . Length ; int p = letter - ' a ' ; for ( int i = 0 ; i < 100 ; i ++ ) { pre [ i ] = false ; } for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { verify ( i , j ) ; } } for ( int i = 0 ; i < l ; i ++ ) { if ( pre [ i ] == true ) { if ( w [ i ] == letter ) { w [ i ] = ( letter == ' a ' ) ? ' b ' : ' a ' ; } else if ( w [ i ] == ( char ) ( ( int ) ' A ' + p ) ) { w [ i ] = ( letter == ' a ' ) ? ' B ' : ' A ' ; } else if ( w [ i ] >= ' a ' && w [ i ] <= ' z ' ) { w [ i ] = letter ; } else if ( w [ i ] >= ' A ' && w [ i ] <= ' Z ' ) { w [ i ] = ( char ) ( ( int ) ' A ' + p ) ; } } } Console . WriteLine ( w ) ; } public static void Main ( string [ ] args ) { n = 3 ; s [ 0 ] = " etr " ; s [ 1 ] = " ed " ; s [ 2 ] = " ied " ; z = " PEtrUnited " ; solve ( ) ; } }