codeparrot/xlcost-text-to-code huggingface dataset:
Number of substrings with odd decimal value in a binary string | C # program to count substrings with odd decimal value ; Function to count number of substrings with odd decimal representation ; auxiliary array to store count of 1 's before ith index ; store count of 1 's before  i-th index ; variable to store answer ; Traverse the string reversely to calculate number of odd substrings before i - th index ; Driver Code
using System ; class GFG { static int countSubstr ( string s ) { int n = s . Length ; int [ ] auxArr = new int [ n ] ; if ( s [ 0 ] == '1' ) auxArr [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) auxArr [ i ] = auxArr [ i - 1 ] + 1 ; else auxArr [ i ] = auxArr [ i - 1 ] ; } int count = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) if ( s [ i ] == '1' ) count += auxArr [ i ] ; return count ; } public static void Main ( ) { string s = "1101" ; Console . WriteLine ( countSubstr ( s ) ) ; } }
Check whether second string can be formed from characters of first string | C # program to check whether second string can be formed from first string ; Create a count array and count frequencies characters in str1 . ; Now traverse through str2 to check if every character has enough counts ; Driver Code
using System ; class GFG { static int MAX = 256 ; static bool canMakeStr2 ( string str1 , string str2 ) { int [ ] count = new int [ MAX ] ; for ( int i = 0 ; i < str1 . Length ; i ++ ) count [ str1 [ i ] ] ++ ; for ( int i = 0 ; i < str2 . Length ; i ++ ) { if ( count [ str2 [ i ] ] == 0 ) return false ; count [ str2 [ i ] ] -- ; } return true ; } static public void Main ( ) { string str1 = " geekforgeeks " ; string str2 = " for " ; if ( canMakeStr2 ( str1 , str2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Position of robot after given movements | C # implementation to find final position of robot after the complete movement ; function to find final position of robot after the complete movement ; traverse the instruction string ' move ' ; for each movement increment its respective counter ; required final position of robot ; Driver code
using System ; class GFG { static void finalPosition ( String move ) { int l = move . Length ; int countUp = 0 , countDown = 0 ; int countLeft = 0 , countRight = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( move [ i ] == ' U ' ) countUp ++ ; else if ( move [ i ] == ' D ' ) countDown ++ ; else if ( move [ i ] == ' L ' ) countLeft ++ ; else if ( move [ i ] == ' R ' ) countRight ++ ; } Console . WriteLine ( " Final ▁ Position : ▁ ( " + ( countRight - countLeft ) + " , ▁ " + ( countUp - countDown ) + " ) " ) ; } public static void Main ( ) { String move = " UDDLLRUUUDUURUDDUULLDRRRR " ; finalPosition ( move ) ; } }
Length of longest balanced parentheses prefix | C # Program to find length of longest balanced parentheses prefix . ; Return the length of longest balanced parentheses prefix . ; Traversing the string . ; If open bracket add 1 to sum . ; If closed bracket subtract 1 from sum ; if first bracket is closing bracket then this condition would help ; If sum is 0 , store the index value . ; Driven Program
using System ; class GFG { static int maxbalancedprefix ( string str , int n ) { int sum = 0 ; int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) sum += 1 ; else sum -= 1 ; if ( sum < 0 ) break ; if ( sum == 0 ) maxi = i + 1 ; } return maxi ; } public static void Main ( ) { string str = " ( ( ( ) ( ) ) ( ) ) ( ( " ; int n = str . Length ; Console . WriteLine ( maxbalancedprefix ( str , n ) ) ; } }
Minimum cost to convert string into palindrome | C # program to find minimum cost to make a palindrome . ; Function to return cost ; length of string ; Iterate from both sides of string . If not equal , a cost will be there ; Driver code
using System ; class GFG { static int cost ( String str ) { int len = str . Length ; int res = 0 ; for ( int i = 0 , j = len - 1 ; i < j ; i ++ , j -- ) if ( str [ i ] != str [ j ] ) res += Math . Min ( str [ i ] , str [ j ] ) - ' a ' + 1 ; return res ; } public static void Main ( ) { string str = " abcdef " ; Console . WriteLine ( cost ( str ) ) ; } }
Encoding a word into Pig Latin | C # program to encode a word to a Pig Latin . ; the index of the first vowel is stored . ; Pig Latin is possible only if vowels is present ; Take all characters after index ( including index ) . Append all characters which are before index . Finally append " ay " ; Driver code
using System ; class GFG { static bool isVowel ( char c ) { return ( c == ' A ' c == ' E ' c == ' I ' c == ' O ' c == ' U ' c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static string pigLatin ( string s ) { int len = s . Length ; int index = - 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( isVowel ( s [ i ] ) ) { index = i ; break ; } } if ( index == - 1 ) return " - 1" ; return s . Substring ( index ) + s . Substring ( 0 , index ) + " ay " ; } public static void Main ( ) { string str = pigLatin ( " graphic " ) ; if ( str == " - 1" ) Console . WriteLine ( " No ▁ vowels " + " found . ▁ Pig ▁ Latin " + " ▁ not ▁ possible " ) ; else Console . WriteLine ( str ) ; } }
Possibility of a word from a given set of characters | C # program to check if a query string is present is given set . ; Count occurrences of all characters in s . ; Check if number of occurrences of every character in q is less than or equal to that in s . ; driver program
using System ; public class GFG { static int MAX_CHAR = 256 ; static bool isPresent ( string s , string q ) { int [ ] freq = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < s . Length ; i ++ ) freq [ s [ i ] ] ++ ; for ( int i = 0 ; i < q . Length ; i ++ ) { freq [ q [ i ] ] -- ; if ( freq [ q [ i ] ] < 0 ) return false ; } return true ; } static public void Main ( ) { string s = " abctd " ; string q = " cat " ; if ( isPresent ( s , q ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Minimum reduce operations to convert a given string into a palindrome | C # program to count minimum reduce operations to make a palindrome ; Returns count of minimum character reduce operations to make palindrome . ; Compare every character of first half with the corresponding character of second half and add difference to result . ; Driver code
using System ; class GFG { static int countReduce ( string str ) { int n = str . Length ; int res = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) res += Math . Abs ( str [ i ] - str [ n - i - 1 ] ) ; return res ; } public static void Main ( ) { string str = " abcd " ; Console . WriteLine ( countReduce ( str ) ) ; } }
Minimal operations to make a number magical | C # program to make a number magical ; function to calculate the minimal changes ; maximum digits that can be changed ; nested loops to generate all 6 digit numbers ; counter to count the number of change required ; if first digit is equal ; if 2 nd digit is equal ; if 3 rd digit is equal ; if 4 th digit is equal ; if 5 th digit is equal ; if 6 th digit is equal ; checks if less then the previous calculate changes ; returns the answer ; Driver code ; number stored in string ; prints the minimum operations
using System ; class GFG { static int calculate ( string s ) { int ans = 6 ; for ( int i = 0 ; i < 10 ; ++ i ) { for ( int j = 0 ; j < 10 ; ++ j ) { for ( int k = 0 ; k < 10 ; ++ k ) { for ( int l = 0 ; l < 10 ; ++ l ) { for ( int m = 0 ; m < 10 ; ++ m ) { for ( int n = 0 ; n < 10 ; ++ n ) { if ( i + j + k == l + m + n ) { int c = 0 ; if ( i != s [ 0 ] - '0' ) c ++ ; if ( j != s [ 1 ] - '0' ) c ++ ; if ( k != s [ 2 ] - '0' ) c ++ ; if ( l != s [ 3 ] - '0' ) c ++ ; if ( m != s [ 4 ] - '0' ) c ++ ; if ( n != s [ 5 ] - '0' ) c ++ ; if ( c < ans ) ans = c ; } } } } } } } return ans ; } static public void Main ( ) { string s = "123456" ; Console . WriteLine ( calculate ( s ) ) ; } }
Check if a two character string can be made using given words | C # code to check if a two character string can be made using given strings ; Function to check if str can be made using given words ; If str itself is present ; Match first character of str with second of word and vice versa ; If both characters found . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool makeAndCheckString ( List < String > words , String str ) { int n = words . Count ; bool first = false , second = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( words [ i ] == str ) return true ; if ( str [ 0 ] == words [ i ] [ 1 ] ) first = true ; if ( str [ 1 ] == words [ i ] [ 0 ] ) second = true ; if ( first && second ) return true ; } return false ; } public static void Main ( String [ ] args ) { String str = " ya " ; String [ ] array = { " ah " , " oy " , " to " , " ha " } ; List < String > words = new List < String > ( array ) ; if ( makeAndCheckString ( words , str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Print N | C # program to print all N - bit binary ; Function to get the binary representation of the number N ; loop for each bit ; Generate numbers in the range of ( 2 ^ N ) - 1 to 2 ^ ( N - 1 ) inclusive ; longest prefix check ; If counts of 1 is greater than counts of zero ; Do sub - prefixes check ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static string getBinaryRep ( int N , int num_of_bits ) { string r = " " ; num_of_bits -- ; while ( num_of_bits >= 0 ) { if ( ( N & ( 1 << num_of_bits ) ) != 0 ) r += "1" ; else r += "0" ; num_of_bits -- ; } return r ; } static List < string > NBitBinary ( int N ) { List < string > r = new List < string > ( ) ; int first = 1 << ( N - 1 ) ; int last = first * 2 ; for ( int i = last - 1 ; i >= first ; -- i ) { int zero_cnt = 0 ; int one_cnt = 0 ; int t = i ; int num_of_bits = 0 ; while ( t > 0 ) { if ( ( t & 1 ) != 0 ) one_cnt ++ ; else zero_cnt ++ ; num_of_bits ++ ; t = t >> 1 ; } if ( one_cnt >= zero_cnt ) { bool all_prefix_match = true ; int msk = ( 1 << num_of_bits ) - 2 ; int prefix_shift = 1 ; while ( msk > 0 ) { int prefix = ( msk & i ) >> prefix_shift ; int prefix_one_cnt = 0 ; int prefix_zero_cnt = 0 ; while ( prefix > 0 ) { if ( ( prefix & 1 ) != 0 ) prefix_one_cnt ++ ; else prefix_zero_cnt ++ ; prefix = prefix >> 1 ; } if ( prefix_zero_cnt > prefix_one_cnt ) { all_prefix_match = false ; break ; } prefix_shift ++ ; msk = msk & ( msk << 1 ) ; } if ( all_prefix_match ) { r . Add ( getBinaryRep ( i , num_of_bits ) ) ; } } } return r ; } static public void Main ( ) { int n = 4 ; List < string > results = NBitBinary ( n ) ; for ( int i = 0 ; i < results . Count ; ++ i ) Console . Write ( results [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } }
Find winner of an election where votes are represented as candidate names | C # program to find winner in an election . ; We have four Candidates with name as ' John ' , ' Johnny ' , ' jamie ' , ' jackie ' . The votes in String array are as per the votes casted . Print the name of candidates received Max vote . ; Insert all votes in a hashmap ; Traverse through map to find the candidate with maximum votes . ; If there is a tie , pick lexicographically smaller . ; Driver code
using System ; using System . Collections . Generic ; public class ElectoralVotingBallot { public static void findWinner ( String [ ] votes ) { Dictionary < String , int > map = new Dictionary < String , int > ( ) ; foreach ( String str in votes ) { if ( map . ContainsKey ( str ) ) map [ str ] = map [ str ] + 1 ; else map . Add ( str , 1 ) ; } int maxValueInMap = 0 ; String winner = " " ; foreach ( KeyValuePair < String , int > entry in map ) { String key = entry . Key ; int val = entry . Value ; if ( val > maxValueInMap ) { maxValueInMap = val ; winner = key ; } else if ( val == maxValueInMap && winner . CompareTo ( key ) > 0 ) winner = key ; } Console . WriteLine ( winner ) ; } public static void Main ( String [ ] args ) { String [ ] votes = { " john " , " johnny " , " jackie " , " johnny " , " john " , " jackie " , " jamie " , " jamie " , " john " , " johnny " , " jamie " , " johnny " , " john " } ; findWinner ( votes ) ; } }
Luhn algorithm | C # program to implement Luhn algorithm ; Returns true if given card number is valid ; We add two digits to handle cases that make two digits after doubling ; Driver code
using System ; class GFG { static bool checkLuhn ( String cardNo ) { int nDigits = cardNo . Length ; int nSum = 0 ; bool isSecond = false ; for ( int i = nDigits - 1 ; i >= 0 ; i -- ) { int d = cardNo [ i ] - '0' ; if ( isSecond == true ) d = d * 2 ; nSum += d / 10 ; nSum += d % 10 ; isSecond = ! isSecond ; } return ( nSum % 10 == 0 ) ; } static public void Main ( ) { String cardNo = "79927398713" ; if ( checkLuhn ( cardNo ) ) Console . WriteLine ( " This ▁ is ▁ a ▁ valid ▁ card " ) ; else Console . WriteLine ( " This ▁ is ▁ not ▁ a ▁ valid ▁ card " ) ; } }
Distributing all balls without repetition | C # program to find if its possible to distribute balls without repitiion ; function to find if its possible to distribute balls or not ; count array to count how many times each color has occurred ; increasing count of each color every time it appears ; to check if any color appears more than K times if it does we will print NO ; Driver code
using System ; public class GFG { static int MAX_CHAR = 26 ; static bool distributingBalls ( long k , long n , string str ) { int [ ] a = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ str [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( a [ i ] > k ) return false ; return true ; } static public void Main ( ) { long n = 6 , k = 3 ; string str = " aacaab " ; if ( distributingBalls ( k , n , str ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Find substrings that contain all vowels | C # program to find all substring that contain all vowels ; Returns true if x is vowel . ; Function to check whether a character is vowel or not ; To store vowels Outer loop picks starting character and inner loop picks ending character . ; If current character is not vowel , then no more result substrings possible starting from str [ i ] . ; If vowel , then we insert it in hash ; If all vowels are present in current substring ; Driver code
using System ; using System . Collections . Generic ; public class GFG { public static bool isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } public static void findSubstring ( string str ) { HashSet < char > hash = new HashSet < char > ( ) ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( isVowel ( str [ j ] ) == false ) { break ; } hash . Add ( str [ j ] ) ; if ( hash . Count == 5 ) { Console . Write ( str . Substring ( i , ( j + 1 ) - i ) + " ▁ " ) ; } } hash . Clear ( ) ; } } public static void Main ( string [ ] args ) { string str = " aeoibsddaeiouudb " ; findSubstring ( str ) ; } }
Find if an array contains a string with one mismatch | C # program to find if given string is present with one mismatch . ; If the array is empty ; If sizes are same ; If first mismatch ; Second mismatch ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static bool check ( List < String > list , String s ) { int n = ( int ) list . Count ; if ( n == 0 ) { return false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( list [ i ] . Length != s . Length ) { continue ; } bool diff = false ; for ( int j = 0 ; j < ( int ) list [ i ] . Length ; j ++ ) { if ( list [ i ] [ j ] != s [ j ] ) { if ( ! diff ) { diff = true ; } else { diff = false ; break ; } } } if ( diff ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { List < String > s = new List < String > ( ) ; s . Add ( " bana " ) ; s . Add ( " apple " ) ; s . Add ( " banacb " ) ; s . Add ( " bonanza " ) ; s . Add ( " banamf " ) ; Console . WriteLine ( check ( s , " banana " ) == true ? 1 : 0 ) ; } }
Sentence Palindrome ( Palindrome after removing spaces , dots , . . etc ) | C # program to find if a sentence is palindrome ; To check sentence is palindrome or not ; Lowercase string ; Compares character until they are equal ; If there is another symbol in left of sentence ; If there is another symbol in right of sentence ; If characters are equal ; If characters are not equal then sentence is not palindrome ; Returns true if sentence is palindrome ; Driver Code
using System ; public class GFG { static bool sentencePalindrome ( String str ) { int l = 0 ; int h = str . Length - 1 ; str = str . ToLower ( ) ; while ( l <= h ) { char getAtl = str [ l ] ; char getAth = str [ h ] ; if ( ! ( getAtl >= ' a ' && getAtl <= ' z ' ) ) l ++ ; else if ( ! ( getAth >= ' a ' && getAth <= ' z ' ) ) h -- ; else if ( getAtl == getAth ) { l ++ ; h -- ; } else return false ; } return true ; } public static void Main ( ) { String str = " Too ▁ hot ▁ to ▁ hoot . " ; if ( sentencePalindrome ( str ) ) Console . Write ( " Sentence ▁ is ▁ palindrome " ) ; else Console . Write ( " Sentence ▁ is ▁ not " + " ▁ " + " palindrome " ) ; } }
Ways to remove one element from a binary string so that XOR becomes zero | C # program to count number of ways to remove an element so that XOR of remaining string becomes 0. ; Returns number of ways in which XOR become ZERO by remove 1 element ; Counting number of 0 and 1 ; If count of ones is even then return count of zero else count of one ; Driver Code
using System ; class GFG { static int xorZero ( string s ) { int one_count = 0 , zero_count = 0 ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] == '1' ) one_count ++ ; else zero_count ++ ; if ( one_count % 2 == 0 ) return zero_count ; return one_count ; } public static void Main ( ) { string s = "11111" ; Console . WriteLine ( xorZero ( s ) ) ; } }
Keyword Cipher | C # program for encoding the string using classical cipher ; Function generates the encoded text ; This array represents the 26 letters of alphabets ; This loop inserts the keyword at the start of the encoded string ; To check whether the character is inserted earlier in the encoded string or not ; This loop inserts the remaining characters in the encoded string . ; Function that generates encodes ( cipher ) the message ; This loop ciphered the message . Spaces , special characters and numbers remain same . ; Driver code ; Hold the Keyword ; Function call to generate encoded text ; Message that need to encode ; Function call to print ciphered text
using System ; class GFG { static String encoder ( char [ ] key ) { String encoded = " " ; Boolean [ ] arr = new Boolean [ 26 ] ; for ( int i = 0 ; i < key . Length ; i ++ ) { if ( key [ i ] >= ' A ' && key [ i ] <= ' Z ' ) { if ( arr [ key [ i ] - 65 ] == false ) { encoded += ( char ) key [ i ] ; arr [ key [ i ] - 65 ] = true ; } } else if ( key [ i ] >= ' a ' && key [ i ] <= ' z ' ) { if ( arr [ key [ i ] - 97 ] == false ) { encoded += ( char ) ( key [ i ] - 32 ) ; arr [ key [ i ] - 97 ] = true ; } } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == false ) { arr [ i ] = true ; encoded += ( char ) ( i + 65 ) ; } } return encoded ; } static String cipheredIt ( String msg , String encoded ) { String cipher = " " ; for ( int i = 0 ; i < msg . Length ; i ++ ) { if ( msg [ i ] >= ' a ' && msg [ i ] <= ' z ' ) { int pos = msg [ i ] - 97 ; cipher += encoded [ pos ] ; } else if ( msg [ i ] >= ' A ' && msg [ i ] <= ' Z ' ) { int pos = msg [ i ] - 65 ; cipher += encoded [ pos ] ; } else { cipher += msg [ i ] ; } } return cipher ; } public static void Main ( String [ ] args ) { String key ; key = " Computer " ; Console . WriteLine ( " Keyword ▁ : ▁ " + key ) ; String encoded = encoder ( key . ToCharArray ( ) ) ; String message = " GeeksforGeeks " ; Console . WriteLine ( " Message ▁ before ▁ Ciphering ▁ : ▁ " + message ) ; Console . WriteLine ( " Ciphered ▁ Text ▁ : ▁ " + cipheredIt ( message , encoded ) ) ; } }
Check if both halves of the string have same set of characters | C # program to check if it two half of string contain same Character set or not ; function to check both halves for equality ; Counter array initialized with 0 ; Length of the string ; traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; Driver program to test above function ; String to be checked
using System ; public class GFG { static int MAX_CHAR = 26 ; static bool checkCorrectOrNot ( String s ) { int [ ] count = new int [ MAX_CHAR ] ; int n = s . Length ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s [ i ] - ' a ' ] ++ ; count [ s [ j ] - ' a ' ] -- ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } public static void Main ( String [ ] args ) { String s = " abab " ; if ( checkCorrectOrNot ( s ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if a string is Isogram or not | C # code to check string is isogram or not ; function to check isogram ; loop to store count of chars and check if it is greater than 1 ; if count > 1 , return false ; Driver code ; checking str as isogram ; checking str2 as isogram
using System ; public class GFG { static bool check_isogram ( String str ) { int length = str . Length ; int [ ] mapHash = new int [ 26 ] ; for ( int i = 0 ; i < length ; i ++ ) { mapHash [ str [ i ] - ' a ' ] ++ ; if ( mapHash [ str [ i ] - ' a ' ] > 1 ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { String str = " geeks " ; String str2 = " computer " ; if ( check_isogram ( str ) ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; if ( check_isogram ( str2 ) ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }
Check if a binary string has a 0 between 1 s or not | Set 1 ( General approach ) | C # program to check if a string is valid or not . ; Method returns 1 when string is valid else returns 0 ; Find first occurrence of 1 in s [ ] ; Find last occurrence of 1 in s [ ] ; Check if there is any 0 in range ; Driver method
using System ; class GFG { static bool checkString ( String s ) { int len = s . Length ; int first = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( s [ i ] == '1' ) { first = i ; break ; } } int last = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { last = i ; break ; } } for ( int i = first ; i <= last ; i ++ ) if ( s [ i ] == '0' ) return false ; return true ; } public static void Main ( ) { string s = "00011111111100000" ; Console . WriteLine ( checkString ( s ) ? " VALID " : " NOT ▁ VALID " ) ; } }
Program to print all substrings of a given string | C # program to print all possible subStrings of a given String without checking for duplication . ; Function to print all ( n * ( n + 1 ) ) / 2 subStrings of a given String s of length n . ; Fix start index in outer loop . Reveal new character in inner loop till end of String . Print till - now - formed String . ; Driver code
using System ; class GFG { public static void printAllSubStrings ( String s , int n ) { for ( int i = 0 ; i < n ; i ++ ) { char [ ] temp = new char [ n - i + 1 ] ; int tempindex = 0 ; for ( int j = i ; j < n ; j ++ ) { temp [ tempindex ++ ] = s [ j ] ; temp [ tempindex ] = ' \0' ; Console . WriteLine ( temp ) ; } } } public static void Main ( ) { String s = " Geeky " ; printAllSubStrings ( s , s . Length ) ; } }
Reverse a string preserving space positions | C # program to implement the above approach ; Initialize two pointers as two corners ; Move both pointers toward each other ; If character at start or end is space , ignore it ; If both are not spaces , do swap ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void preserveSpace ( string str ) { int n = str . Length ; int start = 0 ; int end = n - 1 ; char [ ] Str = str . ToCharArray ( ) ; while ( start < end ) { if ( Str [ start ] == ' ▁ ' ) { start ++ ; continue ; } else if ( Str [ end ] == ' ▁ ' ) { end -- ; continue ; } else { char temp = Str [ start ] ; Str [ start ] = Str [ end ] ; Str [ end ] = temp ; start ++ ; end -- ; } } Console . Write ( new string ( Str ) ) ; } static void Main ( ) { string str = " internship ▁ at ▁ geeks ▁ for ▁ geeks " ; preserveSpace ( str ) ; } }