codeparrot/xlcost-text-to-code huggingface dataset:
Determine if a string has all Unique Characters | C ++ program to illustrate String with unique characters using set data structure ; Inserting character of string into set ; If length of set is equal to len of string then it will have unique characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool uniqueCharacters ( string str ) { set < char > char_set ; for ( char c : str ) { char_set . insert ( c ) ; } if ( char_set . size ( ) == str . size ( ) ) { return true ; } else { return false ; } } int main ( ) { string str = " GeeksforGeeks " ; if ( uniqueCharacters ( str ) ) { cout << " The ▁ String ▁ " << str << " ▁ has ▁ all ▁ unique ▁ characters STRNEWLINE " ; } else { cout << " The ▁ String ▁ " << str << " ▁ has ▁ duplicate ▁ characters STRNEWLINE " ; } return 0 ; }
Ropes Data Structure ( Fast String Concatenation ) | Simple C ++ program to concatenate two strings ; Function that concatenates strings a [ 0. . n1 - 1 ] and b [ 0. . n2 - 1 ] and stores the result in c [ ] ; Copy characters of A [ ] to C [ ] ; Copy characters of B [ ] ; Driver code ; Concatenate a [ ] and b [ ] and store result in c [ ]
#include <iostream> NEW_LINE using namespace std ; void concatenate ( char a [ ] , char b [ ] , char c [ ] , int n1 , int n2 ) { int i ; for ( i = 0 ; i < n1 ; i ++ ) c [ i ] = a [ i ] ; for ( int j = 0 ; j < n2 ; j ++ ) c [ i ++ ] = b [ j ] ; c [ i ] = ' \0' ; } int main ( ) { char a [ ] = " Hi ▁ This ▁ is ▁ geeksforgeeks . ▁ " ; int n1 = sizeof ( a ) / sizeof ( a [ 0 ] ) ; char b [ ] = " You ▁ are ▁ welcome ▁ here . " ; int n2 = sizeof ( b ) / sizeof ( b [ 0 ] ) ; char c [ n1 + n2 - 1 ] ; concatenate ( a , b , c , n1 , n2 ) ; for ( int i = 0 ; i < n1 + n2 - 1 ; i ++ ) cout << c [ i ] ; return 0 ; }
Convert characters of a string to opposite case | C ++ program to toggle all characters ; Function to toggle characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void toggleChars ( string & S ) { for ( auto & it : S ) { if ( isalpha ( it ) ) { it ^= ( 1 << 5 ) ; } } } int main ( ) { string S = " GeKf @ rGeek $ " ; toggleChars ( S ) ; cout << " String ▁ after ▁ toggle ▁ " << endl ; cout << S << endl ; return 0 ; }
Binary representation of next greater number with same number of 1 ' s ▁ and ▁ 0' s | C ++ program to find next permutation in a binary string . ; Function to find the next greater number with same number of 1 ' s ▁ and ▁ 0' s ; locate first ' i ' from end such that bnum [ i ] == '0' and bnum [ i + 1 ] == '1' swap these value and break ; ; if no swapping performed ; Since we want the smallest next value , shift all 1 ' s ▁ at ▁ the ▁ end ▁ in ▁ the ▁ binary ▁ ▁ substring ▁ starting ▁ from ▁ index ▁ ' i + 2 ' ; special case while swapping if '0' occurs then break ; required next greater number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string nextGreaterWithSameDigits ( string bnum ) { int l = bnum . size ( ) ; int i ; for ( int i = l - 2 ; i >= 1 ; i -- ) { if ( bnum . at ( i ) == '0' && bnum . at ( i + 1 ) == '1' ) { char ch = bnum . at ( i ) ; bnum . at ( i ) = bnum . at ( i + 1 ) ; bnum . at ( i + 1 ) = ch ; break ; } } if ( i == 0 ) " no ▁ greater ▁ number " ; int j = i + 2 , k = l - 1 ; while ( j < k ) { if ( bnum . at ( j ) == '1' && bnum . at ( k ) == '0' ) { char ch = bnum . at ( j ) ; bnum . at ( j ) = bnum . at ( k ) ; bnum . at ( k ) = ch ; j ++ ; k -- ; } else if ( bnum . at ( i ) == '0' ) break ; else j ++ ; } return bnum ; } int main ( ) { string bnum = "10010" ; cout << " Binary ▁ representation ▁ of ▁ next ▁ greater ▁ number ▁ = ▁ " << nextGreaterWithSameDigits ( bnum ) ; return 0 ; }
Generate all rotations of a given string | A simple C ++ program to generate all rotations of a given string ; Print all the rotated string . ; Generate all rotations one by one and print ; Current index in str ; Current index in temp ; Copying the second part from the point of rotation . ; Copying the first part from the point of rotation . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRotatedString ( char str [ ] ) { int len = strlen ( str ) ; char temp [ len ] ; for ( int i = 0 ; i < len ; i ++ ) { int j = i ; int k = 0 ; while ( str [ j ] != ' \0' ) { temp [ k ] = str [ j ] ; k ++ ; j ++ ; } j = 0 ; while ( j < i ) { temp [ k ] = str [ j ] ; j ++ ; k ++ ; } printf ( " % s STRNEWLINE " , temp ) ; } } int main ( ) { char str [ ] = " geeks " ; printRotatedString ( str ) ; return 0 ; }
All possible strings of any length that can be formed from a given string | C ++ code to generate all possible strings that can be formed from given string ; Number of subsequences is ( 2 * * n - 1 ) ; Generate all subsequences of a given string . using counter 000. . 1 to 111. . 1 ; Check if jth bit in the counter is set If set then print jth element from arr [ ] ; Print all permutations of current subsequence ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAll ( string str ) { int n = str . length ( ) ; unsigned int opsize = pow ( 2 , n ) ; for ( int counter = 1 ; counter < opsize ; counter ++ ) { string subs = " " ; for ( int j = 0 ; j < n ; j ++ ) { if ( counter & ( 1 << j ) ) subs . push_back ( str [ j ] ) ; } do { cout << subs << " ▁ " ; } while ( next_permutation ( subs . begin ( ) , subs . end ( ) ) ) ; } } int main ( ) { string str = " abc " ; printSubsequences ( str ) ; return 0 ; }
Longest Non | C ++ implementation to find maximum length substring which is not palindrome ; utility function to check whether a string is palindrome or not ; Check for palindrome . ; palindrome string ; function to find maximum length substring which is not palindrome ; to check whether all characters of the string are same or not ; All characters are same , we can 't  make a non-palindromic string. ; If string is palindrome , we can make it non - palindrome by removing any corner character ; Complete string is not a palindrome . ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string str ) { int n = str . size ( ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) if ( str . at ( i ) != str . at ( n - i - 1 ) ) return false ; return true ; } int maxLengthNonPalinSubstring ( string str ) { int n = str . size ( ) ; char ch = str . at ( 0 ) ; int i = 1 ; for ( i = 1 ; i < n ; i ++ ) if ( str . at ( i ) != ch ) break ; if ( i == n ) return 0 ; if ( isPalindrome ( str ) ) return n - 1 ; return n ; } int main ( ) { string str = " abba " ; cout << " Maximum ▁ length ▁ = ▁ " << maxLengthNonPalinSubstring ( str ) ; return 0 ; }
Move spaces to front of string in single traversal | C ++ program to bring all spaces in front of string using swapping technique ; Function to find spaces and move to beginning ; Traverse from end and swap spaces ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void moveSpaceInFront ( char str [ ] ) { int i = strlen ( str ) - 1 ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) swap ( str [ i -- ] , str [ j ] ) ; } int main ( ) { char str [ ] = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " ; moveSpaceInFront ( str ) ; cout << str ; return 0 ; }
Move spaces to front of string in single traversal | CPP program to bring all spaces in front of string using swapping technique ; Function to find spaces and move to beginning ; Keep copying non - space characters ; Move spaces to be beginning ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void moveSpaceInFront ( char str [ ] ) { int i = strlen ( str ) ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) str [ i -- ] = str [ j ] ; while ( i >= 0 ) str [ i -- ] = ' ▁ ' ; } int main ( ) { char str [ ] = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " ; moveSpaceInFront ( str ) ; cout << str ; return 0 ; }
Minimum number of Appends needed to make a string palindrome | C program to find minimum number of appends needed to make a string Palindrome ; Checking if the string is palindrome or not ; single character is always palindrome ; pointing to first character ; pointing to last character ; Recursive function to count number of appends ; Removing first character of string by incrementing base address pointer . ; Driver program to test above functions
#include <stdio.h> NEW_LINE #include <string.h> NEW_LINE #include <stdbool.h> NEW_LINE bool isPalindrome ( char * str ) { int len = strlen ( str ) ; if ( len == 1 ) return true ; char * ptr1 = str ; char * ptr2 = str + len - 1 ; while ( ptr2 > ptr1 ) { if ( * ptr1 != * ptr2 ) return false ; ptr1 ++ ; ptr2 -- ; } return true ; } int noOfAppends ( char s [ ] ) { if ( isPalindrome ( s ) ) return 0 ; s ++ ; return 1 + noOfAppends ( s ) ; } int main ( ) { char s [ ] = " abede " ; printf ( " % d STRNEWLINE " , noOfAppends ( s ) ) ; return 0 ; }
Find Excel column number from column title | C ++ program to return title to result of excel sheet . ; Returns resul when we pass title . ; This process is similar to binary - to - decimal conversion ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int titleToNumber ( string s ) { int result = 0 ; for ( const auto & c : s ) { result *= 26 ; result += c - ' A ' + 1 ; } return result ; } int main ( ) { cout << titleToNumber ( " CDA " ) << endl ; return 0 ; }
Check whether K | CPP program to check if k - th bit of a given number is set or not using right shift operator . ; Driver code
#include <iostream> NEW_LINE using namespace std ; void isKthBitSet ( int n , int k ) { if ( ( n >> ( k - 1 ) ) & 1 ) cout << " SET " ; else cout << " NOT ▁ SET " ; } int main ( ) { int n = 5 , k = 1 ; isKthBitSet ( n , k ) ; return 0 ; }
Reverse string without using any temporary variable | C ++ Program to reverse a string without using temp variable ; Function to reverse string and return revesed string ; Iterate loop upto start not equal to end ; XOR for swapping the variable ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string reversingString ( string str , int start , int end ) { while ( start < end ) { str [ start ] ^= str [ end ] ; str [ end ] ^= str [ start ] ; str [ start ] ^= str [ end ] ; ++ start ; -- end ; } return str ; } int main ( ) { string s = " GeeksforGeeks " ; cout << reversingString ( s , 0 , 12 ) ; return 0 ; }
Check if a string follows a ^ nb ^ n pattern or not | C ++ code to check a ^ nb ^ n pattern ; Returns " Yes " str is of the form a ^ nb ^ n . ; check first half is ' a ' and other half is full of ' b ' ; Driver code ; Function call
#include <iostream> NEW_LINE using namespace std ; string isAnBn ( string str ) { int n = str . length ( ) ; if ( n & 1 ) return " No " ; int i ; for ( i = 0 ; i < n / 2 ; i ++ ) if ( str [ i ] != ' a ' str [ n - i - 1 ] != ' b ' ) return " No " ; return " Yes " ; } int main ( ) { string str = " ab " ; cout << isAnBn ( str ) ; return 0 ; }
Number of substrings divisible by 6 in a string of integers | C ++ program to calculate number of substring divisible by 6. ; Return the number of substring divisible by 6 and starting at index i in s [ ] and previous sum of digits modulo 3 is m . ; End of the string . ; If already calculated , return the stored value . ; Converting into integer . ; Increment result by 1 , if current digit is divisible by 2 and sum of digits is divisible by 3. And recur for next index with new modulo . ; Returns substrings divisible by 6. ; For storing the value of all states . ; If string contain 0 , increment count by 1. ; Else calculate using recursive function . Pass previous sum modulo 3 as 0. ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  100002 NEW_LINE using namespace std ; int f ( int i , int m , char s [ ] , int memoize [ ] [ 3 ] ) { if ( i == strlen ( s ) ) return 0 ; if ( memoize [ i ] [ m ] != -1 ) return memoize [ i ] [ m ] ; int x = s [ i ] - '0' ; int ans = ( ( x + m ) % 3 == 0 && x % 2 == 0 ) + f ( i + 1 , ( m + x ) % 3 , s , memoize ) ; return memoize [ i ] [ m ] = ans ; } int countDivBy6 ( char s [ ] ) { int n = strlen ( s ) ; int memoize [ n + 1 ] [ 3 ] ; memset ( memoize , -1 , sizeof memoize ) ; int ans = 0 ; for ( int i = 0 ; i < strlen ( s ) ; i ++ ) { if ( s [ i ] == '0' ) ans ++ ; else ans += f ( i , 0 , s , memoize ) ; } return ans ; } int main ( ) { char s [ ] = "4806" ; cout << countDivBy6 ( s ) << endl ; return 0 ; }
Lexicographically first palindromic string | C ++ program to find first palindromic permutation of given string ; Function to count frequency of each char in the string . freq [ 0 ] for ' a ' , ... . , freq [ 25 ] for ' z ' ; Cases to check whether a palindr0mic string can be formed or not ; count_odd to count no of chars with odd frequency ; For even length string no odd freq character ; For odd length string one odd freq character ; Function to find odd freq char and reducing its freq by 1 returns " " if odd freq char is not present ; To find lexicographically first palindromic string . ; Assigning odd freq character if present else empty string . ; Traverse characters in increasing order ; Divide all occurrences into two halves . Note that odd character is removed by findOddAndRemoveItsFreq ( ) ; creating front string ; creating rear string ; Final palindromic string which is lexicographically first ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const char MAX_CHAR = 26 ; void countFreq ( string str , int freq [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) freq [ str . at ( i ) - ' a ' ] ++ ; } bool canMakePalindrome ( int freq [ ] , int len ) { int count_odd = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( freq [ i ] % 2 != 0 ) count_odd ++ ; if ( len % 2 == 0 ) { if ( count_odd > 0 ) return false ; else return true ; } if ( count_odd != 1 ) return false ; return true ; } string findOddAndRemoveItsFreq ( int freq [ ] ) { string odd_str = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] % 2 != 0 ) { freq [ i ] -- ; odd_str = odd_str + ( char ) ( i + ' a ' ) ; return odd_str ; } } return odd_str ; } string findPalindromicString ( string str ) { int len = str . length ( ) ; int freq [ MAX_CHAR ] = { 0 } ; countFreq ( str , freq , len ) ; if ( ! canMakePalindrome ( freq , len ) ) return " No ▁ Palindromic ▁ String " ; string odd_str = findOddAndRemoveItsFreq ( freq ) ; string front_str = " " , rear_str = " ▁ " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { string temp = " " ; if ( freq [ i ] != 0 ) { char ch = ( char ) ( i + ' a ' ) ; for ( int j = 1 ; j <= freq [ i ] / 2 ; j ++ ) temp = temp + ch ; front_str = front_str + temp ; rear_str = temp + rear_str ; } } return ( front_str + odd_str + rear_str ) ; } int main ( ) { string str = " malayalam " ; cout << findPalindromicString ( str ) ; return 0 ; }
Count substrings with same first and last characters | C ++ program to count all substrings with same first and last characters . ; Returns true if first and last characters of s are same . ; Starting point of substring ; Length of substring ; Check if current substring has same starting and ending characters . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkEquality ( string s ) { return ( s [ 0 ] == s [ s . size ( ) - 1 ] ) ; } int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) if ( checkEquality ( s . substr ( i , len ) ) ) result ++ ; return result ; } int main ( ) { string s ( " abcab " ) ; cout << countSubstringWithEqualEnds ( s ) ; return 0 ; }
Count substrings with same first and last characters | Space efficient C ++ program to count all substrings with same first and last characters . ; Iterating through all substrings in way so that we can find first and last character easily ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( s [ i ] == s [ j ] ) result ++ ; return result ; } int main ( ) { string s ( " abcab " ) ; cout << countSubstringWithEqualEnds ( s ) ; return 0 ; }
Find the missing number in a string of numbers with no separator | C ++ program to find a missing number in a string of consecutive numbers without any separator . ; gets the integer at position i with length m , returns it or - 1 , if none ; Find value at index i and length m . ; Returns value of missing number ; Try all lengths for first number ; Get value of first number with current length / ; To store missing number of current length ; To indicate whether the sequence failed anywhere for current length . ; Find subsequent numbers with previous number as n ; If we haven 't yet found the missing number  for current length. Next number is n+2. Note  that we use Log10 as (n+2) may have more  length than n. ; If next value is ( n + 1 ) ; return - 1 ; not found or no missing number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_DIGITS = 6 ; int getValue ( const string & str , int i , int m ) { if ( i + m > str . length ( ) ) return -1 ; int value = 0 ; for ( int j = 0 ; j < m ; j ++ ) { int c = str [ i + j ] - '0' ; if ( c < 0 c > 9 ) return -1 ; value = value * 10 + c ; } return value ; } int findMissingNumber ( const string & str ) { for ( int m = 1 ; m <= MAX_DIGITS ; ++ m ) { int n = getValue ( str , 0 , m ) ; if ( n == -1 ) break ; int missingNo = -1 ; bool fail = false ; for ( int i = m ; i != str . length ( ) ; i += 1 + log10l ( n ) ) { if ( ( missingNo == -1 ) && ( getValue ( str , i , 1 + log10l ( n + 2 ) ) == n + 2 ) ) { missingNo = n + 1 ; n += 2 ; } else if ( getValue ( str , i , 1 + log10l ( n + 1 ) ) == n + 1 ) n ++ ; else { fail = true ; break ; } } if ( ! fail ) return missingNo ; } } int main ( ) { cout << findMissingNumber ( "99101102" ) ; return 0 ; }
Maximum consecutive repeating character in string | C ++ program to find the maximum consecutive repeating character in given string ; function to find out the maximum repeating character in given string ; Find the maximum repeating character starting from str [ i ] ; Update result if required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char maxRepeating ( string str ) { int len = str . length ( ) ; int count = 0 ; char res = str [ 0 ] ; for ( int i = 0 ; i < len ; i ++ ) { int cur_count = 1 ; for ( int j = i + 1 ; j < len ; j ++ ) { if ( str [ i ] != str [ j ] ) break ; cur_count ++ ; } if ( cur_count > count ) { count = cur_count ; res = str [ i ] ; } } return res ; } int main ( ) { string str = " aaaabbaaccde " ; cout << maxRepeating ( str ) ; return 0 ; }
Sum of two large numbers | C ++ program to find sum of two large numbers . ; Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty string for storing result ; Calculate length of both string ; Reverse both of strings ; Do school mathematics , compute sum of current digits and carry ; Calculate carry for next step ; Add remaining digits of larger number ; Add remaining carry ; reverse resultant string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findSum ( string str1 , string str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) swap ( str1 , str2 ) ; string str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; reverse ( str1 . begin ( ) , str1 . end ( ) ) ; reverse ( str2 . begin ( ) , str2 . end ( ) ) ; int carry = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { int sum = ( ( str1 [ i ] - '0' ) + ( str2 [ i ] - '0' ) + carry ) ; str . push_back ( sum % 10 + '0' ) ; carry = sum / 10 ; } for ( int i = n1 ; i < n2 ; i ++ ) { int sum = ( ( str2 [ i ] - '0' ) + carry ) ; str . push_back ( sum % 10 + '0' ) ; carry = sum / 10 ; } if ( carry ) str . push_back ( carry + '0' ) ; reverse ( str . begin ( ) , str . end ( ) ) ; return str ; } int main ( ) { string str1 = "12" ; string str2 = "198111" ; cout << findSum ( str1 , str2 ) ; return 0 ; }
Sum of two large numbers | C ++ program to find sum of two large numbers . ; Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty string for storing result ; Calculate length of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute sum of current digits and carry ; Add remaining digits of str2 [ ] ; Add remaining carry ; reverse resultant string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findSum ( string str1 , string str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) swap ( str1 , str2 ) ; string str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; int diff = n2 - n1 ; int carry = 0 ; for ( int i = n1 - 1 ; i >= 0 ; i -- ) { int sum = ( ( str1 [ i ] - '0' ) + ( str2 [ i + diff ] - '0' ) + carry ) ; str . push_back ( sum % 10 + '0' ) ; carry = sum / 10 ; } for ( int i = n2 - n1 - 1 ; i >= 0 ; i -- ) { int sum = ( ( str2 [ i ] - '0' ) + carry ) ; str . push_back ( sum % 10 + '0' ) ; carry = sum / 10 ; } if ( carry ) str . push_back ( carry + '0' ) ; reverse ( str . begin ( ) , str . end ( ) ) ; return str ; } int main ( ) { string str1 = "12" ; string str2 = "198111" ; cout << findSum ( str1 , str2 ) ; return 0 ; }
Palindrome pair in an array of words ( or strings ) | C ++ program to find if there is a pair that can form a palindrome . ; Utility function to check if a string is a palindrome ; compare each character from starting with its corresponding character from last ; Function to check if a palindrome pair exists ; Consider each pair one by one ; concatenate both strings ; check if the concatenated string is palindrome ; check for other combination of the two strings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len / 2 ; i ++ ) if ( str [ i ] != str [ len - i - 1 ] ) return false ; return true ; } bool checkPalindromePair ( vector < string > vect ) { for ( int i = 0 ; i < vect . size ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < vect . size ( ) ; j ++ ) { string check_str ; check_str = vect [ i ] + vect [ j ] ; if ( isPalindrome ( check_str ) ) return true ; check_str = vect [ j ] + vect [ i ] ; if ( isPalindrome ( check_str ) ) return true ; } } return false ; } int main ( ) { vector < string > vect = { " geekf " , " geeks " , " or " , " keeg " , " abc " , " bc " } ; checkPalindromePair ( vect ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Print consecutive characters together in a line | C ++ program to print consecutive characters together in a line . ; Driver code
#include <iostream> NEW_LINE using namespace std ; void print ( string str ) { cout << str [ 0 ] ; for ( int i = 1 ; str [ i ] != ' \0' ; i ++ ) { if ( ( str [ i ] == str [ i - 1 ] + 1 ) || ( str [ i ] == str [ i - 1 ] - 1 ) ) cout << str [ i ] ; else cout << " STRNEWLINE " << str [ i ] ; ; } } int main ( ) { string str = " ABCXYZACCD " ; print ( str ) ; return 0 ; }
Efficiently check if a string has all unique characters without using any additional data structure | A space efficient C ++ program to check if all characters of string are unique . ; Returns true if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areChractersUnique ( string str ) { int checker = 0 ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { int val = ( str [ i ] - ' a ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return false ; checker |= ( 1 << val ) ; } return true ; } int main ( ) { string s = " aaabbccdaa " ; if ( areChractersUnique ( s ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }