Home > Software design >  Need help in C syntax
Need help in C syntax

Time:03-29

#define FIRST 'a'
#define LAST  'd'
#define ALL  ((1 << (LAST-FIRST 1)) - 1)

int main()
{
    string s1, s2;
    s1 = "aabcd";
    s2 = "caabd";
    // getline(cin, s1);
    // getline(cin, s2);

    // Build masks
    int mask[1 << CHAR_BIT] = {};
    for (char c = FIRST; c <= LAST;   c)
    {
        auto it1 = s1.begin(), it2 = s2.begin();
        bool done = false, fail = false;
        int count[1 << CHAR_BIT] = {};
        mask[c] = ALL;
        // cout << "c:" << c << " " << ALL << " " <<  mask[c] << '\n';
        do {
            // Count characters until next match for c
            while (it1 != s1.end() && *it1 != c)   count[(unsigned char)*it1  ];

Can someone explain how the last sentence works (while (it1 != s1.end() && *it1 != c) count[(unsigned char)*it1 ]; in layman terms. Also, count[1 << CHAR_BIT]. how do i print the content of the count[] out?

CodePudding user response:

  1. it1 : this is a post-increment statement. This means that the returned value from the operation it1 would be the previous value held by it1 (meaning, before the increment).
  2. *it1 : the * operator, when applied to iterators, accesses its contents. So, here we're accessing the contents of the previous iterator (meaning, the one before the increment took place).
  3. (unsigned char)*it1 : cast the value contained in the previous iterator to unsigned char (probably from char; this is to avoid negative values as an array index).
  4. count[(unsigned char)*it1 ]: use the previous value as an index for the count array.
  5. count[(unsigned char)*it1 ]: this is a pre-increment statement. This means that the return value will be the result of incrementing the value contained at the given index. Since the array is default initialized using braces ({}), all values will initially evaluate to 0. So, adding:
  6. cout << count[(unsigned char)*it1 ] << "\n"; will print the actual count for that letter, with a minimum of 1 since it starts at 0 and it's a pre-increment.

CodePudding user response:

I wrote that code as part of this answer.

The part you're asking about is this:

auto it1 = s1.begin();
int count[1 << CHAR_BIT] = {};

do {
    while (it1 != s1.end() && *it1 != c)   count[(unsigned char)*it1  ];
} while (...);

First, it obtains an iterator to the beginning of string s1. It will be used to walk through the string.

auto it1 = s1.begin();

Then it intitializes a histogram as the array count which holds one integer for every possible character, and all values are initialized to zero.

int count[1 << CHAR_BIT] = {};

Let's break down the while-loop in parts:

  • it1 != s1.end() tests that the iterator has not reached the end of the string
  • *it1 != c dereferences the iterator (obtaining the character at that position) and compares it to c
  • count[(unsigned char)*it1 ]; increases the count array at the index given by the current character, and also moves the iterator to the next position in the string.

So what this does is read characters until reaching the end of the string or encountering the character c, and increases the count for the current character by 1.

Regarding your other question:

how do i print the content of the count[] out?

Like this:

for (char c = FIRST; c <= LAST;   c)
{
    cout << "count(" << c << ") = " << count[(unsigned char)c] << "\n";
}
  •  Tags:  
  • c
  • Related