codeparrot/xlcost-text-to-code huggingface dataset:
Print the longest common substring | C ++ implementation to print the longest common substring ; function to find and print the longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Note that LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; To store the index of the cell which contains the maximum value . This cell 's index helps in building  up the longest common substring from right to left. ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion . ; if true , then no common substring exists ; allocate space for the longest common substring ; traverse up diagonally form the ( row , col ) cell until LCSuff [ row ] [ col ] != 0 ; move diagonally up to previous cell ; required longest common substring ; Driver program to test above function
#include <iostream> NEW_LINE #include <stdlib.h> NEW_LINE #include <string.h> NEW_LINE using namespace std ; void printLCSubStr ( char * X , char * Y , int m , int n ) { int LCSuff [ m + 1 ] [ n + 1 ] ; int len = 0 ; int row , col ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) LCSuff [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) { LCSuff [ i ] [ j ] = LCSuff [ i - 1 ] [ j - 1 ] + 1 ; if ( len < LCSuff [ i ] [ j ] ) { len = LCSuff [ i ] [ j ] ; row = i ; col = j ; } } else LCSuff [ i ] [ j ] = 0 ; } } if ( len == 0 ) { cout << " No ▁ Common ▁ Substring " ; return ; } char * resultStr = ( char * ) malloc ( ( len + 1 ) * sizeof ( char ) ) ; while ( LCSuff [ row ] [ col ] != 0 ) { row -- ; col -- ; } cout << resultStr ; } int main ( ) { char X [ ] = " OldSite : GeeksforGeeks . org " ; char Y [ ] = " NewSite : GeeksQuiz . com " ; int m = strlen ( X ) ; int n = strlen ( Y ) ; printLCSubStr ( X , Y , m , n ) ; return 0 ; }
Find the first repeated word in a string | CPP program for finding first repeated word in a string ; returns first repeated word ; break string into tokens and then each string into set if a word appeared before appears again , return the word and break ; hashmap for storing word and its count in sentence ; store all the words of string and the count of word in hashmap ; setOfWords [ token ] += 1 ; word exists ; insert new word to set ; either take a new stream or store the words in vector of strings in previous loop ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findFirstRepeated ( string s ) { istringstream iss ( s ) ; string token ; unordered_map < string , int > setOfWords ; while ( getline ( iss , token , ' ▁ ' ) ) { if ( setOfWords . find ( token ) != setOfWords . end ( ) ) else setOfWords . insert ( make_pair ( token , 1 ) ) ; } istringstream iss2 ( s ) ; while ( getline ( iss2 , token , ' ▁ ' ) ) { int count = setOfWords [ token ] ; if ( count > 1 ) { return token ; } } return " NoRepetition " ; } int main ( ) { string s ( " Ravi ▁ had ▁ been ▁ saying ▁ that ▁ he ▁ had ▁ been ▁ there " ) ; string firstWord = findFirstRepeated ( s ) ; if ( firstWord != " NoRepetition " ) cout << " First ▁ repeated ▁ word ▁ : : ▁ " << firstWord << endl ; else cout << " No ▁ Repetitionn " ; return 0 ; }
Find the first repeated word in a string | CPP program for finding first repeated word in a string ; returns first repeated word ; break string into tokens and then each string into set if a word appeared before appears again , return the word and break ; hashset for storing word and its count in sentence ; store all the words of string and the count of word in hashset ; if word exists return ; insert new word to set ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findFirstRepeated ( string s ) { istringstream iss ( s ) ; string token ; set < string > setOfWords ; while ( getline ( iss , token , ' ▁ ' ) ) { if ( setOfWords . find ( token ) != setOfWords . end ( ) ) { return token ; } setOfWords . insert ( token ) ; } return " NoRepetition " ; } int main ( ) { string s ( " Ravi ▁ had ▁ been ▁ saying ▁ that ▁ he ▁ had ▁ been ▁ there " ) ; string firstWord = findFirstRepeated ( s ) ; if ( firstWord != " NoRepetition " ) cout << " First ▁ repeated ▁ word ▁ : : ▁ " << firstWord << endl ; else cout << " No ▁ Repetitionn " ; return 0 ; }
Convert all substrings of length ' k ' from base ' b ' to decimal | Simple C ++ program to convert all substrings from decimal to given base . ; Saving substring in sub ; Evaluating decimal for current substring and printing it . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int substringConversions ( string str , int k , int b ) { for ( int i = 0 ; i + k <= str . size ( ) ; i ++ ) { string sub = str . substr ( i , k ) ; int sum = 0 , counter = 0 ; for ( int i = sub . size ( ) - 1 ; i >= 0 ; i -- ) { sum = sum + ( ( sub . at ( i ) - '0' ) * pow ( b , counter ) ) ; counter ++ ; } cout << sum << " ▁ " ; } } int main ( ) { string str = "12212" ; int b = 3 , k = 3 ; substringConversions ( str , b , k ) ; return 0 ; }
Find numbers of balancing positions in string | C ++ program to find number of balancing points in string ; function to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 256 ; int countBalance ( char * str ) { int leftVisited [ MAX_CHAR ] = { 0 } ; int rightVisited [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) rightVisited [ str [ i ] ] ++ ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { leftVisited [ str [ i ] ] ++ ; rightVisited [ str [ i ] ] -- ; int j ; for ( j = 0 ; j < MAX_CHAR ; j ++ ) { if ( ( leftVisited [ j ] == 0 && rightVisited [ j ] != 0 ) || ( leftVisited [ j ] != 0 && rightVisited [ j ] == 0 ) ) break ; } if ( j == MAX_CHAR ) res ++ ; } return res ; } int main ( ) { char str [ ] = " abaababa " ; cout << countBalance ( str ) ; return 0 ; }
Min flips of continuous characters to make all characters same in a string | CPP program to find min flips in binary string to make all characters equal ; To find min number of flips in binary string ; If last character is not equal to str [ i ] increase res ; To return min flips ; Driver program to check findFlips ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findFlips ( char str [ ] , int n ) { char last = ' ▁ ' ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( last != str [ i ] ) res ++ ; last = str [ i ] ; } return res / 2 ; } int main ( ) { char str [ ] = "00011110001110" ; int n = strlen ( str ) ; cout << findFlips ( str , n ) ; return 0 ; }
Maximum length substring having all same characters after k changes | C ++ program to find maximum length equal character string with k changes ; function to find the maximum length of substring having character ch ; traverse the whole string ; if character is not same as ch increase count ; While count > k traverse the string again until count becomes less than k and decrease the count when characters are not same ; length of substring will be rightIndex - leftIndex + 1. Compare this with the maximum length and return maximum length ; function which returns maximum length of substring ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findLen ( string & A , int n , int k , char ch ) { int maxlen = 1 ; int cnt = 0 ; int l = 0 , r = 0 ; while ( r < n ) { if ( A [ r ] != ch ) ++ cnt ; while ( cnt > k ) { if ( A [ l ] != ch ) -- cnt ; ++ l ; } maxlen = max ( maxlen , r - l + 1 ) ; ++ r ; } return maxlen ; } int answer ( string & A , int n , int k ) { int maxlen = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) { maxlen = max ( maxlen , findLen ( A , n , k , i + ' A ' ) ) ; maxlen = max ( maxlen , findLen ( A , n , k , i + ' a ' ) ) ; } return maxlen ; } int main ( ) { int n = 5 , k = 2 ; string A = " ABABA " ; cout << " Maximum ▁ length ▁ = ▁ " << answer ( A , n , k ) << endl ; n = 6 , k = 4 ; string B = " HHHHHH " ; cout << " Maximum ▁ length ▁ = ▁ " << answer ( B , n , k ) << endl ; return 0 ; }
Given a sequence of words , print all anagrams together using STL | C ++ program for finding all anagram pairs in the given array ; Utility function for printing anagram list ; Utility function for storing the vector of strings into HashMap ; sort the string ; make hash of a sorted string ; print utility function for printing all the anagrams ; Driver code ; initialize vector of strings ; utility function for storing strings into hashmap
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; void printAnagram ( unordered_map < string , vector < string > > & store ) { for ( auto it : store ) { vector < string > temp_vec ( it . second ) ; int size = temp_vec . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) cout << temp_vec [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } } void storeInMap ( vector < string > & vec ) { unordered_map < string , vector < string > > store ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { string tempString ( vec [ i ] ) ; sort ( tempString . begin ( ) , tempString . end ( ) ) ; store [ tempString ] . push_back ( vec [ i ] ) ; } printAnagram ( store ) ; } int main ( ) { vector < string > arr ; arr . push_back ( " geeksquiz " ) ; arr . push_back ( " geeksforgeeks " ) ; arr . push_back ( " abcd " ) ; arr . push_back ( " forgeeksgeeks " ) ; arr . push_back ( " zuiqkeegs " ) ; arr . push_back ( " cat " ) ; arr . push_back ( " act " ) ; arr . push_back ( " tca " ) ; storeInMap ( arr ) ; return 0 ; }
Quick way to check if all the characters of a string are same | C ++ program for above approach ; Function to check is all the characters in string are or not ; Insert characters in the set ; If all characters are same Size of set will always be 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void allCharactersSame ( string s ) { set < char > s1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) s1 . insert ( s [ i ] ) ; if ( s1 . size ( ) == 1 ) cout << " YES " ; else cout << " NO " ; } int main ( ) { string str = " nnnn " ; allCharactersSame ( str ) ; return 0 ; }
Find the character in first string that is present at minimum index in second string | C ++ implementation to find the character in first string that is present at minimum index in second string ; function to find the minimum index character ; to store the index of character having minimum index ; lengths of the two strings ; traverse ' patt ' ; for each character of ' patt ' traverse ' str ' ; if patt [ i ] is found in ' str ' , check if it has the minimum index or not . If yes , then update ' minIndex ' and break ; print the minimum index character ; if no character of ' patt ' is present in ' str ' ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMinIndexChar ( string str , string patt ) { int minIndex = INT_MAX ; int m = str . size ( ) ; int n = patt . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( patt [ i ] == str [ j ] && j < minIndex ) { minIndex = j ; break ; } } } if ( minIndex != INT_MAX ) cout << " Minimum ▁ Index ▁ Character ▁ = ▁ " << str [ minIndex ] ; else cout << " No ▁ character ▁ present " ; } int main ( ) { string str = " geeksforgeeks " ; string patt = " set " ; printMinIndexChar ( str , patt ) ; return 0 ; }
Find the character in first string that is present at minimum index in second string | C ++ implementation to find the character in first string that is present at minimum index in second string ; function to find the minimum index character ; unordered_map ' um ' implemented as hash table ; to store the index of character having minimum index ; lengths of the two strings ; store the first index of each character of ' str ' ; traverse the string ' patt ' ; if patt [ i ] is found in ' um ' , check if it has the minimum index or not accordingly update ' minIndex ' ; print the minimum index character ; if no character of ' patt ' is present in ' str ' ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMinIndexChar ( string str , string patt ) { unordered_map < char , int > um ; int minIndex = INT_MAX ; int m = str . size ( ) ; int n = patt . size ( ) ; - for ( int i = 0 ; i < m ; i ++ ) if ( um . find ( str [ i ] ) == um . end ( ) ) um [ str [ i ] ] = i ; for ( int i = 0 ; i < n ; i ++ ) if ( um . find ( patt [ i ] ) != um . end ( ) && um [ patt [ i ] ] < minIndex ) minIndex = um [ patt [ i ] ] ; if ( minIndex != INT_MAX ) cout << " Minimum ▁ Index ▁ Character ▁ = ▁ " << str [ minIndex ] ; else cout << " No ▁ character ▁ present " ; } int main ( ) { string str = " geeksforgeeks " ; string patt = " set " ; printMinIndexChar ( str , patt ) ; return 0 ; }
Check if both halves of the string have same set of characters | C ++ program to check if it is possible to split string or not ; function to check if we can split string or not ; Counter array initialized with 0 ; Length of the string ; traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; Driver program to test above function ; String to be checked
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; bool checkCorrectOrNot ( string s ) { int count1 [ MAX_CHAR ] = { 0 } ; int count2 [ MAX_CHAR ] = { 0 } ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count1 [ s [ i ] - ' a ' ] ++ ; count2 [ s [ j ] - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } int main ( ) { string s = " abab " ; if ( checkCorrectOrNot ( s ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Given a number as a string , find the number of contiguous subsequences which recursively add up to 9 | Set 2 | C ++ program to count substrings with recursive sum equal to 9 ; to store no . of previous encountered modular sums ; no . of modular sum ( == 0 ) encountered till now = 1 ; if number is 0 increase ; no . of continuous_zero by 1 ; else continuous_zero is 0 ; increase d value of this mod_sum ; subtract no . of cases where there are only zeroes in substring ; driver program to test above function
#include <iostream> NEW_LINE #include <cstring> NEW_LINE using namespace std ; int count9s ( char number [ ] ) { int n = strlen ( number ) ; int d [ 9 ] ; memset ( d , 0 , sizeof ( d ) ) ; d [ 0 ] = 1 ; int result = 0 ; int mod_sum = 0 , continuous_zero = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! int ( number [ i ] - '0' ) ) continuous_zero ++ ; else continuous_zero = 0 ; mod_sum += int ( number [ i ] - '0' ) ; mod_sum %= 9 ; result += d [ mod_sum ] ; d [ mod_sum ] ++ ; result -= continuous_zero ; } return result ; } int main ( ) { cout << count9s ( "01809" ) << endl ; cout << count9s ( "1809" ) << endl ; cout << count9s ( "4189" ) ; return 0 ; }
Extract maximum numeric value from a given string | Set 1 ( General approach ) | C ++ program for above implementation ; Utility function to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Function to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Drivers program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string maximumNum ( string curr_num , string res ) { int len1 = curr_num . length ( ) ; int len2 = res . length ( ) ; if ( len1 == len2 ) { int i = 0 ; while ( curr_num [ i ] == res [ i ] ) i ++ ; if ( curr_num [ i ] < res [ i ] ) return res ; else return curr_num ; } return len1 < len2 ? res : curr_num ; } string extractMaximum ( string str ) { int n = str . length ( ) ; string curr_num = " " ; string res ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n && str [ i ] == '0' ) i ++ ; while ( i < n && str [ i ] >= '0' && str [ i ] <= '9' ) { curr_num = curr_num + str [ i ] ; i ++ ; } if ( i == n ) break ; if ( curr_num . size ( ) > 0 ) i -- ; res = maximumNum ( curr_num , res ) ; curr_num = " " ; } if ( curr_num . size ( ) == 0 && res . size ( ) == 0 ) res = res + '0' ; return maximumNum ( curr_num , res ) ; } int main ( ) { string str = "100klh564abc365bg " ; cout << extractMaximum ( str ) << endl ; return 0 ; }
To check divisibility of any large number by 999 | CPP for divisibility of number by 999 ; function to check divisibility ; Append required 0 s at the beginning . ; add digits in group of three in gSum ; group saves 3 - digit group ; calculate result till 3 digit sum ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisible999 ( string num ) { int n = num . length ( ) ; if ( n == 0 && num [ 0 ] == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; int gSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int group = 0 ; group += ( num [ i ++ ] - '0' ) * 100 ; group += ( num [ i ++ ] - '0' ) * 10 ; group += num [ i ] - '0' ; gSum += group ; } if ( gSum > 1000 ) { num = to_string ( gSum ) ; n = num . length ( ) ; gSum = isDivisible999 ( num ) ; } return ( gSum == 999 ) ; } int main ( ) { string num = "1998" ; int n = num . length ( ) ; if ( isDivisible999 ( num ) ) cout << " Divisible " ; else cout << " Not ▁ divisible " ; return 0 ; }
Rearrange a string in sorted order followed by the integer sum | C ++ program for above implementation ; Function to return string in lexicographic order followed by integers sum ; Traverse the string ; Count occurrence of uppercase alphabets ; Store sum of integers ; Traverse for all characters A to Z ; Append the current character in the string no . of times it occurs in the given string ; Append the sum of integers ; return resultant string ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; string arrangeString ( string str ) { int char_count [ MAX_CHAR ] = { 0 } ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) char_count [ str [ i ] - ' A ' ] ++ ; else sum = sum + ( str [ i ] - '0' ) ; } string res = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { char ch = ( char ) ( ' A ' + i ) ; while ( char_count [ i ] -- ) res = res + ch ; } if ( sum > 0 ) res = res + to_string ( sum ) ; return res ; } int main ( ) { string str = " ACCBA10D2EW30" ; cout << arrangeString ( str ) ; return 0 ; }
Program to print all substrings of a given string | C ++ program to print all possible substrings of a given string ; Function to print all sub strings ; Pick starting point ; Pick ending point ; Print characters from current starting point to current ending point . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subString ( char str [ ] , int n ) { for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; for ( int k = i ; k <= j ; k ++ ) cout << str [ k ] ; cout << endl ; } } } int main ( ) { char str [ ] = " abc " ; subString ( str , strlen ( str ) ) ; return 0 ; }
Reverse a string preserving space positions | C ++ program to reverse a string preserving spaces . ; Function to reverse the string and preserve the space position ; Mark spaces in result ; Traverse input string from beginning and put characters in result from end ; Ignore spaces in input string ; ignore spaces in result . ; Driver code
#include <iostream> NEW_LINE using namespace std ; string reverses ( string str ) { int n = str . size ( ) ; string result ( n , ' \0' ) ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == ' ▁ ' ) result [ i ] = ' ▁ ' ; int j = n - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] != ' ▁ ' ) { if ( result [ j ] == ' ▁ ' ) j -- ; result [ j ] = str [ i ] ; j -- ; } } return result ; } int main ( ) { string str = " internship ▁ at ▁ geeks ▁ for ▁ geeks " ; cout << reverses ( str ) << endl ; return 0 ; }
Find uncommon characters of the two strings | C ++ implementation to find the uncommon characters of the two strings ; size of the hash table ; function to find the uncommon characters of the two strings ; mark presence of each character as 0 in the hash table ' present [ ] ' ; for each character of str1 , mark its presence as 1 in ' present [ ] ' ; for each character of str2 ; if a character of str2 is also present in str1 , then mark its presence as - 1 ; else mark its presence as 2 ; print all the uncommon characters ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; void findAndPrintUncommonChars ( string str1 , string str2 ) { int present [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) present [ i ] = 0 ; int l1 = str1 . size ( ) ; int l2 = str2 . size ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) present [ str1 [ i ] - ' a ' ] = 1 ; for ( int i = 0 ; i < l2 ; i ++ ) { if ( present [ str2 [ i ] - ' a ' ] == 1 present [ str2 [ i ] - ' a ' ] == -1 ) present [ str2 [ i ] - ' a ' ] = -1 ; else present [ str2 [ i ] - ' a ' ] = 2 ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( present [ i ] == 1 present [ i ] == 2 ) cout << ( char ( i + ' a ' ) ) << " ▁ " ; } int main ( ) { string str1 = " characters " ; string str2 = " alphabets " ; findAndPrintUncommonChars ( str1 , str2 ) ; return 0 ; }
Length Of Last Word in a String | C ++ program for implementation of simple approach to find length of last word ; String a is ' final ' -- can not be modified So , create a copy and trim the spaces from both sides ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <boost/algorithm/string.hpp> NEW_LINE using namespace std ; int lengthOfLastWord ( string a ) { int len = 0 ; string str ( a ) ; boost :: trim_right ( str ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . at ( i ) == ' ▁ ' ) len = 0 ; else len ++ ; } return len ; } int main ( ) { string input = " Geeks ▁ For ▁ Geeks ▁ " ; cout << " The ▁ length ▁ of ▁ last ▁ word ▁ is ▁ " << lengthOfLastWord ( input ) ; }
Program to count vowels in a string ( Iterative and Recursive ) | C ++ program to count vowels in a string ; Function to check the Vowel ; Returns count of vowels in str ; if ( isVowel ( str [ i ] ) ) Check for vowel ; Main Calling Function ; string object ; Total numbers of Vowel
#include <iostream> NEW_LINE using namespace std ; bool isVowel ( char ch ) { ch = toupper ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) ; } int countVowels ( string str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) ++ count ; return count ; } int main ( ) { string str = " abc ▁ de " ; cout << countVowels ( str ) << endl ; return 0 ; }
Toggle case of a string using Bitwise Operators | C ++ program to get toggle case of a string ; tOGGLE cASE = swaps CAPS to lower case and lower case to CAPS ; Bitwise EXOR with 32 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char * toggleCase ( char * a ) { for ( int i = 0 ; a [ i ] != ' \0' ; i ++ ) { a [ i ] ^= 32 ; } return a ; } int main ( ) { char str [ ] = " CheRrY " ; cout << " Toggle ▁ case : ▁ " << toggleCase ( str ) << endl ; cout << " Original ▁ string : ▁ " << toggleCase ( str ) << endl ; return 0 ; }