codeparrot/xlcost-text-to-code huggingface dataset:
Convert given string so that it holds only distinct characters | C ++ implementation of the approach ; Function to return the index of the character that has 0 occurrence starting from index i ; If current character has 0 occurrence ; If no character has 0 occurrence ; Function to return the modified string which consists of distinct characters ; String cannot consist of all distinct characters ; Count the occurrences for each of the character ; Index for the first character that hasn 't appeared in the string ; If current character appeared more than once then it has to be replaced with some character that hasn 't occurred yet ; Decrement current character 's occurrence by 1 ; Replace the character ; Update the new character ' s ▁ occurrence ▁ ▁ This ▁ step ▁ can ▁ also ▁ be ▁ skipped ▁ as ▁ ▁ we ' ll never encounter this character in the string because it has been added just now ; Find the next character that hasn 't occurred yet ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nextZero ( int i , int occurrences [ ] ) { while ( i < 26 ) { if ( occurrences [ i ] == 0 ) return i ; i ++ ; } return -1 ; } string getModifiedString ( string str ) { int n = str . length ( ) ; if ( n > 26 ) return " - 1" ; string ch = str ; int i , occurrences [ 26 ] = { 0 } ; for ( i = 0 ; i < n ; i ++ ) occurrences [ ch [ i ] - ' a ' ] ++ ; int index = nextZero ( 0 , occurrences ) ; for ( i = 0 ; i < n ; i ++ ) { if ( occurrences [ ch [ i ] - ' a ' ] > 1 ) { occurrences [ ch [ i ] - ' a ' ] -- ; ch [ i ] = ( char ) ( ' a ' + index ) ; occurrences [ index ] = 1 ; index = nextZero ( index + 1 , occurrences ) ; } } cout << ch << endl ; } int main ( ) { string str = " geeksforgeeks " ; getModifiedString ( str ) ; }
Replace all occurrences of a string with space | C ++ implementation to extract the secret message ; Function to extract the secret message ; Replacing all occurrences of Sub in Str by empty spaces ; Removing unwanted spaces in the start and end of the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string trim ( const string & s ) { auto start = s . begin ( ) ; while ( start != s . end ( ) && isspace ( * start ) ) start ++ ; auto end = s . end ( ) ; do { end -- ; } while ( distance ( start , end ) > 0 && isspace ( * end ) ) ; return string ( start , end + 1 ) ; } string extractSecretMessage ( string str , string sub ) { size_t pos ; while ( ( pos = str . find ( sub ) ) != string :: npos ) str . replace ( pos , 3 , " ▁ " ) ; str = trim ( str ) ; return str ; } int main ( int argc , char const * argv [ ] ) { string str = " LIELIEILIEAMLIECOOL " ; string sub = " LIE " ; cout << extractSecretMessage ( str , sub ) << endl ; return 0 ; }
Sub | C ++ implementation of the approach ; Function to return the count of sub - strings starting from startIndex that are also the prefixes of str ; Function to return the count of all possible sub - strings of str that are also the prefixes of str ; If current character is equal to the starting character of str ; Driver code ; Function Call
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int subStringsStartingHere ( string str , int n , int startIndex ) { int count = 0 , i = 1 ; while ( i <= n ) { if ( str . substr ( 0 , i ) == str . substr ( startIndex , i ) ) { count ++ ; } else break ; i ++ ; } return count ; } int countSubStrings ( string str , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) count += subStringsStartingHere ( str , n , i ) ; } return count ; } int main ( ) { string str = " abcda " ; int n = str . length ( ) ; cout << ( countSubStrings ( str , n ) ) ; }
Binary Search a String | CPP program to implement Binary Search for strings ; Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binarySearch ( string arr [ ] , string x , int n ) { int l = 0 ; int r = n - 1 ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( x == ( arr [ m ] ) ) res = 0 ; if ( res == 0 ) return m ; if ( x > ( arr [ m ] ) ) l = m + 1 ; else r = m - 1 ; } return -1 ; } int main ( ) { string arr [ ] = { " contribute " , " geeks " , " ide " , " practice " } ; string x = " ide " ; int n = 4 ; int result = binarySearch ( arr , x , n ) ; if ( result == -1 ) cout << ( " Element ▁ not ▁ present " ) ; else cout << ( " Element ▁ found ▁ at ▁ index ▁ " ) << result ; }
Count characters in a string whose ASCII values are prime | C ++ implementation of above approach ; Function to find prime characters in the string ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a Boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Traverse all the characters ; Driver program ; print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define max_val  257 NEW_LINE int PrimeCharacters ( string s ) { vector < bool > prime ( max_val + 1 , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( prime [ int ( s [ i ] ) ] ) count ++ ; } return count ; } int main ( ) { string S = " geeksforgeeks " ; cout << PrimeCharacters ( S ) ; return 0 ; }
Students with maximum average score of three subjects | C ++ program to find the list of students having maximum average score ; Function to find the list of students having maximum average score Driver code ; Variables to store average score of a student and maximum average score ; List to store names of students having maximum average score ; Traversing the file data ; finding average score of a student ; Clear the list and add name of student having current maximum average score in the list ; Printing the maximum average score and names of students having this maximum average score as per the order in the file . ; Driver code ; Number of elements in string array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getStudentsList ( string file [ ] , int n ) { int avgScore ; int maxAvgScore = INT_MIN ; vector < string > names ; for ( int i = 0 ; i < n ; i += 4 ) { avgScore = ( stoi ( file [ i + 1 ] ) + stoi ( file [ i + 2 ] ) + stoi ( file [ i + 3 ] ) ) / 3 ; if ( avgScore > maxAvgScore ) { maxAvgScore = avgScore ; names . clear ( ) ; names . push_back ( file [ i ] ) ; } else if ( avgScore == maxAvgScore ) names . push_back ( file [ i ] ) ; } for ( int i = 0 ; i < names . size ( ) ; i ++ ) { cout << names [ i ] + " ▁ " ; } cout << maxAvgScore ; } int main ( ) { string file [ ] = { " Shrikanth " , "20" , "30" , "10" , " Ram " , "100" , "50" , "10" } ; int n = sizeof ( file ) / sizeof ( file [ 0 ] ) ; getStudentsList ( file , n ) ; }
Number of sub | C ++ program to find the number of sub - strings of s1 which are anagram of any sub - string of s2 ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of string pat [ ] in string txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; cout << pat << " ▁ " << txt << " ▁ " ; ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Function to return the number of sub - strings of s1 that are anagrams of any sub - string of s2 ; initializing variables ; outer loop for picking starting point ; loop for different length of substrings ; If s2 has any substring which is anagram of s1 . substr ( i , len ) ; increment the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ALL_CHARS  256 NEW_LINE bool compare ( char * arr1 , char * arr2 ) { for ( int i = 0 ; i < ALL_CHARS ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } bool search ( string pat , string txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int i ; char countP [ ALL_CHARS ] = { 0 } ; char countTW [ ALL_CHARS ] = { 0 } ; for ( i = 0 ; i < M ; i ++ ) { ( countP [ pat [ i ] ] ) ++ ; ( countTW [ txt [ i ] ] ) ++ ; } for ( i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) { return true ; } ( countTW [ txt [ i ] ] ) ++ ; countTW [ txt [ i - M ] ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } int calculatesubString ( string s1 , string s2 , int n ) { int count = 0 , j = 0 , x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = 1 ; len <= n - i ; len ++ ) { if ( search ( s1 . substr ( i , len ) , s2 ) ) { count = count + 1 ; } } } return count ; } int main ( ) { string str1 = " PLEASEHELPIMTRAPPED " ; string str2 = " INAKICKSTARTFACTORY " ; int len = str1 . length ( ) ; cout << calculatesubString ( str1 , str2 , len ) ; return 0 ; }
Sum of the alphabetical values of the characters of a string | C ++ implementation of the approach ; Function to find string score ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int strScore ( string str [ ] , string s , int n ) { int score = 0 , index ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == s ) { for ( int j = 0 ; j < s . length ( ) ; j ++ ) score += s [ j ] - ' a ' + 1 ; index = i + 1 ; break ; } } score = score * index ; return score ; } int main ( ) { string str [ ] = { " sahil " , " shashanak " , " sanjit " , " abhinav " , " mohit " } ; string s = " abhinav " ; int n = sizeof ( str ) / sizeof ( str [ 0 ] ) ; int score = strScore ( str , s , n ) ; cout << score << endl ; return 0 ; }
Minimum swaps to group similar characters side by side ? | checks whether a string has similar characters side by side ; If similar chars side by side , continue ; If we have found a char equal to current char and does not exist side to it , return false ; counts min swap operations to convert a string that has similar characters side by side ; Base case ; considering swapping of i and l chars ; Backtrack ; not considering swapping of i and l chars ; taking min of above two ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sameCharAdj ( string str ) { int n = str . length ( ) , i ; set < char > st ; st . insert ( str [ 0 ] ) ; for ( i = 1 ; i < n ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) continue ; if ( st . find ( str [ i ] ) != st . end ( ) ) return false ; st . insert ( str [ i ] ) ; } return true ; } int minSwaps ( string str , int l , int r , int cnt , int minm ) { if ( l == r ) { if ( sameCharAdj ( str ) ) return cnt ; else return INT_MAX ; } for ( int i = l + 1 ; i <= r ; i ++ ) { swap ( str [ i ] , str [ l ] ) ; cnt ++ ; int x = minSwaps ( str , l + 1 , r , cnt , minm ) ; swap ( str [ i ] , str [ l ] ) ; cnt -- ; int y = minSwaps ( str , l + 1 , r , cnt , minm ) ; minm = min ( minm , min ( x , y ) ) ; } return minm ; } int main ( ) { string str = " abbaacb " ; int n = str . length ( ) , cnt = 0 , minm = INT_MAX ; cout << minSwaps ( str , 0 , n - 1 , cnt , minm ) << endl ; return 0 ; }
Union | Naive implementation of find ; Naive implementation of union ( )
int find ( int parent [ ] , int i ) { if ( parent [ i ] == -1 ) return i ; return find ( parent , parent [ i ] ) ; } void Union ( int parent [ ] , int x , int y ) { int xset = find ( parent , x ) ; int yset = find ( parent , y ) ; parent [ xset ] = yset ; }
Get K | CPP implementation of above approach ; Function to return the K - th letter from new String . ; finding size = length of new string S ' ; get the K - th letter ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string K_thletter ( string S , int K ) { int N = S . size ( ) ; long size = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( isdigit ( S [ i ] ) ) size = size * ( S [ i ] - '0' ) ; else size += 1 ; } for ( int i = N - 1 ; i >= 0 ; -- i ) { K %= size ; if ( K == 0 && isalpha ( S [ i ] ) ) return ( string ) " " + S [ i ] ; if ( isdigit ( S [ i ] ) ) size = size / ( S [ i ] - '0' ) ; else size -= 1 ; } } int main ( ) { string S = " geeks2for2" ; int K = 15 ; cout << K_thletter ( S , K ) ; return 0 ; }
Minimum number of Parentheses to be added to make it valid | C ++ Program to find minimum number of ' ( ' or ' ) ' must be added to make Parentheses string valid . ; Function to return required minimum number ; maintain balance of string ; It is guaranteed bal >= - 1 ; Driver code ; Function to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minParentheses ( string p ) { int bal = 0 ; int ans = 0 ; for ( int i = 0 ; i < p . length ( ) ; ++ i ) { bal += p [ i ] == ' ( ' ? 1 : -1 ; if ( bal == -1 ) { ans += 1 ; bal += 1 ; } } return bal + ans ; } int main ( ) { string p = " ( ) ) " ; cout << minParentheses ( p ) ; return 0 ; }
Check if suffix and prefix of a string are palindromes | C ++ implementation of the approach ; Function to check whether the string is a palindrome ; reverse the string to compare with the original string ; check if both are same ; Function to check whether the string has prefix and suffix substrings of length greater than 1 which are palindromes . ; check all prefix substrings ; check if the prefix substring is a palindrome ; If we did not find any palindrome prefix of length greater than 1. ; check all suffix substrings , as the string is reversed now ; check if the suffix substring is a palindrome ; If we did not find a suffix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string r ) { string p = r ; reverse ( p . begin ( ) , p . end ( ) ) ; return ( r == p ) ; } bool CheckStr ( string s ) { int l = s . length ( ) ; int i ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substr ( 0 , i ) ) ) break ; } if ( i == ( l + 1 ) ) return false ; i = 2 ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substr ( l - i , i ) ) ) return true ; } return false ; } int main ( ) { string s = " abccbarfgdbd " ; if ( CheckStr ( s ) ) cout << " YES STRNEWLINE " ; else cout << " NO STRNEWLINE " ; return 0 ; }
Rotations of a Binary String with Odd Value | CPP program to find count of rotations with odd value . ; function to calculate total odd decimal equivalent ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int oddEquivalent ( string s , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) count ++ ; } return count ; } int main ( ) { string s = "1011011" ; int n = s . length ( ) ; cout << oddEquivalent ( s , n ) ; return 0 ; }
Minimum operation require to make first and last character same | C ++ program to find minimum operation require to make first and last character same ; Return the minimum operation require to make string first and last character same . ; Store indexes of first occurrences of characters . ; Initialize result ; Traverse through all characters ; Find first occurrence ; Update result for subsequent occurrences ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  256 NEW_LINE int minimumOperation ( string s ) { int n = s . length ( ) ; vector < int > first_occ ( MAX , -1 ) ; int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { char x = s [ i ] ; if ( first_occ [ x ] == -1 ) first_occ [ x ] = i ; else { int last_occ = ( n - i - 1 ) ; res = min ( res , first_occ [ x ] + last_occ ) ; } } return res ; } int main ( ) { string s = " bacdefghipalop " ; cout << minimumOperation ( s ) << endl ; return 0 ; }
Lexicographically smallest and largest substring of size k | CPP program to find lexicographically largest and smallest substrings of size k . ; Initialize min and max as first substring of size k ; Consider all remaining substrings . We consider every substring ending with index i . ; Print result . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getSmallestAndLargest ( string s , int k ) { string currStr = s . substr ( 0 , k ) ; string lexMin = currStr ; string lexMax = currStr ; for ( int i = k ; i < s . length ( ) ; i ++ ) { currStr = currStr . substr ( 1 , k ) + s [ i ] ; if ( lexMax < currStr ) lexMax = currStr ; if ( lexMin > currStr ) lexMin = currStr ; } cout << ( lexMin ) << endl ; cout << ( lexMax ) << endl ; } int main ( ) { string str = " GeeksForGeeks " ; int k = 3 ; getSmallestAndLargest ( str , k ) ; }
Program to print the initials of a name with the surname | C ++ program to print the initials of a name with the surname ; to remove any leading or trailing spaces ; to store extracted words ; forming the word ; when space is encountered it means the name is completed and thereby extracted ; printing the first letter of the name in capital letters ; for the surname , we have to print the entire surname and not just the initial string " t " has the surname now ; first letter of surname in capital letter ; rest of the letters in small ; printing surname ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printInitials ( string str ) { int len = str . length ( ) ; str . erase ( 0 , str . find_first_not_of ( ' ▁ ' ) ) ; str . erase ( str . find_last_not_of ( ' ▁ ' ) + 1 ) ; string t = " " ; for ( int i = 0 ; i < len ; i ++ ) { char ch = str [ i ] ; if ( ch != ' ▁ ' ) t = t + ch ; else { cout << ( char ) toupper ( t [ 0 ] ) << " . ▁ " ; t = " " ; } } string temp = " " ; for ( int j = 0 ; j < t . length ( ) ; j ++ ) { if ( j == 0 ) temp = temp + ( char ) toupper ( t [ 0 ] ) ; else temp = temp + ( char ) tolower ( t [ j ] ) ; } cout << temp << endl ; } int main ( ) { string str = " ishita ▁ bhuiya " ; printInitials ( str ) ; }
Count of strings that can be formed from another string using each character at | / C ++ program to print the number of times str2 can be formed from str1 using the characters of str1 only once ; Function to find the number of str2 that can be formed using characters of str1 ; iterate and mark the frequencies of all characters in str1 ; find the minimum frequency of every character in str1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumberOfTimes ( string str1 , string str2 ) { int freq [ 26 ] = { 0 } ; int freq2 [ 26 ] = { 0 } ; int l1 = str1 . length ( ) ; int l2 = str2 . length ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) freq [ str1 [ i ] - ' a ' ] += 1 ; for ( int i = 0 ; i < l2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] += 1 ; int count = INT_MAX ; for ( int i = 0 ; i < l2 ; i ++ ) { if ( freq2 [ str2 [ i ] - ' a ' ] != 0 ) count = min ( count , freq [ str2 [ i ] - ' a ' ] / freq2 [ str2 [ i ] - ' a ' ] ) ; } return count ; } int main ( ) { string str1 = " foreeksgekseg " ; string str2 = " geeks " ; cout << findNumberOfTimes ( str1 , str2 ) << endl ; return 0 ; }
String transformation using XOR and OR | C ++ program to check if string1 can be converted to string2 using XOR and OR operations ; function to check if conversion is possible or not ; if lengths are different ; iterate to check if both strings have 1 ; to check if there is even one 1 in string s1 ; to check if there is even one 1 in string s2 ; if both strings have only '0' ; if both string do not have a '1' . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool solve ( string s1 , string s2 ) { bool flag1 = 0 , flag2 = 0 ; if ( s1 . length ( ) != s2 . length ( ) ) return false ; int l = s1 . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( s1 [ i ] == '1' ) flag1 = 1 ; if ( s2 [ i ] == '1' ) flag2 = 1 ; if ( flag1 && flag2 ) return true ; } if ( ! flag1 && ! flag2 ) return true ; return false ; } int main ( ) { string s1 = "100101" ; string s2 = "100000" ; if ( solve ( s1 , s2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Smallest alphabet greater than a given character | C ++ Program to find the smallest character from the given set of letter , which is greater than the target element ; Returns the smallest character from the given set of letters that is greater than K ; Take the first element as l and the rightmost element as r ; if this while condition does not satisfy simply return the first element . ; Return the smallest element ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; char nextGreatestAlphabet ( vector < char > & alphabets , char K ) { int n = alphabets . size ( ) ; if ( K >= alphabets [ n - 1 ] ) return alphabets [ 0 ] ; int l = 0 , r = alphabets . size ( ) - 1 ; int ans = -1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( alphabets [ mid ] > K ) { r = mid - 1 ; ans = mid ; } else l = mid + 1 ; } return alphabets [ ans ] ; } int main ( ) { vector < char > letters { ' A ' , ' K ' , ' S ' } ; char K = ' L ' ; char result = nextGreatestAlphabet ( letters , K ) ; cout << result << endl ; return 0 ; }
Longest substring of 0 s in a string formed by k concatenations | C ++ code to find maximum length of substring that contains only 0 's ; Function to calculate maximum length of substring containing only zero ; loop to first calculate longest substring in string ; if all elements in string are '0' ; Else , find size of prefix and suffix containing only zeroes ; Calculate prefix containing only zeroes ; Calculate suffix containing only zeroes ; if k <= 1 then there is no need to take prefix + suffix into account ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subzero ( string str , int k ) { int ans = 0 , curr = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( str [ i ] == '0' ) curr ++ ; else curr = 0 ; ans = max ( ans , curr ) ; } if ( ans == len ) return len * k ; else { int pre = 0 , suff = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) pre ++ ; else break ; } for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) suff ++ ; else break ; } if ( k > 1 ) ans = max ( ans , pre + suff ) ; return ans ; } } int main ( ) { string str = "00100110" ; int k = 5 ; cout << subzero ( str , k ) ; return 0 ; }
Find nth term of the Dragon Curve Sequence | CPP code to find nth term of the Dragon Curve Sequence ; function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Driver program ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output
#include <bits/stdc++.h> NEW_LINE using namespace std ; string Dragon_Curve_Sequence ( int n ) { string s = "1" ; for ( int i = 2 ; i <= n ; i ++ ) { string temp = "1" ; char prev = '1' , zero = '0' , one = '1' ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { temp += s [ j ] ; if ( prev == '0' ) { temp += one ; prev = one ; } else { temp += zero ; prev = zero ; } } s = temp ; } return s ; } int main ( ) { int n = 4 ; string s = Dragon_Curve_Sequence ( n ) ; cout << s << " STRNEWLINE " ; }
Number of substrings of a string | CPP program to count number of substrings of a string ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNonEmptySubstr ( string str ) { int n = str . length ( ) ; return n * ( n + 1 ) / 2 ; } int main ( ) { string s = " abcde " ; cout << countNonEmptySubstr ( s ) ; return 0 ; }