ASCII Codes :

Pasted image 20231217135254.png#

Strings :

Pasted image 20231217140230.png

  • ! Note that we are avoiding the use of "" here.
    Pasted image 20231217140502.png
  • Without \0 it is just an array of characters.

String :

Pasted image 20231217141217.png

  • ! Note that we are using "" for strings here.

Reading and writing a string :

Pasted image 20231217141619.png

  • scanf("%s", name) here won't take space separated strings whereas gets(name) will.

Length of a String :

Pasted image 20231217144626.png

Changing Case of letters :

Upper to Lower case :

  • Add +32
  • Pasted image 20231217145809.png

Lower to Upper :

  • Subtract -32

Toggle Case :

Pasted image 20231217150044.png

Reversing a string :

Method 1 :

Pasted image 20231217152855.png

Method 2 :

by swapping characters at ith and jth position.
Pasted image 20231217153201.png

Comparing two strings :

ek ek letter compare karlo in each string :
Pasted image 20231217153534.png

  • Non Case Sensitive string equality checking : convert all the string characters to small letters and then check.

Finding Duplicates in a string :

Method: 1

same as array : jo element mil jaye usko 0 (zero) se replace kardo (array me -1 se replace kiya tha hamne)

Method: 2 : Using Hash Table :

  • ASCII codes ke hisaab se array lena.
  • To reduce the size of the hash table: Suppose we only want to search in letters that belong to 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
  • Therefore take the hashtable with size 26 so that the indexes will range from 0 to 25.
  • Fill the hash table with zeros.
  • Letter find hone pe increment the value in hash table.

Pasted image 20231217162004.png

Method 3 : Using Bit Wise Operators :

Masking :

Knowing if a particular bit inside a memory is on or off is called masking.
We use and (&) operator here.
Pasted image 20231217164632.png

Merging :

Turning a bit on or off is called Merging.
We use or (|) operator here.
Pasted image 20231217170029.png

Finding Duplicates in a String using Bitwise Operations

Pasted image 20231218174436.png

#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;
}

Check for Anagram :

  • Traditional Method : first check if the two strings are of equal size. If they are not then they are not an anagram. Then check if all the elements are present in the 2nd string then take care of duplicacy (this is a pretty gruesome method). Let's take a look at method which utilizes Hash Table :
  • Agar lowercase uppercase symbols sab check karna hai to 128 size ka array lenge.
  • take the hash table and fill it with zeros (here we haven't shown it filled with zeros so as to avoid congestion).
#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;
}

Permutations of a String :

Pasted image 20231218194743.png
Pasted image 20231218201850.png

  • With the help of recursion we are performing backtracking which in turn helps us to BruteForce.
#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;
}

Using swapping :

Pasted image 20231218215915.png

#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;
}