codeparrot/xlcost-text-to-code huggingface dataset:
Count of substrings which contains a given character K times | C # program to count the number of substrings which contains the character C exactly K times ; Function to count the number of substrings which contains the character C exactly K times ; left and right counters for characters on both sides of substring window ; left and right pointer on both sides of substring window ; initialize the frequency ; result and length of string ; initialize the left pointer ; initialize the right pointer ; traverse all the window substrings ; counting the characters on leftSide of substring window ; counting the characters on rightSide of substring window ; Add the possible substrings on both sides to result ; Setting the frequency for next substring window ; reset the left , right counters ; Driver code
using System ; class GFG { static int countSubString ( string s , char c , int k ) { int leftCount = 0 , rightCount = 0 ; int left = 0 , right = 0 ; int freq = 0 ; int result = 0 , len = s . Length ; while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } right = left + 1 ; while ( freq != ( k - 1 ) && ( right - 1 ) < len ) { if ( s [ right ] == c ) freq ++ ; right ++ ; } while ( left < len && ( right - 1 ) < len ) { while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } while ( right < len && s [ right ] != c ) { if ( s [ right ] == c ) freq ++ ; right ++ ; rightCount ++ ; } result = result + ( leftCount + 1 ) * ( rightCount + 1 ) ; freq = k - 1 ; leftCount = 0 ; rightCount = 0 ; left ++ ; right ++ ; } return result ; } static public void Main ( ) { string s = "3123231" ; char c = '3' ; int k = 2 ; Console . WriteLine ( countSubString ( s , c , k ) ) ; } }
Count of sub | C # Implementation of above approach ; Function to return the total required sub - sequences ; Find ways for all values of x ; x + 1 ; Removing all unnecessary digits ; Prefix Sum Array for X + 1 digit ; Sum of squares ; Previous sum of all possible pairs ; To find sum of multiplication of all possible pairs ; To prevent overcounting ; Adding ways for all possible x ; Driver code
using System ; class GFG { static int solve ( string test , int MOD ) { int size = test . Length ; int total = 0 ; for ( int i = 0 ; i <= 8 ; i ++ ) { int x = i ; int y = i + 1 ; string newtest = " " ; for ( int j = 0 ; j < size ; j ++ ) { if ( test [ j ] == x + 48 test [ j ] == y + 48 ) { newtest += test [ j ] ; } } if ( newtest . Length > 0 ) { int size1 = newtest . Length ; int [ ] prefix = new int [ size1 ] ; for ( int j = 0 ; j < size1 ; j ++ ) { prefix [ j ] = 0 ; if ( newtest [ j ] == y + 48 ) { prefix [ j ] ++ ; } } for ( int j = 1 ; j < size1 ; j ++ ) { prefix [ j ] += prefix [ j - 1 ] ; } int count = 0 ; int firstcount = 0 ; int ss = 0 ; int prev = 0 ; for ( int j = 0 ; j < size1 ; j ++ ) { if ( newtest [ j ] == x + 48 ) { count ++ ; firstcount ++ ; } else { ss += count * count ; int pairsum = ( firstcount * firstcount - ss ) / 2 ; int temp = pairsum ; pairsum -= prev ; prev = temp ; int secondway = prefix [ size1 - 1 ] ; if ( j != 0 ) secondway -= prefix [ j - 1 ] ; int answer = count * ( count - 1 ) * secondway * ( secondway - 1 ) ; answer /= 4 ; answer += ( pairsum * secondway * ( secondway - 1 ) ) / 2 ; total += answer ; count = 0 ; } } } } return total ; } public static void Main ( ) { string test = "13134422" ; int MOD = 1000000007 ; Console . WriteLine ( solve ( test , MOD ) ) ; } }
Find if it is possible to make a binary string which contanins given number of "0" , "1" , "01" and "10" as sub sequences | C # implementation of the approach ; Function that returns true if it is possible to make a binary string consisting of l 0 ' s , ▁ m ▁ 1' s , x "01" sub - sequences and y "10" sub - sequences ; Driver code
using System ; class sol { static Boolean isPossible ( int l , int m , int x , int y ) { if ( l * m == x + y ) return true ; return false ; } public static void Main ( String [ ] args ) { int l = 3 , m = 2 , x = 4 , y = 2 ; if ( isPossible ( l , m , x , y ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count pairs of characters in a string whose ASCII value difference is K | C # implementation of the approach ; Function to return the count of required pairs of characters ; Length of the string ; To store the frequency of each character ; Update the frequency of each character ; To store the required count of pairs ; If ascii value difference is zero ; If there exists similar characters more than once ; If there exits characters with ASCII value difference as k ; Return the required count ; Driver code
using System ; class GFG { static int MAX = 26 ; static int countPairs ( char [ ] str , int k ) { int n = str . Length ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } int cnt = 0 ; if ( k == 0 ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 1 ) { cnt += ( ( freq [ i ] * ( freq [ i ] - 1 ) ) / 2 ) ; } } } else { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 0 && i + k < MAX && freq [ i + k ] > 0 ) { cnt += ( freq [ i ] * freq [ i + k ] ) ; } } ; } return cnt ; } public static void Main ( String [ ] args ) { String str = " abcdab " ; int k = 0 ; Console . WriteLine ( countPairs ( str . ToCharArray ( ) , k ) ) ; } }
Count of three non | C # implementation of the approach ; Function that returns true if s [ i ... j ] + s [ k ... l ] + s [ p ... q ] is a palindrome ; Function to return the count of valid sub - strings ; To store the count of required sub - strings ; For choosing the first sub - string ; For choosing the second sub - string ; For choosing the third sub - string ; Check if the concatenation is a palindrome ; Driver code
using System ; class GFG { static bool isPalin ( int i , int j , int k , int l , int p , int q , String s ) { int start = i , end = q ; while ( start < end ) { if ( s [ start ] != s [ end ] ) { return false ; } start ++ ; if ( start == j + 1 ) { start = k ; } end -- ; if ( end == p - 1 ) { end = l ; } } return true ; } static int countSubStr ( String s ) { int count = 0 ; int n = s . Length ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i ; j < n - 2 ; j ++ ) { for ( int k = j + 1 ; k < n - 1 ; k ++ ) { for ( int l = k ; l < n - 1 ; l ++ ) { for ( int p = l + 1 ; p < n ; p ++ ) { for ( int q = p ; q < n ; q ++ ) { if ( isPalin ( i , j , k , l , p , q , s ) ) { count ++ ; } } } } } } } return count ; } public static void Main ( String [ ] args ) { String s = " abca " ; Console . WriteLine ( countSubStr ( s ) ) ; } }
Find the sum of the ascii values of characters which are present at prime positions | C # implementation of the approach ; Function that returns true if n is prime ; Function to return the sum of the ascii values of the characters which are present at prime positions ; To store the sum ; For every character ; If current position is prime then add the ASCII value of the character at the current position ; Driver code
using System ; class GFG { static bool isPrime ( int n ) { if ( n == 0 n == 1 ) { return false ; } for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static int sumAscii ( string str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( i + 1 ) ) { sum += ( int ) ( str [ i ] ) ; } } return sum ; } public static void Main ( ) { string str = " geeksforgeeks " ; int n = str . Length ; Console . WriteLine ( sumAscii ( str , n ) ) ; } }
Count the nodes of the tree which make a pangram when concatenated with the sub | C # implementation of the above approach ; Function that returns if the String x is a pangram ; Function to return the count of nodes which make pangram with the sub - tree nodes ; Function to perform dfs and update the nodes such that weight [ i ] will store the weight [ i ] concatenated with the weights of all the nodes in the sub - tree ; Driver code ; Weights of the nodes ; Edges of the tree
using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] graph = new List < int > [ 100 ] ; static String [ ] weight = new String [ 100 ] ; static bool Pangram ( String x ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; int n = x . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( x [ i ] ) ) { mp [ x [ i ] ] = mp [ x [ i ] ] + 1 ; } else { mp . Add ( x [ i ] , 1 ) ; } } if ( mp . Count == 26 ) return true ; else return false ; } static int countTotalPangram ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( Pangram ( weight [ i ] ) ) cnt ++ ; return cnt ; } static void dfs ( int node , int parent ) { foreach ( int to in graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; weight [ node ] += weight [ to ] ; } } public static void Main ( String [ ] args ) { int n = 6 ; weight [ 1 ] = " abcde " ; weight [ 2 ] = " fghijkl " ; weight [ 3 ] = " abcdefg " ; weight [ 4 ] = " mnopqr " ; weight [ 5 ] = " stuvwxy " ; weight [ 6 ] = " zabcdef " ; for ( int i = 0 ; i < graph . Length ; i ++ ) graph [ i ] = new List < int > ( ) ; graph [ 1 ] . Add ( 2 ) ; graph [ 2 ] . Add ( 3 ) ; graph [ 2 ] . Add ( 4 ) ; graph [ 1 ] . Add ( 5 ) ; graph [ 5 ] . Add ( 6 ) ; dfs ( 1 , 1 ) ; Console . Write ( countTotalPangram ( n ) ) ; } }
Count the nodes of a tree whose weighted string does not contain any duplicate characters | C # implementation of the approach ; Function that returns true if the String contains unique characters ; Function to perform dfs ; If weighted String of the current node contains unique characters ; Driver code ; Weights of the nodes ; Edges of the tree
using System ; using System . Collections . Generic ; class GFG { static int cnt = 0 ; static List < int > [ ] graph = new List < int > [ 100 ] ; static String [ ] weight = new String [ 100 ] ; static bool uniqueChars ( char [ ] arr ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } if ( mp . Count == arr . Length ) return true ; else return false ; } static void dfs ( int node , int parent ) { if ( uniqueChars ( weight [ node ] . ToCharArray ( ) ) ) cnt += 1 ; foreach ( int to in graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; } } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) graph [ i ] = new List < int > ( ) ; weight [ 1 ] = " abc " ; weight [ 2 ] = " aba " ; weight [ 3 ] = " bcb " ; weight [ 4 ] = " moh " ; weight [ 5 ] = " aa " ; graph [ 1 ] . Add ( 2 ) ; graph [ 2 ] . Add ( 3 ) ; graph [ 2 ] . Add ( 4 ) ; graph [ 1 ] . Add ( 5 ) ; dfs ( 1 , 1 ) ; Console . Write ( cnt ) ; } }
Check if a string contains two non overlapping sub | C # implementation of the approach ; Function that returns true if s contains two non overlapping sub Strings " geek " and " keeg " ; If " geek " and " keeg " are both present in s without over - lapping and " keeg " starts after " geek " ends ; Driver code
using System ; class GFG { static bool isValid ( string s ) { if ( ( s . IndexOf ( " geek " ) != - 1 ) && ( s . IndexOf ( " keeg " , s . IndexOf ( " geek " ) + 4 ) != - 1 ) ) return true ; return false ; } public static void Main ( ) { string s = " geekeekeeg " ; if ( isValid ( s ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find the last non repeating character in string | C # implementation of the approach ; Maximum distinct characters possible ; Function to return the last non - repeating character ; To store the frequency of each of the character of the given string ; Update the frequencies ; Starting from the last character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the string are repeating ; Driver code
using System ; class GFG { static readonly int MAX = 256 ; static String lastNonRepeating ( String str , int n ) { int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] ] ++ ; for ( int i = n - 1 ; i >= 0 ; i -- ) { char ch = str [ i ] ; if ( freq [ ch ] == 1 ) return ( " " + ch ) ; } return " - 1" ; } public static void Main ( String [ ] args ) { String str = " GeeksForGeeks " ; int n = str . Length ; Console . WriteLine ( lastNonRepeating ( str , n ) ) ; } }
Minimum number of operations on a binary string such that it gives 10 ^ A as remainder when divided by 10 ^ B | C # implementation of the approach ; Function to return the minimum number of operations on a binary string such that it gives 10 ^ A as remainder when divided by 10 ^ B ; Initialize result ; Loop through last b digits ; Driver code
using System ; class GFG { static int findCount ( string s , int n , int a , int b ) { int res = 0 ; for ( int i = 0 ; i < b ; i ++ ) { if ( i == a ) { if ( s [ n - i - 1 ] != '1' ) res += 1 ; } else { if ( s [ n - i - 1 ] != '0' ) res += 1 ; } } return res ; } static public void Main ( ) { string str = "1001011001" ; int N = str . Length ; int A = 3 , B = 6 ; Console . WriteLine ( findCount ( str , N , A , B ) ) ; } }
Find letter 's position in Alphabet using Bit operation | C # implementation of the approach ; Function to calculate the position of characters ; Performing AND operation with number 31 ; Driver code
using System ; class GFG { public static int NUM = 31 ; static void positions ( string str , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ( str [ i ] & NUM ) + " ▁ " ) ; } } public static void Main ( ) { string str = " Geeks " ; int n = str . Length ; positions ( str , n ) ; } }
Generate all permutations of a string that follow given constraints | Simple C # program to print all permutations of a string that follow given constraint ; Check if current permutation is valid ; Recursively generate all permutation ; Driver code
using System ; using System . Text ; class GFG { static void permute ( StringBuilder str , int l , int r ) { if ( l == r ) { if ( str . ToString ( ) . IndexOf ( " AB " ) == - 1 ) { Console . Write ( str . ToString ( ) + " ▁ " ) ; } return ; } for ( int i = l ; i <= r ; i ++ ) { char tmp = str [ l ] ; str [ l ] = str [ i ] ; str [ i ] = tmp ; permute ( str , l + 1 , r ) ; tmp = str [ l ] ; str [ l ] = str [ i ] ; str [ i ] = tmp ; } } static void Main ( string [ ] arg ) { string str = " ABC " ; StringBuilder s = new StringBuilder ( str ) ; permute ( s , 0 , str . Length - 1 ) ; } }
Length of the longest substring that do not contain any palindrome | C # implementation of the above approach ; Function to find the length of the longest substring ; initializing the variables ; checking palindrome of size 2 example : aa ; checking palindrome of size 3 example : aba ; else incrementing length of substring ; max1 = Math . Max ( max1 , len + 1 ) ; finding maximum ; if there exits single character then it is always palindrome ; Driver Code
using System ; class GFG { public static int lenoflongestnonpalindrome ( String s ) { int max1 = 1 , len = 0 ; char [ ] new_str = s . ToCharArray ( ) ; for ( int i = 0 ; i < new_str . Length - 1 ; i ++ ) { if ( new_str [ i ] == new_str [ i + 1 ] ) len = 0 ; else if ( i > 0 && ( new_str [ i + 1 ] == new_str [ i - 1 ] ) ) len = 1 ; len ++ ; } if ( max1 == 1 ) return 0 ; else return max1 ; } public static void Main ( String [ ] args ) { String s = " synapse " ; Console . WriteLine ( lenoflongestnonpalindrome ( s ) ) ; } }
Make lexicographically smallest palindrome by substituting missing characters | C # implementation of the approach ; Function to return the lexicographically smallest palindrome that can be made from the given string after replacing the required characters ; If characters are missing at both the positions then substitute it with ' a ' ; If only str [ j ] = ' * ' then update it with the value at str [ i ] ; If only str [ i ] = ' * ' then update it with the value at str [ j ] ; If characters at both positions are not equal and != ' * ' then the string cannot be made palindrome ; Return the required palindrome ; Driver code
using System ; class GFG { static String makePalindrome ( char [ ] str ) { int i = 0 , j = str . Length - 1 ; while ( i <= j ) { if ( str [ i ] == ' * ' && str [ j ] == ' * ' ) { str [ i ] = ' a ' ; str [ j ] = ' a ' ; } else if ( str [ j ] == ' * ' ) str [ j ] = str [ i ] ; else if ( str [ i ] == ' * ' ) str [ i ] = str [ j ] ; else if ( str [ i ] != str [ j ] ) return " - 1" ; i ++ ; j -- ; } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { char [ ] str = " na * an " . ToCharArray ( ) ; Console . WriteLine ( makePalindrome ( str ) ) ; } }
Calculate score for the given binary string | C # implementation of the approach ; Function to return the score for the given binary string ; Traverse through string character ; Initialize current chunk 's size ; Get current character ; Calculate total chunk size of same characters ; Add / subtract pow ( chunkSize , 2 ) depending upon character ; Return the score ; Driver code
using System ; class GFG { public static int calcScore ( String str ) { int score = 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; ) { int chunkSize = 1 ; char currentChar = str [ i ++ ] ; while ( i < len && str [ i ] == currentChar ) { chunkSize ++ ; i ++ ; } if ( currentChar == '1' ) score += ( int ) Math . Pow ( chunkSize , 2 ) ; else score -= ( int ) Math . Pow ( chunkSize , 2 ) ; } return score ; } public static void Main ( String [ ] args ) { String str = "11011" ; Console . WriteLine ( calcScore ( str ) ) ; } }
Number of sub | C # implementation of the approach ; Function to return the count of required sub - strings ; Left and right counters for characters on both sides of sub - string window ; Left and right pointer on both sides of sub - string window ; Initialize the frequency ; Result and length of string ; Initialize the left pointer ; Initialize the right pointer ; Traverse all the window sub - strings ; Counting the characters on left side of the sub - string window ; Counting the characters on right side of the sub - string window ; Add the possible sub - strings on both sides to result ; Setting the frequency for next sub - string window ; Reset the left and right counters ; Driver code
using System ; class GFG { static int countSubString ( char [ ] s , char c , int k ) { int leftCount = 0 , rightCount = 0 ; int left = 0 , right = 0 ; int freq = 0 ; int result = 0 , len = s . Length ; while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } right = left + 1 ; while ( freq != ( k - 1 ) && ( right - 1 ) < len ) { if ( s [ right ] == c ) freq ++ ; right ++ ; } while ( left < len && ( right - 1 ) < len ) { while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } while ( right < len && s [ right ] != c ) { if ( s [ right ] == c ) freq ++ ; right ++ ; rightCount ++ ; } result = result + ( leftCount + 1 ) * ( rightCount + 1 ) ; freq = k - 1 ; leftCount = 0 ; rightCount = 0 ; left ++ ; right ++ ; } return result ; } public static void Main ( String [ ] args ) { String s = " abada " ; char c = ' a ' ; int k = 2 ; Console . WriteLine ( countSubString ( s . ToCharArray ( ) , c , k ) ) ; } }
Maximum length palindrome that can be created with characters in range L and R | C # implementation of the approach ; Function to return the length of the longest palindrome that can be formed using the characters in the range [ l , r ] ; 0 - based indexing ; Marks if there is an odd frequency character ; Length of the longest palindrome possible from characters in range ; Traverse for all characters and count their frequencies ; Find the frequency in range 1 - r ; Exclude the frequencies in range 1 - ( l - 1 ) ; If frequency is odd , then add 1 less than the original frequency to make it even ; Else completely add if even ; If any odd frequency character is present then add 1 ; Function to pre - calculate the frequencies of the characters to reduce complexity ; Iterate and increase the count ; Create a prefix type array ; Driver code ; Pre - calculate prefix array ; Perform queries
using System ; class GFG { static int N = 4 ; static int performQueries ( int l , int r , int [ , ] prefix ) { l -- ; r -- ; bool flag = false ; int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int cnt = prefix [ r , i ] ; if ( l > 0 ) cnt -= prefix [ l - 1 , i ] ; if ( cnt % 2 == 1 ) { flag = true ; count += cnt - 1 ; } else count += cnt ; } if ( flag ) count += 1 ; return count ; } static void preCalculate ( string s , int [ , ] prefix ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { prefix [ i , s [ i ] - ' a ' ] ++ ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) prefix [ i , j ] += prefix [ i - 1 , j ] ; } } public static void Main ( ) { string s = " amim " ; int [ , ] prefix = new int [ N , 26 ] ; preCalculate ( s , prefix ) ; int [ , ] queries = { { 1 , 4 } , { 3 , 4 } } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) { Console . WriteLine ( performQueries ( queries [ i , 0 ] , queries [ i , 1 ] , prefix ) ) ; } } }
Check whether the given string is a valid identifier | C # implementation of the approach ; Function that returns true if str is a valid identifier ; If first character is invalid ; Traverse the string for the rest of the characters ; String is a valid identifier ; Driver code
using System ; class GFG { static bool isValid ( String str , int n ) { if ( ! ( ( str [ 0 ] >= ' a ' && str [ 0 ] <= ' z ' ) || ( str [ 0 ] >= ' A ' && str [ 0 ] <= ' Z ' ) str [ 0 ] == ' _ ' ) ) return false ; for ( int i = 1 ; i < str . Length ; i ++ ) { if ( ! ( ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) || ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= '0' && str [ i ] <= '9' ) str [ i ] == ' _ ' ) ) return false ; } return true ; } public static void Main ( String [ ] args ) { String str = " _ geeks123" ; int n = str . Length ; if ( isValid ( str , n ) ) Console . WriteLine ( " Valid " ) ; else Console . WriteLine ( " Invalid " ) ; } }
Check if all the 1 's in a binary string are equidistant or not | C # implementation of the approach ; Function that returns true if all the 1 's in the binary string s are equidistant ; Initialize vector to store the position of 1 's ; Store the positions of 1 's ; Size of the position vector ; If condition isn 't satisfied ; Drivers code
using System ; using System . Collections . Generic ; class GFG { static bool check ( String s , int l ) { List < int > pos = new List < int > ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( s [ i ] == '1' ) { pos . Add ( i ) ; } } int t = pos . Count ; for ( int i = 1 ; i < t ; i ++ ) { if ( ( pos [ i ] - pos [ i - 1 ] ) != ( pos [ 1 ] - pos [ 0 ] ) ) return false ; } return true ; } public static void Main ( String [ ] args ) { String s = "100010001000" ; int l = s . Length ; if ( check ( s , l ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Capitalize the first and last character of each word in a string | C # program to remove the first and last character of each word in a string . ; Create an equivalent char array of given string ; k stores index of first character and i is going to store index of last character . ; Check if the character is a small letter If yes , then Capitalise ; Driver code
using System ; class GFG { static String FirstAndLast ( String str ) { char [ ] ch = str . ToCharArray ( ) ; for ( int i = 0 ; i < ch . Length ; i ++ ) { int k = i ; while ( i < ch . Length && ch [ i ] != ' ▁ ' ) i ++ ; ch [ k ] = ( char ) ( ch [ k ] >= ' a ' && ch [ k ] <= ' z ' ? ( ( int ) ch [ k ] - 32 ) : ( int ) ch [ k ] ) ; ch [ i - 1 ] = ( char ) ( ch [ i - 1 ] >= ' a ' && ch [ i - 1 ] <= ' z ' ? ( ( int ) ch [ i - 1 ] - 32 ) : ( int ) ch [ i - 1 ] ) ; } return new String ( ch ) ; } public static void Main ( String [ ] args ) { String str = " Geeks ▁ for ▁ Geeks " ; Console . WriteLine ( str ) ; Console . WriteLine ( FirstAndLast ( str ) ) ; } }