Home > Software design >  I can not understanding this portion of code for this token identification program
I can not understanding this portion of code for this token identification program

Time:01-12

I have got this code reference from a website and practicing with this. When I am trying to working with malloc() function then how we getting the value from right-left 2

// Extraction string
char* subString(char* str, int left, int right)
{
    int i;
    char* subStr = (char*)malloc(
                  sizeof(char) * (right - left   2));

    for (i = left; i <= right; i  )
        subStr[i - left] = str[i];
    subStr[right - left   1] = '\0';
    return (subStr);
}

// Parsing the input STRING.
void parse(char* str)
{
    int left = 0, right = 0;
    int len = strlen(str);

    while (right <= len && left <= right) {
        if (isDelimiter(str[right]) == false)
            right  ;

        if (isDelimiter(str[right]) == true && left == right) {
            if (isOperator(str[right]) == true)
                printf("'%c' IS AN OPERATOR\n", str[right]);

            right  ;
            left = right;
        } else if (isDelimiter(str[right]) == true && left != right
                   || (right == len && left != right)) {
            char* subStr = subString(str, left, right - 1);

            if (isKeyword(subStr) == true)
                printf("'%s' IS A KEYWORD\n", subStr);

            left = right;
        }
    }
    return;
}

// DRIVER FUNCTION
int main()
{
     // maximum length of string is 100 here
    char str[100] = "int a = b   1c; ";

    parse(str); // calling the parse function

    return (0);
}

I have got all of the concepts but this concept I am not getting this:

// Extracts the SUBSTRING.
char* subString(char* str, int left, int right)
{
    int i;
    char* subStr = (char*)malloc(
                  sizeof(char) * (right - left   2));

    for (i = left; i <= right; i  )
        subStr[i - left] = str[i];
    subStr[right - left   1] = '\0';
    return (subStr);
}

how the subString function is working. Specially (right - left 2) part. If anyone can help with any documentation or tutorial or over a comment to understand.

CodePudding user response:

For starters the code in whole is bad and it seems is written by a low-qualified programmer. You should not trust every code that you will find in the internet.

For example the function subString should be declared like

char * subString( const char *str, size_t left, size_t right );

instead of

char* subString(char* str, int left, int right);

That is the source string is not changed within the function. So the first parameter should have the qualifier const. The second and third parameters should have the unsigned integer type size_t (it is the type of values returned for example by the function strlen or by the operator sizeof) instead of the signed integer type int.

The function subString builds a dynamically allocated character array that will contain a substring of the passed string with the range [left, right]. So the length of the substring is calculated like right - left 1. Also one more byte shall be reserved for the terminating zero character '\0' of the substring. So in whole there is required to allocate memory of the size right - left 2.

In the for loop

for (i = left; i <= right; i  )
    subStr[i - left] = str[i];

the characters from the source string starting from the index left are copied in the dynamically allocated array starting from its position 0.

So as the index in the for loop starts from left then indices in the destination array are calculated like i - left. That is when i initially equal to left then the expression i - left produces the value 0 as required for the destination array. When i after the first iteration of the loop will be increased by 1 then the expression i - left will produce the value 1 and so on.

At last this statement

subStr[right - left   1] = '\0';

sets the last character of the destination substring equal to the terminating zero character '\0'.

Instead of the for loop you could use standard C function memcpy like

memcpy( subStr, str   left, right - left   1 );
subStr[right - left   1] = '\0';

Also the function should check whether the memory was allocated successfully before filling the allocated ,memory.

So the function could look for example the following way

// Extracts the SUBSTRING.
char * subString( const char *str, size_t left, size_t right )
{
    assert( left < right );

    char *subStr = malloc( right - left   1   sizeof( ( char )'\0' ) );

    if ( subStr != NULL )
    {
        memcpy( subStr, str   left, right - left   1 );
        subStr[right - left   1] = '\0';
    }

    return subStr;
}

A more general function can look the following way.

// Extracts the SUBSTRING.
char * subString( const char *str, size_t left, size_t right )
{
    char *subStr = NULL;

    if ( !( right < left ) )
    {
        size_t n = right - left   ( right != left );

        subStr = malloc( n   1 );

        if ( subStr != NULL )
        {
            memcpy( subStr, str   left, n );
            subStr[n] = '\0';
        }
    }

    return subStr;
}
  • Related