codeparrot/xlcost-text-to-code huggingface dataset:
Print string after removing all ( “ 10 ” or “ 01 ” ) from the binary string | C ++ program to print the final string after removing all the occurrences of "10" and "01" from the given binary string ; Function to print the final string after removing all the occurrences of "10" and "01" from the given binary string ; Variables to store the count of 1 ' s ▁ and ▁ 0' s ; Variable left will store whether 0 ' s ▁ or ▁ 1' s is left in the final string ; Length of the string ; For loop to count the occurrences of 1 ' s ▁ and ▁ 0' s in the string ; To check if the count of 1 ' s ▁ is ▁ ▁ greater ▁ than ▁ the ▁ count ▁ of ▁ 0' s or not . If x is greater , then those many 1 's  are printed. ; Length of the final remaining string after removing all the occurrences ; Printing the final string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void finalString ( string str ) { int x = 0 , y = 0 ; int left ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) x ++ ; else y ++ ; } if ( x > y ) left = 1 ; else left = 0 ; int length = n - 2 * min ( x , y ) ; for ( int i = 0 ; i < length ; i ++ ) { cout << left ; } } int main ( ) { string str = "010110100100000" ; finalString ( str ) ; return 0 ; }
Longest palindrome formed by concatenating and reordering strings of equal length | C ++ program to find the Longest palindrome that can be formed by concatenating and reordering given N strings of equal length ; Function to print the longest palindrome ; Printing every string in left vector ; Printing the palindromic string in the middle ; Printing the reverse of the right vector to make the final output palindromic ; Function to find and print the longest palindrome that can be formed ; Inserting each string in the set ; Vectors to add the strings in the left and right side ; To add the already present palindrome string in the middle of the solution ; Iterating through all the given strings ; If the string is a palindrome it is added in the middle ; Checking if the reverse of the string is already present in the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPalindrome ( vector < string > left , string mid , vector < string > right ) { for ( string x : left ) cout << x ; cout << mid ; reverse ( right . begin ( ) , right . end ( ) ) ; for ( string x : right ) cout << x ; cout << endl ; } void findPalindrome ( vector < string > & S , int N , int M ) { set < string > dict ; for ( int i = 0 ; i < M ; i ++ ) { cin >> S [ i ] ; dict . insert ( S [ i ] ) ; } vector < string > left , right ; string mid ; for ( int i = 0 ; i < N ; i ++ ) { string t = S [ i ] ; reverse ( t . begin ( ) , t . end ( ) ) ; if ( t == S [ i ] ) mid = t ; else if ( dict . find ( t ) != dict . end ( ) ) { left . push_back ( S [ i ] ) ; right . push_back ( t ) ; dict . erase ( S [ i ] ) ; dict . erase ( t ) ; } } printPalindrome ( left , mid , right ) ; } int main ( ) { vector < string > S { " tab " , " one " , " bat " } ; int M = 3 ; int N = S . size ( ) ; findPalindrome ( S , N , M ) ; }
Lexicographically smaller string by swapping at most one character pair | C ++ program check whether is it possible to make string A lexicographically smaller than string B ; Swap function ; Function that finds whether is it possible to make string A lexicographically smaller than string B ; Condition if string A is already smaller than B ; Sorting temp string ; Condition for first changed character of string A and temp ; Condition if string A is already sorted ; Finding first changed character from last of string A ; Swap the two characters ; Condition if string A is smaller than B ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( char & x , char & y ) { char temp = x ; x = y ; y = temp ; } bool IsLexicographicallySmaller ( string A , string B ) { if ( A < B ) { return true ; } string temp = A ; sort ( temp . begin ( ) , temp . end ( ) ) ; int index = -1 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A [ i ] != temp [ i ] ) { index = i ; break ; } } if ( index == -1 ) { return false ; } int j ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A [ i ] == temp [ index ] ) j = i ; } swap ( A [ index ] , A [ j ] ) ; if ( A < B ) { return true ; } else { return false ; } } int main ( ) { string A = " AGAIN " ; string B = " ACTION " ; if ( IsLexicographicallySmaller ( A , B ) ) { cout << " Yes " << " STRNEWLINE " ; } else { cout << " No " << " STRNEWLINE " ; } return 0 ; }
Length of longest palindromic sub | C ++ implementation to find the length of longest palindromic sub - string using Recursion ; Function to find maximum of the two variables ; Function to find the longest palindromic substring : Recursion ; Base condition when the start index is greater than end index ; Base condition when both the start and end index are equal ; Condition when corner characters are equal in the string ; Recursive call to find the longest Palindromic string by excluding the corner characters ; Recursive call to find the longest Palindromic string by including one corner character at a time ; Function to find the longest palindromic sub - string ; Utility function call ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; int max ( int x , int y ) { return ( x > y ) ? x : y ; } int longestPalindromic ( string str , int i , int j , int count ) { if ( i > j ) return count ; if ( i == j ) return ( count + 1 ) ; if ( str [ i ] == str [ j ] ) { count = longestPalindromic ( str , i + 1 , j - 1 , count + 2 ) ; return max ( count , max ( longestPalindromic ( str , i + 1 , j , 0 ) , longestPalindromic ( str , i , j - 1 , 0 ) ) ) ; } return max ( longestPalindromic ( str , i + 1 , j , 0 ) , longestPalindromic ( str , i , j - 1 , 0 ) ) ; } int longest_palindromic_substr ( string str ) { return longestPalindromic ( str , 0 , str . length ( ) - 1 , 0 ) ; } int main ( ) { string str = " aaaabbaa " ; cout << longest_palindromic_substr ( str ) ; return 0 ; }
Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency | C ++ implementation to find the prefix of the s such that occurrence of each character is atmost the count of minimum frequency in the s ; Function to find the maximum possible prefix of the s ; Hash map to store the frequency of the characters in the s ; Iterate over the s to find the occurence of each Character ; Minimum frequency of the Characters ; Loop to find the count of minimum frequency in the hash - map ; Loop to find the maximum possible length of the prefix in the s ; Condition to check if the frequency is greater than minimum possible freq ; maxprefix s and its length . ; Driver code ; s is initialize . ; str is passed in MaxPrefix function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MaxPrefix ( string s ) { map < char , int > Dict ; for ( char i : s ) { Dict [ i ] ++ ; } int minfrequency = INT_MAX ; for ( auto x : Dict ) { minfrequency = min ( minfrequency , x . second ) ; } int countminFrequency = 0 ; for ( auto x : Dict ) { if ( x . second == minfrequency ) countminFrequency += 1 ; } map < char , int > mapper ; int indi = 0 ; for ( char i : s ) { mapper [ i ] += 1 ; if ( mapper [ i ] > countminFrequency ) break ; indi += 1 ; } cout << ( s . substr ( 0 , indi ) ) ; } int main ( ) { string str = " aabcdaab " ; MaxPrefix ( str ) ; }
Count of Substrings that can be formed without using the given list of Characters | C ++ implementation of the above approach ; Function to find the Number of sub - strings without using given character ; Mark the given characters in the freq array ; Count variable to store the count of the characters until a character from given L is encountered ; If a character from L is encountered , then the answer variable is incremented by the value obtained by using the mentioned formula and count is set to 0 ; For last remaining characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstring ( string & S , char L [ ] , int & n ) { int freq [ 26 ] = { 0 } , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ ( int ) ( L [ i ] - ' a ' ) ] = 1 ; } int count = 0 ; for ( auto x : S ) { if ( freq [ ( int ) ( x - ' a ' ) ] ) { ans += ( count * count + count ) / 2 ; count = 0 ; } else count ++ ; } ans += ( count * count + count ) / 2 ; return ans ; } int main ( ) { string S = " abcpxyz " ; char L [ ] = { ' a ' , ' p ' , ' q ' } ; int n = sizeof ( L ) / sizeof ( L [ 0 ] ) ; cout << countSubstring ( S , L , n ) ; return 0 ; }
Program to accept Strings starting with a Vowel | C ++ program to accept String starting with Vowel ; Function to check if first character is vowel ; Function to check ; Driver function
#include <iostream> NEW_LINE using namespace std ; int checkIfStartsWithVowels ( string str ) { if ( ! ( str [ 0 ] == ' A ' str [ 0 ] == ' a ' str [ 0 ] == ' E ' str [ 0 ] == ' e ' str [ 0 ] == ' I ' str [ 0 ] == ' i ' str [ 0 ] == ' O ' str [ 0 ] == ' o ' str [ 0 ] == ' U ' str [ 0 ] == ' u ' ) ) return 1 ; else return 0 ; } void check ( string str ) { if ( checkIfStartsWithVowels ( str ) ) cout << " Not ▁ Accepted STRNEWLINE " ; else cout << " Accepted STRNEWLINE " ; } int main ( ) { string str = " animal " ; check ( str ) ; str = " zebra " ; check ( str ) ; return 0 ; }
Find the Nth occurrence of a character in the given String | C ++ implementation to find the Nth occurrence of a character ; Function to find the Nth occurrence of a character ; Loop to find the Nth occurrence of the character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNthOccur ( string str , char ch , int N ) { int occur = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ch ) { occur += 1 ; } if ( occur == N ) return i ; } return -1 ; } int main ( ) { string str = " geeks " ; char ch = ' e ' ; int N = 2 ; cout << findNthOccur ( str , ch , N ) ; }
Longest equal substring with cost less than K | C ++ program to find the maximum length of equal substring within a given cost ; Function to find the maximum length ; Fill the prefix array with the difference of letters ; Update the maximum length ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( string X , string Y , int N , int K ) { int count [ N + 1 ] = { 0 } ; int sol = 0 ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count [ i ] = count [ i - 1 ] + abs ( X [ i - 1 ] - Y [ i - 1 ] ) ; } int j = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { while ( ( count [ i ] - count [ j ] ) > K ) { j ++ ; } sol = max ( sol , i - j ) ; } return sol ; } int main ( ) { int N = 4 ; string X = " abcd " , Y = " bcde " ; int K = 3 ; cout << solve ( X , Y , N , K ) << " STRNEWLINE " ; return 0 ; }
Jaro and Jaro | C ++ implementation of above approach ; Function to calculate the Jaro Similarity of two strings ; If the strings are equal ; Length of two strings ; Maximum distance upto which matching is allowed ; Count of matches ; Hash for matches ; Traverse through the first string ; Check if there is any matches ; If there is a match ; If there is no match ; Number of transpositions ; Count number of occurrences where two characters match but there is a third matched character in between the indices ; Find the next matched character in second string ; Return the Jaro Similarity ; Jaro Winkler Similarity ; If the jaro Similarity is above a threshold ; Find the length of common prefix ; If the characters match ; Else break ; Maximum of 4 characters are allowed in prefix ; Calculate jaro winkler Similarity ; Driver code ; Print Jaro - Winkler Similarity of two strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; double jaro_distance ( string s1 , string s2 ) { if ( s1 == s2 ) return 1.0 ; int len1 = s1 . length ( ) , len2 = s2 . length ( ) ; if ( len1 == 0 len2 == 0 ) return 0.0 ; int max_dist = floor ( max ( len1 , len2 ) / 2 ) - 1 ; int match = 0 ; int hash_s1 [ s1 . length ( ) ] = { 0 } , hash_s2 [ s2 . length ( ) ] = { 0 } ; for ( int i = 0 ; i < len1 ; i ++ ) { for ( int j = max ( 0 , i - max_dist ) ; j < min ( len2 , i + max_dist + 1 ) ; j ++ ) if ( s1 [ i ] == s2 [ j ] && hash_s2 [ j ] == 0 ) { hash_s1 [ i ] = 1 ; hash_s2 [ j ] = 1 ; match ++ ; break ; } } if ( match == 0 ) return 0.0 ; double t = 0 ; int point = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) if ( hash_s1 [ i ] ) { while ( hash_s2 [ point ] == 0 ) point ++ ; if ( s1 [ i ] != s2 [ point ++ ] ) t ++ ; } t /= 2 ; return ( ( ( double ) match ) / ( ( double ) len1 ) + ( ( double ) match ) / ( ( double ) len2 ) + ( ( double ) match - t ) / ( ( double ) match ) ) / 3.0 ; } double jaro_Winkler ( string s1 , string s2 ) { double jaro_dist = jaro_distance ( s1 , s2 ) ; if ( jaro_dist > 0.7 ) { int prefix = 0 ; for ( int i = 0 ; i < min ( s1 . length ( ) , s2 . length ( ) ) ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) prefix ++ ; else break ; } prefix = min ( 4 , prefix ) ; jaro_dist += 0.1 * prefix * ( 1 - jaro_dist ) ; } return jaro_dist ; } int main ( ) { string s1 = " TRATE " , s2 = " TRACE " ; cout << " Jaro - Winkler ▁ Similarity ▁ = " << jaro_Winkler ( s1 , s2 ) << endl ; return 0 ; }
Check if a word is present in a sentence | C ++ implementation of the approach ; Function that returns true if the word is found ; To break the sentence in words ; To temporarily store each individual word ; Comparing the current word with the word to be searched ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isWordPresent ( string sentence , string word ) { stringstream s ( sentence ) ; string temp ; while ( s >> temp ) { if ( temp . compare ( word ) == 0 ) { return true ; } } return false ; } int main ( ) { string s = " Geeks ▁ for ▁ Geeks " ; string word = " Geeks " ; if ( isWordPresent ( s , word ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if a word is present in a sentence | C ++ implementation of the approach ; Function that returns true if the word is found ; To convert the word in uppercase ; To convert the complete sentence in uppercase ; To break the sentence in words ; To store the individual words of the sentence ; Compare the current word with the word to be searched ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isWordPresent ( string sentence , string word ) { transform ( word . begin ( ) , word . end ( ) , word . begin ( ) , :: toupper ) ; transform ( sentence . begin ( ) , sentence . end ( ) , sentence . begin ( ) , :: toupper ) ; stringstream s ( sentence ) ; string temp ; while ( s >> temp ) { if ( temp . compare ( word ) == 0 ) { return true ; } } return false ; } int main ( ) { string s = " Geeks ▁ for ▁ Geeks " ; string word = " geeks " ; if ( isWordPresent ( s , word ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of 1 | C ++ implementation of the approach ; Function to return the count of required characters ; While there are characters left ; Single bit character ; Two - bit character ; Update the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countChars ( string str , int n ) { int i = 0 , cnt = 0 ; while ( i < n ) { if ( str [ i ] == '0' ) i ++ ; else i += 2 ; cnt ++ ; } return cnt ; } int main ( ) { string str = "11010" ; int n = str . length ( ) ; cout << countChars ( str , n ) ; return 0 ; }
Print the frequency of each character in Alphabetical order | C ++ implementation of the approach ; Function to print the frequency of each of the characters of s in alphabetical order ; To store the frequency of the characters ; Update the frequency array ; Print the frequency in alphatecial order ; If the current alphabet doesn 't  appear in the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 26 ; void compressString ( string s , int n ) { int freq [ MAX ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] == 0 ) continue ; cout << ( char ) ( i + ' a ' ) << freq [ i ] ; } } int main ( ) { string s = " geeksforgeeks " ; int n = s . length ( ) ; compressString ( s , n ) ; return 0 ; }
Find the number obtained after concatenation of binary representation of M and N | C ++ implementation of the approach ; Function to convert decimal number n to its binary representation stored as an array arr [ ] ; Funtion to convert the number represented as a binary array arr [ ] into its decimal equivalent ; Function to concatenate the binary numbers and return the decimal result ; Number of bits in both the numbers ; Convert the bits in both the integers to the arrays a [ ] and b [ ] ; c [ ] will be the binary array for the result ; Update the c [ ] array ; Return the decimal equivalent of the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void decBinary ( int arr [ ] , int n ) { int k = log2 ( n ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n /= 2 ; } } int binaryDec ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } int concat ( int m , int n ) { int k = log2 ( m ) + 1 ; int l = log2 ( n ) + 1 ; int a [ k ] = { 0 } , b [ l ] = { 0 } ; int c [ k + l ] = { 0 } ; decBinary ( a , m ) ; decBinary ( b , n ) ; int in = 0 ; for ( int i = 0 ; i < k ; i ++ ) c [ in ++ ] = a [ i ] ; for ( int i = 0 ; i < l ; i ++ ) c [ in ++ ] = b [ i ] ; return ( binaryDec ( c , k + l ) ) ; } int main ( ) { int m = 4 , n = 5 ; cout << concat ( m , n ) ; return 0 ; }
Find the number obtained after concatenation of binary representation of M and N | C ++ implementation of the approach ; Utility function to calculate binary length of a number . ; Function to concatenate the binary numbers and return the decimal result ; find binary length of n ; left binary shift m and then add n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getBinaryLength ( int n ) { int length = 0 ; while ( n > 0 ) { length += 1 ; n /= 2 ; } return length ; } int concat ( int m , int n ) { int length = getBinaryLength ( n ) ; return ( m << length ) + n ; } int main ( ) { int m = 4 , n = 5 ; cout << concat ( m , n ) ; return 0 ; }
XOR two binary strings of unequal lengths | C ++ implementation of the approach ; Function to insert n 0 s in the beginning of the given string ; Function to return the XOR of the given strings ; Lengths of the given strings ; Make both the strings of equal lengths by inserting 0 s in the beginning ; Updated length ; To store the resultant XOR ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addZeros ( string & str , int n ) { for ( int i = 0 ; i < n ; i ++ ) { str = "0" + str ; } } string getXOR ( string a , string b ) { int aLen = a . length ( ) ; int bLen = b . length ( ) ; if ( aLen > bLen ) { addZeros ( b , aLen - bLen ) ; } else if ( bLen > aLen ) { addZeros ( a , bLen - aLen ) ; } int len = max ( aLen , bLen ) ; string res = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( a [ i ] == b [ i ] ) res += "0" ; else res += "1" ; } return res ; } int main ( ) { string a = "11001" , b = "111111" ; cout << getXOR ( a , b ) ; return 0 ; }
Generate Binary Strings of length N using Branch and Bound | CPP Program to generate Binary Strings using Branch and Bound ; Creating a Node class ; Queue that maintains the list of live Nodes ; Utility function to generate binary strings of length n ; If list is full print combination ; Create a new vector for new combination ; iterate while length is not equal to n ; Driver Code ; Initiate Generation Create a root Node ; Instantiate the Queue
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int * soln ; int level ; vector < Node * > child ; Node * parent ; Node ( Node * parent , int level , int N ) { this -> parent = parent ; this -> level = level ; this -> soln = new int [ N ] ; } } ; queue < Node * > Q ; void generate ( Node * n , int & N , queue < Node * > & Q ) { if ( n -> level == N ) { for ( int i = 0 ; i < N ; i ++ ) cout << n -> soln [ i ] ; cout << endl ; } else { int l = n -> level ; for ( int i = 0 ; i <= 1 ; i ++ ) { Node * x = new Node ( n , l + 1 , N ) ; for ( int k = 0 ; k < l ; k ++ ) x -> soln [ k ] = n -> soln [ k ] ; x -> soln [ l ] = i ; n -> child . push_back ( x ) ; Q . push ( x ) ; } } } int main ( ) { int N = 3 ; Node * root ; root = new Node ( NULL , 0 , N ) ; Q . push ( root ) ; while ( ! Q . empty ( ) ) { Node * E = Q . front ( ) ; Q . pop ( ) ; generate ( E , N , Q ) ; } return 0 ; }
Minimum operations required to make the string satisfy the given condition | C ++ implementation of the approach ; Function to return the minimum operations required ; To store the first and the last occurrence of all the characters ; Set the first and the last occurrence of all the characters to - 1 ; Update the occurrences of the characters ; Only set the first occurrence if it hasn 't already been set ; To store the minimum operations ; If the frequency of the current character in the string is less than 2 ; Count of characters to be removed so that the string starts and ends at the current character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 26 ; int minOperation ( string str , int len ) { int first [ MAX ] , last [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { first [ i ] = -1 ; last [ i ] = -1 ; } for ( int i = 0 ; i < len ; i ++ ) { int index = ( str [ i ] - ' a ' ) ; if ( first [ index ] == -1 ) first [ index ] = i ; last [ index ] = i ; } int minOp = -1 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( first [ i ] == -1 first [ i ] == last [ i ] ) continue ; int cnt = len - ( last [ i ] - first [ i ] + 1 ) ; if ( minOp == -1 cnt < minOp ) minOp = cnt ; } return minOp ; } int main ( ) { string str = " abcda " ; int len = str . length ( ) ; cout << minOperation ( str , len ) ; return 0 ; }
Queries to find the count of vowels in the substrings of the given string | C ++ implementation of the approach ; Function that returns true if ch is a vowel ; Function to return the count of vowels in the substring str [ l ... r ] ; To store the count of vowels ; For every character in the index range [ l , r ] ; If the current character is a vowel ; For every query ; Find the count of vowels for the current query ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  2 NEW_LINE bool isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } int countVowels ( string str , int l , int r ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( isVowel ( str [ i ] ) ) cnt ++ ; } return cnt ; } void performQueries ( string str , int queries [ ] [ N ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) { cout << countVowels ( str , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) << " STRNEWLINE " ; } } int main ( ) { string str = " geeksforgeeks " ; int queries [ ] [ N ] = { { 1 , 3 } , { 2 , 4 } , { 1 , 9 } } ; int q = ( sizeof ( queries ) / sizeof ( queries [ 0 ] ) ) ; performQueries ( str , queries , q ) ; return 0 ; }
Convert a String to a Singly Linked List | C ++ program to Convert a String to a Singly Linked List ; Structure for a Singly Linked List ; Function to add a new node to the Linked List ; Function to convert the string to Linked List . ; curr pointer points to the current node where the insertion should take place ; Function to print the data present in all the nodes ; Driver code
#include <iostream> NEW_LINE using namespace std ; struct node { char data ; node * next ; } ; node * add ( char data ) { node * newnode = new node ; newnode -> data = data ; newnode -> next = NULL ; return newnode ; } node * string_to_SLL ( string text , node * head ) { head = add ( text [ 0 ] ) ; node * curr = head ; for ( int i = 1 ; i < text . size ( ) ; i ++ ) { curr -> next = add ( text [ i ] ) ; curr = curr -> next ; } return head ; } void print ( node * head ) { node * curr = head ; while ( curr != NULL ) { cout << curr -> data << " ▁ - > ▁ " ; curr = curr -> next ; } } int main ( ) { string text = " GEEKS " ; node * head = NULL ; head = string_to_SLL ( text , head ) ; print ( head ) ; return 0 ; }