ASCII Codes :
#
""
here.\0
it is just an array of characters.""
for strings here.scanf("%s", name)
here won't take space separated strings whereas gets(name)
will.+32
-32
by swapping characters at ith and jth position.
ek ek letter compare karlo in each string :
same as array : jo element mil jaye usko 0
(zero) se replace kardo (array me -1
se replace kiya tha hamne)
a to z
range, ascii range of this is 97 to 122
. Now, if we subtract 97 on both sides then we'll get : 0 to 25
26
so that the indexes will range from 0 to 25
.Knowing if a particular bit inside a memory is on or off is called masking.
We use and (&
) operator here.
Turning a bit on or off is called Merging.
We use or (|
) operator here.
#include <iostream>
using namespace std;
void checkViaBinary(char str[])
{
int H = 0; // size : 4 bytes = 32 bits
int x = 0; // size : 4 bytes = 32 bits
for (int i = 0; str[i] != '\0'; i++)
{
x = 1;
x = x << (str[i] - 97);
if ((x & H) > 0)
printf("%c is duplicate\n", str[i]);
else
H = H | x;
}
}
int main()
{
// for ascii 97 to 122 (a-z)
char str[] = "hitarth";
checkViaBinary(str);
return 0;
}
#include <iostream>
using namespace std;
void checkAnagram(char str1[], char str2[])
{
int H[26] = {0};
int i;
for ( i =0; str1[i] != '\0'; i++)
{
H[str1[i]-97]++;
}
for ( i =0; str2[i] != '\0'; i++)
{
H[str2[i]-97]--;
if ( H[str2[i]-97] < 0)
{
printf("Not an Anagram!");
break;
}
}
if (str2[i] == '\0')
printf("It's an anagram");
}
int main()
{
// for ascii 97 to 122 (a-z)
char str1[] = "decimal";
char str2[] = "medical";
checkAnagram(str1, str2);
return 0;
}
#include <iostream>
using namespace std;
void perm(char str[], int k)
{
static int A[10] = {0};
static char res[10];
int i;
if (str[k] == '\0')
{
res[k] = '\0';
printf("%s\n", res);
}
else
{
for(i =0; str[i] != '\0'; i++)
{
if(A[i]==0)
{
res[k] = str[i];
A[i] = 1;
perm(str, k+1);
A[i]=0;
}
}
}
}
int main()
{
char str[] = "ABC";
perm(str, 0);
return 0;
}
#include <iostream>
using namespace std;
// l = 0th index of string
// h = last index of string
void perm(char str[], int l, int h)
{
int i;
if(l==h)
{
printf("%s\n", str);
}
else
{
for (i=l; i<=h; i++)
{
swap(str[l], str[i]);
perm(str, l+1, h);
swap(str[l], str[i]);
}
}
}
int main()
{
char str[] = "ABC";
perm(str, 0, 2);
return 0;
}