santosh kumar
santosh kumar

Reputation: 17

can anyone help me to understand this c++ code?

I explain you the working of this program.

step 1: enter the no. of time you want to run the loop.

step 2: enter two strings s1 and s2.

output : it will give you a string s3 that does not contain any character from string s2.

problem: I am unable to understand the working of for loop, and why the value of hash is 257, and how is loops working.

The code is given below.

#include <iostream>
using namespace std;
#include<string.h>
int main()
{
    int t;
    cout<<"enter any no. to run the loop"<<endl;
    cin>>t;
    while(t--)
    {
        string s1,s2,s3;
        int i,j,l1,l2;
    cout<<"enter two strings s1 and s2"<<endl;
        cin>>s1>>s2;
        l1=s1.length( );
         l2=s2.length( );
         int hash[257];
         for(i=0;i<257;i++)
         {
             hash[i]=0;
         }
          for(i=0;i<l2;i++)
         {
             hash[s2[i]]++;
         }
         
          for(i=0;i<l1;i++)
         {
             if(hash[s1[i]]==0)
             s3=s3+s1[i];
         }
         cout<<s3<<endl;
         
    }
    return 0;
}

Upvotes: 1

Views: 124

Answers (3)

Michael Chen
Michael Chen

Reputation: 645

This program figures out which characters in the first string are not contained in the second string.

Example input for the program:

1
abcdefghijklmnopqrstuvwxyz
helloworld

Example output (thanks to @mch for correction)

abcfgijkmnpqstuvxyz

Edit: Note that this is of course case sensitive as characters a and A produce different integer values.

Here is some commentary on the program:

#include <iostream>
using namespace std;
#include <string.h>
int main() {
    // Do the whole program as many times as the user says
    int t;
    cout << "enter any no. to run the loop" << endl;
    cin >> t;
    while (t--) {
        string s1, s2, s3;
        int i, j, l1, l2;
        // read strings and get their respective lengths
        cout << "enter two strings s1 and s2" << endl;
        cin >> s1 >> s2;
        l1 = s1.length();
        l2 = s2.length();

        // Array with 257 elements
        int hash[257];
        // Initialize all elements of array with 0
        for (i = 0; i < 257; i++) {
            hash[i] = 0;
        }

        // Count occurrences of characters in second string
        // s2[i] is the character at position i in s2
        // Increase the value of hash for this character by 1
        for (i = 0; i < l2; i++) {
            hash[s2[i]]++;
        }

        // Iterate over s1 characters
        // If hash[i] == 0: character i is not contained in s2
        // s3 => string of letters in s1 that are not contained in s2
        for (i = 0; i < l1; i++) {
            if (hash[s1[i]] == 0)
                s3 = s3 + s1[i];
        }

        // output s3
        cout << s3 << endl;
    }
    return 0;
}

Upvotes: 0

H-005
H-005

Reputation: 505

There's a comment above explaining the for-loops.

int hash[257] could actually be int hash[256] . There are 256 different values that can fit in a char (8 bits).

Upvotes: -1

user1196549
user1196549

Reputation:

The code computes an histogram of occurrences of the letters in s1 and copies the letters of s2 that have zero occurrence.

It can crash for any char type not restricted to the range [0,256] (!)

Upvotes: -1

Related Questions