codeparrot/xlcost-text-to-code huggingface dataset:
Maximum distinct lowercase alphabets between two uppercase | CPP Program to find maximum lowercase alphabets present between two uppercase alphabets ; Function which computes the maximum number of distinct lowercase alphabets between two uppercase alphabets ; Ignoring lowercase characters in the beginning . ; We start from next of first capital letter and traverse through remaining character . ; If character is in uppercase , ; Update maximum count if lowercase character before this is more . ; clear the set ; If character is in lowercase ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLower ( string str ) { int n = str . length ( ) ; int i = 0 ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { i ++ ; break ; } } int maxCount = 0 ; unordered_set < int > s ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { maxCount = max ( maxCount , ( int ) s . size ( ) ) ; s . clear ( ) ; } if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) s . insert ( str [ i ] ) ; } return maxCount ; } int main ( ) { string str = " zACaAbbaazzC " ; cout << maxLower ( str ) ; return 0 ; }
First uppercase letter in a string ( Iterative and Recursive ) | C ++ program to find the first uppercase letter using linear search ; Function to find string which has first character of each word . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char first ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( isupper ( str [ i ] ) ) return str [ i ] ; return 0 ; } int main ( ) { string str = " geeksforGeeKS " ; char res = first ( str ) ; if ( res == 0 ) cout << " No ▁ uppercase ▁ letter " ; else cout << res << " STRNEWLINE " ; return 0 ; }
Count maximum | C ++ implementation for counting maximum length palindromes ; factorial of a number ; function to count maximum length palindromes . ; Count number of occurrence of a charterer in the string ; int k = 0 ; Count of singles int num = 0 ; numerator of result int den = 1 ; denominator of result ; if frequency is even fi = ci / 2 ; if frequency is odd fi = ci - 1 / 2. ; sum of all frequencies ; product of factorial of every frequency ; if all character are unique so there will be no pallindrome , so if num != 0 then only we are finding the factorial ; k are the single elements that can be placed in middle ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ans * i ; return ( ans ) ; } int numberOfPossiblePallindrome ( string str , int n ) { unordered_map < char , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ str [ i ] ] ++ ; int fi ; for ( auto it = mp . begin ( ) ; it != mp . end ( ) ; ++ it ) { if ( it -> second % 2 == 0 ) fi = it -> second / 2 ; else { fi = ( it -> second - 1 ) / 2 ; k ++ ; } num = num + fi ; den = den * fact ( fi ) ; } if ( num != 0 ) num = fact ( num ) ; int ans = num / den ; if ( k != 0 ) { ans = ans * k ; } return ( ans ) ; } int main ( ) { char str [ ] = " ababab " ; int n = strlen ( str ) ; cout << numberOfPossiblePallindrome ( str , n ) ; return 0 ; }
Counting even decimal value substrings in a binary string | C ++ code to generate all possible substring and count even decimal value substring . ; generate all substring in arr [ 0. . n - 1 ] ; store the count ; Pick starting point ; Pick ending point ; substring between current starting and ending points ; increment power of 2 by one ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int evenDecimalValue ( string str , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int decimalValue = 0 ; int powerOf2 = 1 ; for ( int k = i ; k <= j ; k ++ ) { decimalValue += ( ( str [ k ] - '0' ) * powerOf2 ) ; powerOf2 *= 2 ; } if ( decimalValue % 2 == 0 ) result ++ ; } } return result ; } int main ( ) { string str = "10010" ; int n = 5 ; cout << evenDecimalValue ( str , n ) << endl ; return 0 ; }
Create a new string by alternately combining the characters of two halves of the string in reverse | C ++ program for creating a string by alternately combining the characters of two halves in reverse ; Function performing calculations ; Calculating the two halves of string s as first and second . The final string p ; It joins the characters to final string in reverse order ; It joins the characters to final string in reverse order ; Driver code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( string s ) { int l = s . length ( ) ; int x = l / 2 ; int y = l ; string p = " " ; while ( x > 0 && y > l / 2 ) { p += s [ x - 1 ] ; x -- ; p += s [ y - 1 ] ; y -- ; } if ( y > l / 2 ) { p += s [ y - 1 ] ; y -- ; } cout << p ; } int main ( ) { string s = " sunshine " ; solve ( s ) ; return 0 ; }
Make a lexicographically smallest palindrome with minimal changes | CPP code for changing a string into lexicographically smallest pallindromic string ; Function to create a palindrome ; Count the occurrences of every character in the string ; Create a string of characters with odd occurrences ; Change the created string upto middle element and update count to make sure that only one odd character exists . ; decrease the count of character updated ; Create three strings to make first half second half and middle one . ; characters with even occurrences ; fill the first half . ; character with odd occurrence ; fill the first half with half of occurrence except one ; For middle element ; create the second half by reversing the first half . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Palindrome ( string s , int n ) { unordered_map < char , int > cnt ; string R = " " ; for ( int i = 0 ; i < n ; i ++ ) { char a = s [ i ] ; cnt [ a ] ++ ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( cnt [ i ] % 2 != 0 ) R += i ; } int l = R . length ( ) ; int j = 0 ; for ( int i = l - 1 ; i >= l / 2 ; i -- ) { cnt [ R [ i ] ] -- ; R [ i ] = R [ j ] ; cnt [ R [ j ] ] ++ ; j ++ ; } string first , middle , second ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( cnt [ i ] != 0 ) { if ( cnt [ i ] % 2 == 0 ) { int j = 0 ; while ( j < cnt [ i ] / 2 ) { first += i ; j ++ ; } } else { int j = 0 ; while ( j < ( cnt [ i ] - 1 ) / 2 ) { first += i ; j ++ ; } middle += i ; } } } second = first ; reverse ( second . begin ( ) , second . end ( ) ) ; string resultant = first + middle + second ; cout << resultant << endl ; } int main ( ) { string S = " geeksforgeeks " ; int n = S . length ( ) ; Palindrome ( S , n ) ; return 0 ; }
Program for length of a string using recursion | CPP program to calculate length of a string using recursion ; Function to calculate length ; if we reach at the end of the string ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int recLen ( char * str ) { if ( * str == ' \0' ) return 0 ; else return 1 + recLen ( str + 1 ) ; } int main ( ) { char str [ ] = " GeeksforGeeks " ; cout << recLen ( str ) ; return 0 ; }
Count consonants in a string ( Iterative and recursive methods ) | Iterative CPP program to count total number of consonants ; Function to check for consonant ; To handle lower case ; To check is character is Consonant ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isConsonant ( char ch ) { ch = toupper ( ch ) ; return ! ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) && ch >= 65 && ch <= 90 ; } int totalConsonants ( string str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( isConsonant ( str [ i ] ) ) ++ count ; return count ; } int main ( ) { string str = " abc ▁ de " ; cout << totalConsonants ( str ) ; return 0 ; }
Count consonants in a string ( Iterative and recursive methods ) | Recursive CPP program to count total number of consonants ; Function to check for consonant ; To handle lower case ; to count total number of consonants from 0 to n - 1 ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isConsonant ( char ch ) { ch = toupper ( ch ) ; return ! ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) && ch >= 65 && ch <= 90 ; } int totalConsonants ( string str , int n ) { if ( n == 1 ) return isConsonant ( str [ 0 ] ) ; return totalConsonants ( str , n - 1 ) + isConsonant ( str [ n - 1 ] ) ; } int main ( ) { string str = " abc ▁ de " ; cout << totalConsonants ( str , str . length ( ) ) ; return 0 ; }
Check if actual binary representation of a number is palindrome | C ++ implementation to check whether binary representation of a number is palindrome or not ; function to reverse bits of a number ; traversing bits of ' n ' from the right ; bitwise left shift ' rev ' by 1 ; if current bit is '1' ; bitwise right shift ' n ' by 1 ; required number ; function to check whether binary representation of a number is palindrome or not ; get the number by reversing bits in the binary representation of ' n ' ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int reverseBits ( unsigned int n ) { unsigned int rev = 0 ; while ( n > 0 ) { rev <<= 1 ; if ( n & 1 == 1 ) rev ^= 1 ; n >>= 1 ; } return rev ; } bool isPalindrome ( unsigned int n ) { unsigned int rev = reverseBits ( n ) ; return ( n == rev ) ; } int main ( ) { unsigned int n = 9 ; if ( isPalindrome ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Reduce Hamming distance by swapping two characters | C ++ code to decrease hamming distance using swap . ; Function to return the swapped indexes to get minimum hamming distance . ; Find the initial hamming distance ; Case - I : To decrease distance by two ; ASCII values of present character . ; If two same letters appear in different positions print their indexes ; Store the index of letters which is in wrong position ; Case : II ; If misplaced letter is found , print its original index and its new index ; Store the index of letters in wrong position ; Case - III ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  26 NEW_LINE void Swap ( string s , string t , int n ) { int dp [ MAX ] [ MAX ] ; memset ( dp , -1 , sizeof dp ) ; int tot = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] != t [ i ] ) tot ++ ; for ( int i = 0 ; i < n ; i ++ ) { int a = s [ i ] - ' a ' ; int b = t [ i ] - ' a ' ; if ( a == b ) continue ; if ( dp [ a ] [ b ] != -1 ) { cout << i + 1 << " ▁ " << dp [ a ] [ b ] + 1 << endl ; return ; } dp [ b ] [ a ] = i ; } int A [ MAX ] , B [ MAX ] ; memset ( A , -1 , sizeof A ) ; memset ( B , -1 , sizeof B ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s [ i ] - ' a ' ; int b = t [ i ] - ' a ' ; if ( a == b ) continue ; if ( A [ b ] != -1 ) { cout << i + 1 << " ▁ " << A [ b ] + 1 << endl ; return ; } if ( B [ a ] != -1 ) { cout << i + 1 << " ▁ " << B [ a ] + 1 << endl ; return ; } A [ a ] = i ; B [ b ] = i ; } cout << -1 << endl ; } int main ( ) { string S = " permanent " ; string T = " pergament " ; int n = S . length ( ) ; if ( S == " " T == " " ) cout << " Required ▁ string ▁ is ▁ empty . " ; else Swap ( S , T , n ) ; return 0 ; }
Convert string X to an anagram of string Y with minimum replacements | C ++ program to convert string X to string Y which minimum number of changes . ; Function that converts string X into lexicographically smallest anagram of string Y with minimal changes ; Counting frequency of characters in each string . ; We maintain two more counter arrays ctrx [ ] and ctry [ ] Ctrx [ ] maintains the count of extra elements present in string X than string Y Ctry [ ] maintains the count of characters missing from string X which should be present in string Y . ; This means that we cannot edit the current character as it 's frequency  in string X is equal to or less  than the frequency in string Y.  Thus, we go to the next position ; Here , we try to find that character , which has more frequency in string Y and less in string X . We try to find this character in lexicographical order so that we get lexicographically smaller string ; This portion deals with the lexicographical property . Now , we put a character in string X when either this character has smaller value than the character present there right now or if this is the last position for it to exchange , else we fix the character already present here in this position . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  26 NEW_LINE void printAnagramAndChanges ( string X , string Y ) { int countx [ MAX ] = { 0 } , county [ MAX ] = { 0 } , ctrx [ MAX ] = { 0 } , ctry [ MAX ] = { 0 } ; int change = 0 ; int l = X . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { countx [ X [ i ] - ' A ' ] ++ ; county [ Y [ i ] - ' A ' ] ++ ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( countx [ i ] > county [ i ] ) ctrx [ i ] += ( countx [ i ] - county [ i ] ) ; else if ( countx [ i ] < county [ i ] ) ctry [ i ] += ( county [ i ] - countx [ i ] ) ; change += abs ( county [ i ] - countx [ i ] ) ; } for ( int i = 0 ; i < l ; i ++ ) { if ( ctrx [ X [ i ] - ' A ' ] == 0 ) continue ; int j ; for ( j = 0 ; j < MAX ; j ++ ) if ( ( ctry [ j ] ) > 0 ) break ; if ( countx [ X [ i ] - ' A ' ] == ctrx [ X [ i ] - ' A ' ] X [ i ] - ' A ' > j ) { countx [ X [ i ] - ' A ' ] -- ; ctrx [ X [ i ] - ' A ' ] -- ; ctry [ j ] -- ; X [ i ] = ' A ' + j ; } else countx [ X [ i ] - ' A ' ] -- ; } cout << " Anagram ▁ : ▁ " << X << endl ; cout << " Number ▁ of ▁ changes ▁ made ▁ : ▁ " << change / 2 ; } int main ( ) { string x = " CDBABC " , y = " ADCABD " ; printAnagramAndChanges ( x , y ) ; return 0 ; }
Count number of equal pairs in a string | CPP program to count the number of pairs ; Function to count the number of equal pairs ; Hash table ; Traverse the string and count occurrence ; Stores the answer ; Traverse and check the occurrence of every character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  256 NEW_LINE int countPairs ( string s ) { int cnt [ MAX ] = { 0 } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) cnt [ s [ i ] ] ++ ; int ans = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) ans += cnt [ i ] * cnt [ i ] ; return ans ; } int main ( ) { string s = " geeksforgeeks " ; cout << countPairs ( s ) ; return 0 ; }
Find a string in lexicographic order which is in between given two strings | CPP program to find the string in lexicographic order which is in between given two strings ; Function to find the lexicographically next string ; Iterate from last character ; If not ' z ' , increase by one ; if ' z ' , change it to ' a ' ; Driver Code ; If not equal , print the resultant string
#include <bits/stdc++.h> NEW_LINE using namespace std ; string lexNext ( string s , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != ' z ' ) { s [ i ] ++ ; return s ; } s [ i ] = ' a ' ; } } int main ( ) { string S = " abcdeg " , T = " abcfgh " ; int n = S . length ( ) ; string res = lexNext ( S , n ) ; if ( res != T ) cout << res << endl ; else cout << " - 1" << endl ; return 0 ; }
Find the arrangement of queue at given time | CPP program to find the arrangement of queue at time = t ; prints the arrangement at time = t ; Checking the entire queue for every moment from time = 1 to time = t . ; If current index contains ' B ' and next index contains ' G ' then swap ; Driver function for the program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int n , int t , string s ) { for ( int i = 0 ; i < t ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) if ( s [ j ] == ' B ' && s [ j + 1 ] == ' G ' ) { char temp = s [ j ] ; s [ j ] = s [ j + 1 ] ; s [ j + 1 ] = temp ; j ++ ; } cout << s ; } int main ( ) { int n = 6 , t = 2 ; string s = " BBGBBG " ; solve ( n , t , s ) ; return 0 ; }
Add two numbers represented by two arrays | CPP program to sum two numbers represented two arrays . ; Return sum of two number represented by the arrays . Size of a [ ] is greater than b [ ] . It is made sure be the wrapper function ; array to store sum . ; Until we reach beginning of array . we are comparing only for second array because we have already compare the size of array in wrapper function . ; find sum of corresponding element of both arrays . ; Finding carry for next sum . ; If second array size is less the first array size . ; Add carry to first array elements . ; If there is carry on adding 0 index elements . append 1 to total sum . ; Converting array into number . ; Wrapper Function ; Making first array which have greater number of element ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calSumUtil ( int a [ ] , int b [ ] , int n , int m ) { int sum [ n ] ; int i = n - 1 , j = m - 1 , k = n - 1 ; int carry = 0 , s = 0 ; while ( j >= 0 ) { s = a [ i ] + b [ j ] + carry ; sum [ k ] = ( s % 10 ) ; carry = s / 10 ; k -- ; i -- ; j -- ; } while ( i >= 0 ) { s = a [ i ] + carry ; sum [ k ] = ( s % 10 ) ; carry = s / 10 ; i -- ; k -- ; } int ans = 0 ; if ( carry ) ans = 10 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { ans += sum [ i ] ; ans *= 10 ; } return ans / 10 ; } int calSum ( int a [ ] , int b [ ] , int n , int m ) { if ( n >= m ) return calSumUtil ( a , b , n , m ) ; else return calSumUtil ( b , a , m , n ) ; } int main ( ) { int a [ ] = { 9 , 3 , 9 } ; int b [ ] = { 6 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 0 ] ) ; cout << calSum ( a , b , n , m ) << endl ; return 0 ; }
Longest Common Anagram Subsequence | C ++ implementation to find the length of the longest common anagram subsequence ; function to find the length of the longest common anagram subsequence ; hash tables for storing frequencies of each character ; calculate frequency of each character of ' str1 [ ] ' ; calculate frequency of each character of ' str2 [ ] ' ; for each character add its minimum frequency out of the two strings in ' len ' ; required length ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  26 NEW_LINE int longCommomAnagramSubseq ( char str1 [ ] , char str2 [ ] , int n1 , int n2 ) { int freq1 [ SIZE ] , freq2 [ SIZE ] ; memset ( freq1 , 0 , sizeof ( freq1 ) ) ; memset ( freq2 , 0 , sizeof ( freq2 ) ) ; int len = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) freq1 [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < SIZE ; i ++ ) len += min ( freq1 [ i ] , freq2 [ i ] ) ; return len ; } int main ( ) { char str1 [ ] = " abdacp " ; char str2 [ ] = " ckamb " ; int n1 = strlen ( str1 ) ; int n2 = strlen ( str2 ) ; cout << " Length ▁ = ▁ " << longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) ; return 0 ; }
Panalphabetic window in a string | CPP Program to check whether given string contain panalphabetic window or not ; Return if given string contain panalphabetic window . ; traversing the string ; if character of string is equal to ch , increment ch . ; if all characters are found , return true . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPanalphabeticWindow ( char s [ ] , int n ) { char ch = ' a ' ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ch ) ch ++ ; if ( ch == ' z ' + 1 ) return true ; } return false ; } int main ( ) { char s [ ] = " abujm ▁ zvcd ▁ acefc ▁ deghf ▁ gijkle " " ▁ m ▁ n ▁ o ▁ p ▁ pafqrstuvwxyzfap " ; int n = strlen ( s ) ; ( isPanalphabeticWindow ( s , n ) ) ? ( cout << " YES " ) : ( cout << " NO " ) ; return 0 ; }
Program to print characters present at prime indexes in a given string | C ++ Program to print Characters at Prime index in a given String ; Corner case ; Check from 2 to n - 1 ; Function to print character at prime index ; Loop to check if index prime or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } void prime_index ( string input ) { int n = input . length ( ) ; for ( int i = 2 ; i <= n ; i ++ ) if ( isPrime ( i ) ) cout << input [ i - 1 ] ; } int main ( ) { string input = " GeeksforGeeks " ; prime_index ( input ) ; return 0 ; }
Check whether a given string is Heterogram or not | C ++ Program to check whether the given string is Heterogram or not . ; traversing the string . ; ignore the space ; if already encountered ; else return false . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isHeterogram ( char s [ ] , int n ) { int hash [ 26 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] != ' ▁ ' ) { if ( hash [ s [ i ] - ' a ' ] == 0 ) hash [ s [ i ] - ' a ' ] = 1 ; else return false ; } } return true ; } int main ( ) { char s [ ] = " the ▁ big ▁ dwarf ▁ only ▁ jumps " ; int n = strlen ( s ) ; ( isHeterogram ( s , n ) ) ? ( cout << " YES " ) : ( cout << " NO " ) ; return 0 ; }
Print given sentence into its equivalent ASCII form | C ++ implementation for converting a string into it 's ASCII equivalent sentence ; Function to compute the ASCII value of each character one by one ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ASCIISentence ( std :: string str ) { int l = str . length ( ) ; int convert ; for ( int i = 0 ; i < l ; i ++ ) { convert = str [ i ] - NULL ; cout << convert ; } } int main ( ) { string str = " GeeksforGeeks " ; cout << " ASCII ▁ Sentence : " << std :: endl ; ASCIISentence ( str ) ; return 0 ; }
Snake case of a given sentence | CPP program to convert given sentence / to snake case ; Function to replace spaces and convert into snake case ; Converting space to underscor ; If not space , convert into lower character ; Driver program ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void convert ( string str ) { int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . at ( i ) == ' ▁ ' ) str . at ( i ) = ' _ ' ; else str . at ( i ) = tolower ( str . at ( i ) ) ; } cout << str ; } int main ( ) { string str = " I ▁ got ▁ intern ▁ at ▁ geeksforgeeks " ; convert ( str ) ; return 0 ; }
Find the size of largest subset of anagram words | C ++ Program to find the size of largest subset of anagram ; Utility function to find size of largest subset of anagram ; sort the string ; Increment the count of string ; Compute the maximum size of string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestAnagramSet ( string arr [ ] , int n ) { int maxSize = 0 ; unordered_map < string , int > count ; for ( int i = 0 ; i < n ; ++ i ) { sort ( arr [ i ] . begin ( ) , arr [ i ] . end ( ) ) ; count [ arr [ i ] ] += 1 ; maxSize = max ( maxSize , count [ arr [ i ] ] ) ; } return maxSize ; } int main ( ) { string arr [ ] = { " ant " , " magenta " , " magnate " , " tan " , " gnamate " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << largestAnagramSet ( arr , n ) << " STRNEWLINE " ; string arr1 [ ] = { " cars " , " bikes " , " arcs " , " steer " } ; n = sizeof ( arr1 ) / sizeof ( arr [ 0 ] ) ; cout << largestAnagramSet ( arr1 , n ) ; return 0 ; }
Program to count vowels , consonant , digits and special characters in string . | Program to count vowels , consonant , digits and special character in a given string . ; Function to count number of vowels , consonant , digitsand special character in a string . ; Declare the variable vowels , consonant , digit and special characters ; str . length ( ) function to count number of character in given string . ; To handle upper case letters ; Driver function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countCharacterType ( string str ) { int vowels = 0 , consonant = 0 , specialChar = 0 , digit = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str [ i ] ; if ( ( ch >= ' a ' && ch <= ' z ' ) || ( ch >= ' A ' && ch <= ' Z ' ) ) { ch = tolower ( ch ) ; if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) vowels ++ ; else consonant ++ ; } else if ( ch >= '0' && ch <= '9' ) digit ++ ; else specialChar ++ ; } cout << " Vowels : ▁ " << vowels << endl ; cout << " Consonant : ▁ " << consonant << endl ; cout << " Digit : ▁ " << digit << endl ; cout << " Special ▁ Character : ▁ " << specialChar << endl ; } int main ( ) { string str = " geeks ▁ for ▁ geeks121" ; countCharacterType ( str ) ; return 0 ; }