codeparrot/xlcost-text-to-code huggingface dataset:
Minimum letters to be removed to make all occurrences of a given letter continuous | C ++ implementation of the above approach ; Function to find the minimum number of deletions required to make the occurrences of the given character K continuous ; Find the first occurrence of the given letter ; Iterate from the first occurrence till the end of the sequence ; Find the index from where the occurrence of the character is not continuous ; Update the answer with the number of elements between non - consecutive occurrences of the given letter ; Update the count for all letters which are not equal to the given letter ; Return the count ; Driver code ; Calling the function ; Calling the function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int noOfDeletions ( string str , char k ) { int ans = 0 , cnt = 0 , pos = 0 ; while ( pos < str . length ( ) && str [ pos ] != k ) { pos ++ ; } int i = pos ; while ( i < str . length ( ) ) { while ( i < str . length ( ) && str [ i ] == k ) { i = i + 1 ; } ans = ans + cnt ; cnt = 0 ; while ( i < str . length ( ) && str [ i ] != k ) { i = i + 1 ; cnt = cnt + 1 ; } } return ans ; } int main ( ) { string str1 = " ababababa " ; char k1 = ' a ' ; cout << noOfDeletions ( str1 , k1 ) << endl ; string str2 = " kprkkoinkopt " ; char k2 = ' k ' ; cout << noOfDeletions ( str2 , k2 ) << endl ; }
Check whether an array of strings can correspond to a particular number X | C ++ implementation to check whether array of strings can correspond to a number X ; Function to find the maximum base possible for the number N ; Function to find the decimal equivalent of the number ; Condition to check if the number is convertible to another base ; Function to check that the array can correspond to a number X ; counter to count the numbers those are convertible to X ; Loop to iterate over the array ; Convert the current string to every base for checking whether it will correspond to X from any base ; Condition to check if every number of the array can be converted to X ; Driver Code ; The set of strings in base from [ 2 , 36 ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } int toDeci ( string str , int base ) { int len = str . size ( ) ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base ) { return -1 ; } num += val ( str [ i ] ) * power ; power = power * base ; } return num ; } void checkCorrespond ( vector < string > str , int x ) { int counter = 0 ; int n = str . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 ; j <= 36 ; j ++ ) { if ( toDeci ( str [ i ] , j ) == x ) { counter ++ ; break ; } } } if ( counter == n ) cout << " YES " << " STRNEWLINE " ; else cout << " NO " << " STRNEWLINE " ; } int main ( ) { int x = 16 ; vector < string > str = { "10000" , "20" , "16" } ; checkCorrespond ( str , x ) ; return 0 ; }
Count of same length Strings that exists lexicographically in between two given Strings | C ++ program to find the count of same length Strings that exists lexicographically in between two given Strings ; Function to find the count of strings less than given string lexicographically ; Find length of string s ; Looping over the string characters and finding strings less than that character ; Function to find the count of same length Strings that exists lexicographically in between two given Strings ; Count string less than S1 ; Count string less than S2 ; Total strings between S1 and S2 would be difference between the counts - 1 ; If S1 is lexicographically greater than S2 then return 0 , otherwise return the value of totalString ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LexicoLesserStrings ( string s ) { int count = 0 ; int len ; len = s . size ( ) ; for ( int i = 0 ; i < len ; i ++ ) { count += ( s [ i ] - ' a ' ) * pow ( 26 , len - i - 1 ) ; } return count ; } int countString ( string S1 , string S2 ) { int countS1 , countS2 , totalString ; countS1 = LexicoLesserStrings ( S1 ) ; countS2 = LexicoLesserStrings ( S2 ) ; totalString = countS2 - countS1 - 1 ; return ( totalString < 0 ? 0 : totalString ) ; } int main ( ) { string S1 , S2 ; S1 = " cda " ; S2 = " cef " ; cout << countString ( S1 , S2 ) ; return 0 ; }
Longest Subsequence of a String containing only vowels | C ++ program to find the longest subsequence containing only vowels ; Function to check whether a character is vowel or not ; Returns true if x is vowel ; Function to find the longest subsequence which contain all vowels ; Length of the string ; Iterating through the string ; Checking if the character is a vowel or not ; If it is a vowel , then add it to the final string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char x ) { x = tolower ( x ) ; return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } string longestVowelSubsequence ( string str ) { string answer = " " ; int n = str . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isVowel ( str [ i ] ) ) { answer += str [ i ] ; } } return answer ; } int main ( ) { string str = " geeksforgeeks " ; cout << longestVowelSubsequence ( str ) << endl ; return 0 ; }
Maximum repeated frequency of characters in a given string | C ++ implementation to find the maximum repeated frequency of characters in the given string ; Function to find the maximum repeated frequency of the characters in the given string ; Hash - Array to store the frequency of characters ; Loop to find the frequency of the characters ; Hash map to store the occurrence of frequencies of characters ; Loop to find the maximum Repeated frequency from hash - map ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxFrequency ( string s ) { int arr [ 26 ] = { 0 } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) arr [ s [ i ] - ' a ' ] ++ ; unordered_map < int , int > hash ; for ( int i = 0 ; i < 26 ; i ++ ) if ( arr [ i ] != 0 ) hash [ arr [ i ] ] ++ ; int max_count = 0 , res = -1 ; for ( auto i : hash ) { if ( max_count < i . second ) { res = i . first ; max_count = i . second ; } } cout << " Frequency ▁ " << res << " ▁ is ▁ repeated ▁ " << max_count << " ▁ times " ; } int main ( ) { string s = " geeksgeeks " ; findMaxFrequency ( s ) ; return 0 ; }
Generate string with Hamming Distance as half of the hamming distance between strings A and B | C ++ implementation of the above approach ; Function to find the required string ; Find the hamming distance between A and B ; If distance is odd , then resultant string is not possible ; Make the resultant string ; To store the final string ; Pick k characters from each string ; Pick K characters from string B ; Pick K characters from string A ; Append the res characters from string to the resultant string ; Print the resultant string ; Driver 's Code ; Function to find the resultant string
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findString ( string A , string B ) { int dist = 0 ; for ( int i = 0 ; A [ i ] ; i ++ ) { if ( A [ i ] != B [ i ] ) { dist ++ ; } } if ( dist & 1 ) { cout << " Not ▁ Possible " << endl ; } else { string res = " " ; int K = dist / 2 ; for ( int i = 0 ; A [ i ] ; i ++ ) { if ( A [ i ] != B [ i ] && K > 0 ) { res . push_back ( B [ i ] ) ; K -- ; } else if ( A [ i ] != B [ i ] ) { res . push_back ( A [ i ] ) ; } else { res . push_back ( A [ i ] ) ; } } cout << res << endl ; } } int main ( ) { string A = "1001010" ; string B = "0101010" ; findString ( A , B ) ; return 0 ; }
Longest suffix such that occurrence of each character is less than N after deleting atmost K characters | C ++ implementation to find longest suffix of the string such that occurrence of each character is less than K ; Function to find the maximum length suffix in the string ; Length of the string ; Map to store the number of occurrence of character ; Loop to iterate string from the last character ; Condition to check if the occurrence of each character is less than given number ; Condition when character cannot be deleted ; Longest suffix ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSuffix ( string s , int n , int k ) { int i = s . length ( ) - 1 ; int arr [ 26 ] = { 0 } ; string suffix = " " ; while ( i > -1 ) { int index = s [ i ] - ' a ' ; if ( arr [ index ] < n ) { arr [ index ] ++ ; suffix += s [ i ] ; i -- ; continue ; } if ( k == 0 ) break ; k -- ; i -- ; } reverse ( suffix . begin ( ) , suffix . end ( ) ) ; cout << suffix ; } int main ( ) { string str = " iahagafedcba " ; int n = 1 , k = 2 ; maximumSuffix ( str , n , k ) ; return 0 ; }
XOR of two Binary Strings | C ++ Implementation to find the XOR of the two Binary Strings ; Function to find the XOR of the two Binary Strings ; Loop to iterate over the Binary Strings ; If the Character matches ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string xoring ( string a , string b , int n ) { string ans = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == b [ i ] ) ans += "0" ; else ans += "1" ; } return ans ; } int main ( ) { string a = "1010" ; string b = "1101" ; int n = a . length ( ) ; string c = xoring ( a , b , n ) ; cout << c << endl ; }
Find Kth largest string from the permutations of the string with two characters | C ++ implementation of above approach ; Function to print the kth largest string ; loop to iterate through series ; total takes the position of second y ; i takes the position of first y ; calculating first y position ; calculating second y position from first y ; print all x before first y ; print first y ; print all x between first y and second y ; print second y ; print x which occur after second y ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kthString ( int n , int k ) { int total = 0 ; int i = 1 ; while ( total < k ) { total = total + n - i ; i ++ ; } int first_y_position = i - 1 ; int second_y_position = k - ( total - n + first_y_position ) ; for ( int j = 1 ; j < first_y_position ; j ++ ) cout << " x " ; cout << " y " ; int j = first_y_position + 1 ; while ( second_y_position > 1 ) { cout << " x " ; second_y_position -- ; j ++ ; } cout << " y " ; while ( j < n ) { cout << " x " ; j ++ ; } } int main ( ) { int n = 5 ; int k = 7 ; kthString ( n , k ) ; return 0 ; }
Largest and Smallest N | C ++ program to find the largest and smallest N - digit numbers in Octal Number System ; Function to return the largest N - digit number in Octal Number System ; Append '7' N times ; Function to return the smallest N - digit number in Octal Number System ; Append '0' ( N - 1 ) times to 1 ; Function to print the largest and smallest N - digit Octal number ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findLargest ( int N ) { string largest = string ( N , '7' ) ; return largest ; } string findSmallest ( int N ) { string smallest = "1" + string ( ( N - 1 ) , '0' ) ; return smallest ; } void printLargestSmallest ( int N ) { cout << " Largest : ▁ " << findLargest ( N ) << endl ; cout << " Smallest : ▁ " << findSmallest ( N ) << endl ; } int main ( ) { int N = 4 ; printLargestSmallest ( N ) ; return 0 ; }
Count of substrings whose Decimal equivalent is greater than or equal to K | C ++ implementation to count the substrings whose decimal equivalent is greater than or equal to K ; Function to count number of substring whose decimal equivalent is greater than or equal to K ; Left pointer of the substring ; Right pointer of the substring ; Loop to maintain the last occurrence of the 1 in the string ; Variable to count the substring ; Loop to maintain the every possible end index of the substring ; Loop to find the substring whose decimal equivalent is greater than or equal to K ; Condition to check no of bits is out of bound ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long long countSubstr ( string & s , int k ) { int n = s . length ( ) ; int l = n - 1 ; int r = n - 1 ; int arr [ n ] ; int last_indexof1 = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { arr [ i ] = i ; last_indexof1 = i ; } else { arr [ i ] = last_indexof1 ; } } unsigned long long no_of_substr = 0 ; for ( r = n - 1 ; r >= 0 ; r -- ) { l = r ; while ( l >= 0 && ( r - l + 1 ) <= 64 && stoull ( s . substr ( l , r - l + 1 ) , 0 , 2 ) < k ) { l -- ; } if ( r - l + 1 <= 64 ) no_of_substr += l + 1 ; else { no_of_substr += arr [ l + 1 ] + 1 ; } } return no_of_substr ; } int main ( ) { string s = "11100" ; unsigned long long int k = 3 ; cout << countSubstr ( s , k ) ; }
Perfect Cube String | C ++ program to find if string is a perfect cube or not . ; Finding ASCII values of each character and finding its sum ; Find the cube root of sum ; Check if sum is a perfect cube ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectCubeString ( string str ) { int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) sum += ( int ) str [ i ] ; long double cr = round ( cbrt ( sum ) ) ; return ( cr * cr * cr == sum ) ; } int main ( ) { string str = " ll " ; if ( isPerfectCubeString ( str ) ) cout << " Yes " ; else cout << " No " ; }
Program to find the XOR of ASCII values of characters in a string | C ++ program to find XOR of ASCII value of characters in string ; Function to find the XOR of ASCII value of characters in string ; store value of first character ; Traverse string to find the XOR ; Return the XOR ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int XorAscii ( string str , int len ) { int ans = int ( str [ 0 ] ) ; for ( int i = 1 ; i < len ; i ++ ) { ans = ( ans ^ ( int ( str [ i ] ) ) ) ; } return ans ; } int main ( ) { string str = " geeksforgeeks " ; int len = str . length ( ) ; cout << XorAscii ( str , len ) << endl ; str = " GfG " ; len = str . length ( ) ; cout << XorAscii ( str , len ) ; return 0 ; }
CamelCase Pattern Matching | C ++ to find CamelCase Pattern matching ; Function that prints the camel case pattern matching ; Map to store the hashing of each words with every uppercase letter found ; Traverse the words array that contains all the string ; Initialise str as empty ; length of string words [ i ] ; For every uppercase letter found map that uppercase to original words ; Traverse the map for pattern matching ; If pattern matches then print the corresponding mapped words ; If word not found print " No ▁ match ▁ found " ; Driver 's Code ; Pattern to be found ; Function call to find the words that match to the given pattern
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void CamelCase ( vector < string > & words , string pattern ) { map < string , vector < string > > map ; for ( int i = 0 ; i < words . size ( ) ; i ++ ) { string str = " " ; int l = words [ i ] . length ( ) ; for ( int j = 0 ; j < l ; j ++ ) { if ( words [ i ] [ j ] >= ' A ' && words [ i ] [ j ] <= ' Z ' ) { str += words [ i ] [ j ] ; map [ str ] . push_back ( words [ i ] ) ; } } } bool wordFound = false ; for ( auto & it : map ) { if ( it . first == pattern ) { wordFound = true ; for ( auto & itt : it . second ) { cout << itt << endl ; } } } if ( ! wordFound ) { cout << " No ▁ match ▁ found " ; } } int main ( ) { vector < string > words = { " Hi " , " Hello " , " HelloWorld " , " HiTech " , " HiGeek " , " HiTechWorld " , " HiTechCity " , " HiTechLab " } ; string pattern = " HT " ; CamelCase ( words , pattern ) ; return 0 ; }
Encryption and Decryption of String according to given technique | C ++ implementation for Custom Encryption and Decryption of String ; Function to encrypt the string ; Matrix to generate the Encrypted String ; Fill the matrix row - wise ; Loop to generate encrypted string ; Function to decrypt the string ; Matrix to generate the Encrypted String ; Fill the matrix column - wise ; Loop to generate decrypted string ; Driver Code ; Encryption of String ; Decryption of String
#include <bits/stdc++.h> NEW_LINE using namespace std ; string encryption ( string s ) { int l = s . length ( ) ; int b = ceil ( sqrt ( l ) ) ; int a = floor ( sqrt ( l ) ) ; string encrypted ; if ( b * a < l ) { if ( min ( b , a ) == b ) { b = b + 1 ; } else { a = a + 1 ; } } char arr [ a ] [ b ] ; memset ( arr , ' ▁ ' , sizeof ( arr ) ) ; int k = 0 ; for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { encrypted = encrypted + arr [ i ] [ j ] ; } } return encrypted ; } string decryption ( string s ) { int l = s . length ( ) ; int b = ceil ( sqrt ( l ) ) ; int a = floor ( sqrt ( l ) ) ; string decrypted ; char arr [ a ] [ b ] ; memset ( arr , ' ▁ ' , sizeof ( arr ) ) ; int k = 0 ; for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { decrypted = decrypted + arr [ i ] [ j ] ; } } return decrypted ; } int main ( ) { string s = " Geeks ▁ For ▁ Geeks " ; string encrypted ; string decrypted ; encrypted = encryption ( s ) ; cout << encrypted << endl ; decrypted = decryption ( encrypted ) ; cout << decrypted ; return 0 ; }
Program to accept String starting with Capital letter | C ++ program to accept String starting with Capital letter ; Function to check if first character is Capital ; Function to check ; Driver function
#include <iostream> NEW_LINE using namespace std ; int checkIfStartsWithCapital ( string str ) { if ( str [ 0 ] >= ' A ' && str [ 0 ] <= ' Z ' ) return 1 ; else return 0 ; } void check ( string str ) { if ( checkIfStartsWithCapital ( str ) ) cout << " Accepted STRNEWLINE " ; else cout << " Not ▁ Accepted STRNEWLINE " ; } int main ( ) { string str = " GeeksforGeeks " ; check ( str ) ; str = " geeksforgeeks " ; check ( str ) ; return 0 ; }
Program to accept a Strings which contains all the Vowels | C ++ implementation to check that a string contains all vowels ; Function to to check that a string contains all vowels ; Hash Array of size 5 such that the index 0 , 1 , 2 , 3 and 4 represent the vowels a , e , i , o and u ; Loop the string to mark the vowels which are present ; Loop to check if there is any vowel which is not present in the string ; Function to to check that a string contains all vowels ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int checkIfAllVowels ( string str ) { int hash [ 5 ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' A ' str [ i ] == ' a ' ) hash [ 0 ] = 1 ; else if ( str [ i ] == ' E ' str [ i ] == ' e ' ) hash [ 1 ] = 1 ; else if ( str [ i ] == ' I ' str [ i ] == ' i ' ) hash [ 2 ] = 1 ; else if ( str [ i ] == ' O ' str [ i ] == ' o ' ) hash [ 3 ] = 1 ; else if ( str [ i ] == ' U ' str [ i ] == ' u ' ) hash [ 4 ] = 1 ; } for ( int i = 0 ; i < 5 ; i ++ ) { if ( hash [ i ] == 0 ) { return 1 ; } } return 0 ; } int checkIfAllVowelsArePresent ( string str ) { if ( checkIfAllVowels ( str ) ) cout << " Not ▁ Accepted STRNEWLINE " ; else cout << " Accepted STRNEWLINE " ; } int main ( ) { string str = " aeioubc " ; checkIfAllVowelsArePresent ( str ) ; return 0 ; }
Smallest odd number with even sum of digits from the given number N | C ++ program to find the smallest odd number with even sum of digits from the given number N ; Function to find the smallest odd number whose sum of digits is even from the given string ; Converting the given string to a list of digits ; An empty array to store the digits ; For loop to iterate through each digit ; If the given digit is odd then the digit is appended to the array b ; Sorting the list of digits ; If the size of the list is greater than 1 then a 2 digit smallest odd number is returned Since the sum of two odd digits is always even ; Else , - 1 is returned ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallest ( string s ) { vector < int > a ( s . length ( ) ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) a [ i ] = s [ i ] - '0' ; vector < int > b ; for ( int i = 0 ; i < a . size ( ) ; i ++ ) { if ( ( a [ i ] ) % 2 != 0 ) b . push_back ( a [ i ] ) ; } sort ( b . begin ( ) , b . end ( ) ) ; if ( b . size ( ) > 1 ) return ( b [ 0 ] ) * 10 + ( b [ 1 ] ) ; return -1 ; } int main ( ) { cout << ( smallest ( "15470" ) ) ; }
Number formed by deleting digits such that sum of the digits becomes even and the number odd | C ++ implementation to convert a number into odd number such that digit - sum is odd ; Function to convert a number into odd number such that digit - sum is odd ; Loop to find any first two odd number such that their sum is even and number is odd ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void converthenumber ( int n ) { string s = to_string ( n ) ; string res ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '1' s [ i ] == '3' s [ i ] == '5' s [ i ] == '7' s [ i ] == '9' ) res += s [ i ] ; if ( res . size ( ) == 2 ) break ; } if ( res . size ( ) == 2 ) cout << res << endl ; else cout << " - 1" << endl ; } int main ( ) { int n = 18720 ; converthenumber ( n ) ; return 0 ; }
Longest palindromic String formed using concatenation of given strings in any order | C ++ implementation to find the longest palindromic String formed using concatenation of given strings in any order ; Function to find the longest palindromic from given array of strings ; Loop to find the pair of strings which are reverse of each other ; Loop to find if any palindromic string is still left in the array ; Update the answer with all strings of pair1 ; Update the answer with palindromic string s1 ; Update the answer with all strings of pair2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestPalindrome ( string a [ ] , int n ) { string pair1 [ n ] ; string pair2 [ n ] ; int r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { string s = a [ i ] ; reverse ( s . begin ( ) , s . end ( ) ) ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] != " " && a [ j ] != " " ) { if ( s == a [ j ] ) { pair1 [ r ] = a [ i ] ; pair2 [ r ++ ] = a [ j ] ; a [ i ] = " " ; a [ j ] = " " ; break ; } } } } string s1 = " " ; for ( int i = 0 ; i < n ; i ++ ) { string s = a [ i ] ; reverse ( a [ i ] . begin ( ) , a [ i ] . end ( ) ) ; if ( a [ i ] != " " ) { if ( a [ i ] == s ) { s1 = a [ i ] ; break ; } } } string ans = " " ; for ( int i = 0 ; i < r ; i ++ ) { ans = ans + pair1 [ i ] ; } if ( s1 != " " ) { ans = ans + s1 ; } for ( int j = r - 1 ; j >= 0 ; j -- ) { ans = ans + pair2 [ j ] ; } cout << ans << endl ; } int main ( ) { string a1 [ 2 ] = { " aba " , " aba " } ; int n1 = sizeof ( a1 ) / sizeof ( a1 [ 0 ] ) ; longestPalindrome ( a1 , n1 ) ; string a2 [ 5 ] = { " abc " , " dba " , " kop " , " abd " , " cba " } ; int n2 = sizeof ( a2 ) / sizeof ( a2 [ 0 ] ) ; longestPalindrome ( a2 , n2 ) ; }
Program to print the given digit in words | C ++ implementation of the above approach ; Function to return the word of the corresponding digit ; Switch block to check for each digit c ; For digit 0 ; For digit 1 ; For digit 2 ; For digit 3 ; For digit 4 ; For digit 5 ; For digit 6 ; For digit 7 ; For digit 8 ; For digit 9 ; Function to iterate through every digit in the given number ; Finding each digit of the number ; Print the digit in words ; Driver code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void printValue ( char digit ) { switch ( digit ) { case '0' : cout << " Zero ▁ " ; break ; case '1' : cout << " One ▁ " ; break ; case '2' : cout << " Two ▁ " ; break ; case '3' : cout << " Three ▁ " ; break ; case '4' : cout << " Four ▁ " ; break ; case '5' : cout << " Five ▁ " ; break ; case '6' : cout << " Six ▁ " ; break ; case '7' : cout << " Seven ▁ " ; break ; case '8' : cout << " Eight ▁ " ; break ; case '9' : cout << " Nine ▁ " ; break ; } } void printWord ( string N ) { int i , length = N . length ( ) ; for ( i = 0 ; i < length ; i ++ ) { printValue ( N [ i ] ) ; } } int main ( ) { string N = "123" ; printWord ( N ) ; return 0 ; }