Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Mar 8, 2022

Permutation palindrome

#include <iostream>
#include <cstring>
using namespace std;

const int maxsize = 128;

void is_permutationPalindrome(char s[])
{
    char r[maxsize];
    memset(r, 0, maxsize);

    for(int i=0; i<strlen(s); i++)
    {
        r[s[i]] ++;
    }
    
    int num_evens = 0, num_odds = 0;
    
    for(int i=0; i<maxsize; i++)
    {
        if(r[i] %2 == 0)
            num_evens++;
        else
            num_odds++;
    }
    
    if(num_odds == 1 || num_odds == 0)
        cout << "it is a palindrome permutation";
    else
        cout << "it is not a palindrome permutation";
}

int main() {
// your code goes here
char *s = "defer";
is_permutationPalindrome(s);
return 0;
}

Mar 2, 2022

Print all permutations of a string

#include <iostream>
#include <cstring>
#include <string>
#define MAXSIZE 256
#define PATSIZE 10
using namespace std;

void permute(char str[], int l, int r)
{
    
    if(l==r)
    {
        cout&lt;&lt; str &lt;&lt; endl;
        return;
    }    
   
    
    for(int i=l;i&lt;=r;i++)
    {
        swap(str[l], str[i]);

        permute(str, l+1, r);
        
        swap(str[l], str[i]);      
    }

}

int main() {
// your code goes here
char txt[] = "abcd";
permute(txt, 0, strlen(txt)-1);
return 0;
}

Apr 19, 2011

Delete a character set present in a given string

char* strdel(char* s1, int len1, char* s2, int len2)
{
int bitmapvec[8]; //bit[n] = 1 if s1[i] == n else 0
for(int i=0; i<8; i++)
bitmapvec[i] = 0;

for(int i=0; i {
bitmapvec[ (s1[i] / 32) ] |= (1 << (s1[i] % 32)) ;
}

int j=0;
for(int i=0; i< len2; i++)
{
if(!(bitmapvec[s2[i]/32] & (1 << (s2[i] % 32))))
{
s2[j] = s2[i];
j++;
}
}

s2[j] = '\0';

return s2;
}