codeparrot/xlcost-text-to-code huggingface dataset:
Print distinct sorted permutations with duplicates allowed in input | C # program to print all permutations of a string in sorted order . ; Calculating factorial of a number ; Method to print the array ; Method to find total number of permutations ; Building Dictionary to store frequencies of all characters . ; Traversing hashmap and finding duplicate elements . ; Start traversing from the end and find position ' i - 1' of the first character which is greater than its successor . ; Finding smallest character after ' i - 1' and greater than temp [ i - 1 ] ; Swapping the above found characters . ; Sort all digits from position next to ' i - 1' to end of the string . ; Print the String ; Sorting String ; Print first permutation ; Finding the total permutations ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int factorial ( int n ) { int f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) f = f * i ; return f ; } static void print ( char [ ] temp ) { for ( int i = 0 ; i < temp . Length ; i ++ ) Console . Write ( temp [ i ] ) ; Console . WriteLine ( ) ; } static int calculateTotal ( char [ ] temp , int n ) { int f = factorial ( n ) ; Dictionary < char , int > hm = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < temp . Length ; i ++ ) { if ( hm . ContainsKey ( temp [ i ] ) ) hm [ temp [ i ] ] = hm [ temp [ i ] ] + 1 ; else hm . Add ( temp [ i ] , 1 ) ; } foreach ( KeyValuePair < char , int > e in hm ) { int x = e . Value ; if ( x > 1 ) { int temp5 = factorial ( x ) ; f = f / temp5 ; } } return f ; } static void nextPermutation ( char [ ] temp ) { int i ; for ( i = temp . Length - 1 ; i > 0 ; i -- ) if ( temp [ i ] > temp [ i - 1 ] ) break ; int min = i ; int j , x = temp [ i - 1 ] ; for ( j = i + 1 ; j < temp . Length ; j ++ ) if ( ( temp [ j ] < temp [ min ] ) && ( temp [ j ] > x ) ) min = j ; char temp_to_swap ; temp_to_swap = temp [ i - 1 ] ; temp [ i - 1 ] = temp [ min ] ; temp [ min ] = temp_to_swap ; Array . Sort ( temp , i , temp . Length - i ) ; print ( temp ) ; } static void printAllPermutations ( String s ) { char [ ] temp = s . ToCharArray ( ) ; Array . Sort ( temp ) ; print ( temp ) ; int total = calculateTotal ( temp , temp . Length ) ; for ( int i = 1 ; i < total ; i ++ ) nextPermutation ( temp ) ; } public static void Main ( String [ ] args ) { String s = " AAB " ; printAllPermutations ( s ) ; } }
Swap Nodes in Binary tree of every k 'th level | C # program swap nodes ; A Binary Tree Node ; function to create a new tree node ; A utility function swap left - node & right node of tree of every k 'th level  ; base case ; if current level + 1 is present in swap vector then we swap left & right node ; Recur for left and right subtrees ; This function mainly calls recursive function swapEveryKLevelUtil ( ) ; call swapEveryKLevelUtil function with initial level as 1. ; Utility method for inorder tree traversal ; Driver Code ; 1 / \ 2 3 / / \ 4 7 8
using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void swapEveryKLevelUtil ( Node root , int level , int k ) { if ( root == null || ( root . left == null && root . right == null ) ) return ; if ( ( level + 1 ) % k == 0 ) { Node temp = root . left ; root . left = root . right ; root . right = temp ; } swapEveryKLevelUtil ( root . left , level + 1 , k ) ; swapEveryKLevelUtil ( root . right , level + 1 , k ) ; } static void swapEveryKLevel ( Node root , int k ) { swapEveryKLevelUtil ( root , 1 , k ) ; } static void inorder ( Node root ) { if ( root == null ) return ; inorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; inorder ( root . right ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . right = newNode ( 8 ) ; root . right . left = newNode ( 7 ) ; int k = 2 ; Console . WriteLine ( " Before ▁ swap ▁ node ▁ : " ) ; inorder ( root ) ; swapEveryKLevel ( root , k ) ; Console . WriteLine ( " STRNEWLINE After ▁ swap ▁ Node ▁ : " ) ; inorder ( root ) ; } }
Convert a sentence into its equivalent mobile numeric keypad sequence | C # implementation to convert a sentence into its equivalent mobile numeric keypad sequence ; Function which computes the sequence ; length of input string ; Checking for space ; Calculating index for each character ; Output sequence ; Driver Function ; storing the sequence in array
using System ; class GFG { static String printSequence ( string [ ] arr , string input ) { string output = " " ; int n = input . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( input [ i ] == ' ▁ ' ) output = output + "0" ; else { int position = input [ i ] - ' A ' ; output = output + arr [ position ] ; } } return output ; } public static void Main ( ) { string [ ] str = { "2" , "22" , "222" , "3" , "33" , "333" , "4" , "44" , "444" , "5" , "55" , "555" , "6" , "66" , "666" , "7" , "77" , "777" , "7777" , "8" , "88" , "888" , "9" , "99" , "999" , "9999" } ; string input = " GEEKSFORGEEKS " ; Console . WriteLine ( printSequence ( str , input ) ) ; } }
Longest Uncommon Subsequence | C # program to find longest uncommon subsequence using naive method ; function to calculate length of longest uncommon subsequence ; creating an unordered map to map strings to their frequency ; traversing all elements of vector strArr ; Creating all possible subsequences , i . e 2 ^ n ; ( ( i >> j ) & 1 ) determines which character goes into string t ; If common subsequence is found , increment its frequency ; traversing the map ; if frequency equals 1 ; Driver code ; input strings
using System ; using System . Collections . Generic ; class GFG { static int findLUSlength ( string a , string b ) { Dictionary < string , int > map = new Dictionary < string , int > ( ) ; List < string > strArr = new List < string > ( ) ; strArr . Add ( a ) ; strArr . Add ( b ) ; foreach ( string s in strArr ) { for ( int i = 0 ; i < ( 1 << s . Length ) ; i ++ ) { string t = " " ; for ( int j = 0 ; j < s . Length ; j ++ ) { if ( ( ( i >> j ) & 1 ) != 0 ) t += s [ j ] ; } if ( map . ContainsKey ( t ) ) { int value = map [ t ] + 1 ; map . Remove ( t ) ; map . Add ( t , value ) ; } else map . Add ( t , 1 ) ; } } int res = 0 ; foreach ( KeyValuePair < string , int > entry in map ) { if ( entry . Value == 1 ) res = Math . Max ( res , entry . Key . Length ) ; } return res ; } static void Main ( ) { string a = " abcdabcd " , b = " abcabc " ; Console . Write ( findLUSlength ( a , b ) ) ; } }
Round the given number to nearest multiple of 10 | C # code for above approach ; Program to round the number to the nearest number having one 's digit 0 ; last character is 0 then return the original string ; if last character is 1 or 2 or 3 or 4 or 5 make it 0 ; process carry ; return final string ; Driver code ; Function Call
using System ; class GFG { static string round ( string s , int n ) { char [ ] c = s . ToCharArray ( ) ; if ( c [ n - 1 ] == '0' ) return s ; else if ( c [ n - 1 ] == '1' c [ n - 1 ] == '2' c [ n - 1 ] == '3' c [ n - 1 ] == '4' c [ n - 1 ] == '5' ) { c [ n - 1 ] = '0' ; return new string ( c ) ; } else { c [ n - 1 ] = '0' ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( c [ i ] == '9' ) c [ i ] = '0' ; else { int t = c [ i ] - '0' + 1 ; c [ i ] = ( char ) ( 48 + t ) ; break ; } } } string s1 = new string ( c ) ; if ( s1 [ 0 ] == '0' ) s1 = "1" + s1 ; return s1 ; } static void Main ( ) { string s = "5748965412485599999874589965999" ; int n = s . Length ; Console . WriteLine ( round ( s , n ) ) ; } }
Check whether given floating point number is even or odd | C # program to check whether given floating point number is even or odd ; Function to check even or odd . ; Loop to traverse number from LSB ; We ignore trailing 0 s after dot ; If it is ' . ' we will check next digit and it means decimal part is traversed . ; If digit is divisible by 2 means even number . ; Driver function
using System ; public class GfG { public static bool isEven ( string s1 ) { int l = s1 . Length ; bool dotSeen = false ; for ( int i = l - 1 ; i >= 0 ; i -- ) { if ( s1 [ i ] == '0' && dotSeen == false ) continue ; if ( s1 [ i ] == ' . ' ) { dotSeen = true ; continue ; } if ( ( s1 [ i ] - '0' ) % 2 == 0 ) return true ; return false ; } return false ; } public static void Main ( ) { string s1 = "100.70" ; if ( isEven ( s1 ) ) Console . WriteLine ( " Even " ) ; else Console . WriteLine ( " Odd " ) ; } }
Minimum cost to construct a string | C # Program to find minimum cost to construct a string ; Initially all characters are un - seen ; Marking seen characters ; Count total seen character , and that is the cost ; Driver code ; s is the string that needs to be constructed
using System ; class GFG { static int minCost ( char [ ] s ) { bool [ ] alphabets = new bool [ 26 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { alphabets [ ( int ) s [ i ] - 97 ] = true ; } int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( alphabets [ i ] ) { count ++ ; } } return count ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; Console . WriteLine ( " Total ▁ cost ▁ to ▁ construct ▁ " + s + " ▁ is ▁ " + minCost ( s . ToCharArray ( ) ) ) ; } }
Root to leaf paths having equal lengths in a Binary Tree | C # program to count root to leaf paths of different lengths . ; A binary tree node ; Utility that allocates a new node with the given data and null left and right pointers . ; Function to store counts of different root to leaf path lengths in hash map m . ; Base condition ; If leaf node reached , increment count of path length of this root to leaf path . ; Recursively call for left and right subtrees with path lengths more than 1. ; A wrapper over pathCountUtil ( ) ; Create an empty hash table ; Recursively check in left and right subtrees . ; Print all path lenghts and their counts . ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newnode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void pathCountUtil ( Node node , Dictionary < int , int > m , int path_len ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { if ( ! m . ContainsKey ( path_len ) ) m . Add ( path_len , 1 ) ; else m [ path_len ] = m [ path_len ] + 1 ; return ; } pathCountUtil ( node . right , m , path_len + 1 ) ; pathCountUtil ( node . left , m , path_len + 1 ) ; } static void pathCounts ( Node root ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; pathCountUtil ( root , m , 1 ) ; foreach ( KeyValuePair < int , int > entry in m ) { Console . WriteLine ( entry . Value + " ▁ paths ▁ have ▁ length ▁ " + entry . Key ) ; } } public static void Main ( String [ ] args ) { Node root = newnode ( 8 ) ; root . left = newnode ( 5 ) ; root . right = newnode ( 4 ) ; root . left . left = newnode ( 9 ) ; root . left . right = newnode ( 7 ) ; root . right . right = newnode ( 11 ) ; root . right . right . left = newnode ( 3 ) ; pathCounts ( root ) ; } }
Possibility of moving out of maze | C # Possibility of moving out of maze ; Function to check whether it will stay inside or come out ; marks all the positions that is visited ; Initial starting point ; initial assumption is it comes out ; runs till it is inside or comes out ; if the movement is towards left then we move left . The start variable and mark that position as visited if not visited previously . Else we break out ; It will be inside forever ; If the movement is towards right , then we move right . The start variable and mark that position as visited if not visited previously else we break out ; it will be inside forever ; Driver code
using System ; class GFG { static void checkingPossibility ( int [ ] a , int n , String s ) { int [ ] mark = new int [ a [ 0 ] * n ] ; int start = 0 ; int possible = 1 ; while ( start >= 0 && start < n ) { if ( s == " < " ) { if ( mark [ start ] == 0 ) { mark [ start ] = 1 ; start -= a [ start ] ; } else { possible = 0 ; break ; } } else { if ( mark [ start ] == 0 ) { mark [ start ] = 1 ; start += a [ start ] ; } else { possible = 0 ; break ; } } } if ( possible == 0 ) Console . Write ( " it ▁ stays ▁ " + " inside ▁ forever " ) ; else Console . Write ( " comes ▁ out " ) ; } public static void Main ( ) { int n = 2 ; String s = " > < " ; int [ ] a = { 1 , 2 } ; checkingPossibility ( a , n , s ) ; } }
a | C # program to remove consecutive duplicates from a given string . ; A iterative function that removes consecutive duplicates from string S ; We don 't need to do anything for  empty or single character string. ; j is used to store index is result string ( or index of current distinct character ) ; Traversing string ; If current character S [ i ] is different from S [ j ] ; Driver code
using System ; class GFG { static void removeDuplicates ( char [ ] S ) { int n = S . Length ; if ( n < 2 ) { return ; } int j = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S [ j ] != S [ i ] ) { j ++ ; S [ j ] = S [ i ] ; } } char [ ] A = new char [ j + 1 ] ; Array . Copy ( S , 0 , A , 0 , j + 1 ) ; Console . WriteLine ( A ) ; } public static void Main ( String [ ] args ) { char [ ] S1 = " geeksforgeeks " . ToCharArray ( ) ; removeDuplicates ( S1 ) ; char [ ] S2 = " aabcca " . ToCharArray ( ) ; removeDuplicates ( S2 ) ; } }
Simplify the directory path ( Unix like ) | C # implementation of optimized Approach 1 ; function to simplify a Unix - styled absolute path ; using vector in place of stack ; forming the current directory . ; if " . . " , we pop . ; do nothing ( added for better understanding . ) ; push the current directory into the vector . ; forming the ans ; vector is empty ; Driver Code ; absolute path which we have to simplify .
using System ; using System . Collections . Generic ; class GFG { static string simplify ( string path ) { List < string > v = new List < string > ( ) ; int n = path . Length ; string ans = " " ; for ( int i = 0 ; i < n ; i ++ ) { string dir = " " ; while ( i < n && path [ i ] != ' / ' ) { dir += path [ i ] ; i ++ ; } if ( dir == " . . " ) { if ( v . Count != 0 ) v . RemoveAt ( v . Count - 1 ) ; } else if ( dir == " . " dir == " " ) { } else { v . Add ( dir ) ; } } foreach ( string i in v ) { ans += " / " + i ; } if ( ans == " " ) return " / " ; return ans ; } static void Main ( ) { string str = " / a / . / b / . . / . . / c / " ; string res = simplify ( str ) ; Console . Write ( res ) ; } }
Length of the smallest sub | C # program to find the length of the smallest substring consisting of maximum distinct characters ; Find maximum distinct characters in any string ; Initialize all character 's count with 0 ; Increase the count in array if a character is found ; size of given string ; Find maximum distinct characters in any string ; result ; Brute force approach to find all substrings ; We have to check here both conditions together 1. substring ' s ▁ distinct ▁ characters ▁ is ▁ equal ▁ ▁ to ▁ maximum ▁ distinct ▁ characters ▁ ▁ 2 . ▁ substring ' s length should be minimum ; Driver program to test above function ; Input String
using System ; class GFG { static int NO_OF_CHARS = 256 ; static int max_distinct_char ( String str , int n ) { int [ ] count = new int [ NO_OF_CHARS ] ; for ( int i = 0 ; i < n ; i ++ ) { count [ str [ i ] ] ++ ; } int max_distinct = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] != 0 ) { max_distinct ++ ; } } return max_distinct ; } static int smallesteSubstr_maxDistictChar ( String str ) { int n = str . Length ; int max_distinct = max_distinct_char ( str , n ) ; int minl = n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { String subs = null ; if ( i < j ) subs = str . Substring ( i , str . Length - j ) ; else subs = str . Substring ( j , str . Length - i ) ; int subs_lenght = subs . Length ; int sub_distinct_char = max_distinct_char ( subs , subs_lenght ) ; if ( subs_lenght < minl && max_distinct == sub_distinct_char ) { minl = subs_lenght ; } } } return minl ; } static public void Main ( String [ ] args ) { String str = " AABBBCBB " ; int len = smallesteSubstr_maxDistictChar ( str ) ; Console . WriteLine ( " ▁ The ▁ length ▁ of ▁ the ▁ smallest ▁ substring " + " ▁ consisting ▁ of ▁ maximum ▁ distinct ▁ " + " characters ▁ : ▁ " + len ) ; } }
Evaluate a boolean expression represented as string | C # program to evaluate value of an expression . ; Evaluates boolean expression and returns the result ; Traverse all operands by jumping a character after every iteration . ; If operator next to current operand is AND . ; If operator next to current operand is OR . ; If operator next to current operand is XOR ( Assuming a valid input ) ; Driver code
using System ; using System . Text ; class GFG { public static int evaluateBoolExpr ( StringBuilder s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i += 2 ) { if ( i + 1 < n && i + 2 < n ) { if ( s [ i + 1 ] == ' A ' ) { if ( s [ i + 2 ] == '0' s [ i ] == 0 ) { s [ i + 2 ] = '0' ; } else { s [ i + 2 ] = '1' ; } } else if ( ( i + 1 ) < n && s [ i + 1 ] == ' B ' ) { if ( s [ i + 2 ] == '1' s [ i ] == '1' ) { s [ i + 2 ] = '1' ; } else { s [ i + 2 ] = '0' ; } } else { if ( s [ i + 2 ] == s [ i ] ) { s [ i + 2 ] = '0' ; } else { s [ i + 2 ] = '1' ; } } } } return s [ n - 1 ] - '0' ; } public static void Main ( string [ ] args ) { string s = "1C1B1B0A0" ; StringBuilder sb = new StringBuilder ( s ) ; Console . WriteLine ( evaluateBoolExpr ( sb ) ) ; } }
Efficiently find first repeated character in a string without using any additional data structure in one traversal | C # program to Efficiently check First repeated character ; Returns - 1 if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code
using System ; public class First_Repeated_char { static int FirstRepeated ( string str ) { int checker = 0 ; for ( int i = 0 ; i < str . Length ; ++ i ) { int val = ( str [ i ] - ' a ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return i ; checker |= ( 1 << val ) ; } return - 1 ; } public static void Main ( ) { string s = " abcfdeacf " ; int i = FirstRepeated ( s ) ; if ( i != - 1 ) Console . WriteLine ( " Char ▁ = ▁ " + s [ i ] + " ▁ and ▁ Index ▁ = ▁ " + i ) ; else Console . WriteLine ( " No ▁ repeated ▁ Char " ) ; } }
Check if a string is Pangrammatic Lipogram | C # program to check if a string is Pangrammatic Lipogram ; collection of letters ; function to check for a Pangrammatic Lipogram ; convert string to lowercase ; variable to keep count of all the letters not found in the string ; traverses the string for every letter of the alphabet ; if character not found in string then increment count ; Driver program to test above function
using System ; class GFG { static String alphabets = " abcdefghijklmnopqrstuvwxyz " ; static void panLipogramChecker ( char [ ] s ) { for ( int i = 0 ; i < s . Length ; i ++ ) { s [ i ] = char . ToLower ( s [ i ] ) ; } int counter = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int pos = find ( s , alphabets [ i ] ) ; if ( pos < 0 pos > s . Length ) counter += 1 ; } if ( counter == 0 ) Console . WriteLine ( " Pangram " ) ; else if ( counter >= 2 ) Console . WriteLine ( " Not ▁ a ▁ pangram ▁ but ▁ might ▁ a ▁ lipogram " ) ; else Console . WriteLine ( " Pangrammatic ▁ Lipogram " ) ; } static int find ( char [ ] arr , char c ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( c == arr [ i ] ) return 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { char [ ] str = " The ▁ quick ▁ brown ▁ fox ▁ jumped ▁ over ▁ the ▁ lazy ▁ dog " . ToCharArray ( ) ; panLipogramChecker ( str ) ; str = " The ▁ quick ▁ brown ▁ fox ▁ jumps ▁ over ▁ the ▁ lazy ▁ dog " . ToCharArray ( ) ; panLipogramChecker ( str ) ; str = " The ▁ quick ▁ brown ▁ fox ▁ jum ▁ over ▁ the ▁ lazy ▁ dog " . ToCharArray ( ) ; panLipogramChecker ( str ) ; } }
Lexicographically n | C # program to print nth permutation with using next_permute ( ) ; Function to print nth permutation using next_permute ( ) ; Sort the string in lexicographically ascending order ; Keep iterating until we reach nth position ; check for nth iteration ; print string after nth iteration ; Utility function ; Driver code
using System ; class GFG { static void nPermute ( char [ ] str , int n ) { Array . Sort ( str ) ; int i = 1 ; do { if ( i == n ) break ; i ++ ; } while ( next_permutation ( str ) ) ; Console . WriteLine ( String . Join ( " " , str ) ) ; } static bool next_permutation ( char [ ] p ) { for ( int a = p . Length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . Length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { char t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . Length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void Main ( ) { String str = " GEEKSFORGEEKS " ; int n = 100 ; nPermute ( str . ToCharArray ( ) , n ) ; } }
Split numeric , alphabetic and special symbols from a String | C # program to split an alphanumeric string using stringbuffer ; Driver code
using System ; using System . Text ; class GFG { static void splitString ( string str ) { StringBuilder alpha = new StringBuilder ( ) ; StringBuilder num = new StringBuilder ( ) ; StringBuilder special = new StringBuilder ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( Char . IsDigit ( str [ i ] ) ) num . Append ( str [ i ] ) ; else if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) alpha . Append ( str [ i ] ) ; else special . Append ( str [ i ] ) ; } Console . WriteLine ( alpha ) ; Console . WriteLine ( num ) ; Console . WriteLine ( special ) ; } public static void Main ( ) { string str = " geeks01 $ $ for02geeks03 ! @ ! ! " ; splitString ( str ) ; } }
Program to print all substrings of a given string | C # program to print all substrings of a string ; Function to print all substring ; Pick starting point in outer loop and lengths of different strings for a given starting point ; Driver code
using System ; public class GFG { public static void SubString ( String str , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; j <= n - i ; j ++ ) Console . WriteLine ( str . Substring ( i , j ) ) ; } public static void Main ( ) { String str = " abcd " ; SubString ( str , str . Length ) ; } }
Program to print all substrings of a given string | C # program for the above approach ; finding the length of the string ; outermost for loop this is for the selection of starting point ; 2 nd for loop is for selection of ending point ; 3 rd loop is for printing from starting point to ending point ; changing the line after printing from starting point to ending point ; Driver Code ; calling method for printing substring
using System ; public class GFG { public static void printSubstrings ( String str ) { int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { for ( int k = i ; k <= j ; k ++ ) { Console . Write ( str [ k ] ) ; } Console . WriteLine ( ) ; } } } public static void Main ( String [ ] args ) { String str = " abcd " ; printSubstrings ( str ) ; } }