How to resolve the algorithm Compare length of two strings step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Compare length of two strings step by step in the C programming language

Table of Contents

Problem Statement

Given two strings of different length, determine which string is longer or shorter. Print both strings and their length, one on each line. Print the longer one first. Measure the length of your string in terms of bytes or characters, as appropriate for your language. If your language doesn't have an operator for measuring the length of a string, note it. Given more than two strings: list = ["abcd","123456789","abcdef","1234567"] Show the strings in descending length order.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Compare length of two strings step by step in the C programming language

This C code provided performs the following tasks:

  1. Sorting an array of integers in reverse order: The cmp function is used as a comparison function for qsort to sort an array of integers in reverse order.

  2. Calculating the lengths of strings in an array: It calculates the lengths of strings in an array and stores them along with their corresponding string indices in an array of integers.

  3. Determining the longest, shortest, and average-length strings: It uses the sorted array of string lengths to determine the longest, shortest, and average-length strings in the array.

Now, let's go through the code step by step:

  • Array Comparison: It takes an array of strings (strings) and the number of strings in the array (n) as input and compares the lengths of the strings. It calculates the lengths of all strings and stores them in an array of integers (si). Each element in si corresponds to a string in strings and contains the length of that string.

  • Sorting Strings: It sorts the si array in reverse order (i.e., longest length first) using qsort and the cmp function. This sorting is done to determine the longest and shortest strings efficiently.

  • Identifying Longest, Shortest, and Average Strings: After sorting, it iterates through the si array to identify the longest, shortest, and average-length strings. It does this by comparing the length of each string to the maximum and minimum lengths found during the sorting process.

  • Printing Results: For each string, it prints the string along with its length and a statement indicating whether it's the longest, shortest, or average-length string.

  • Main Function: In the main function, a sample list of strings (list) is defined and passed to the compareAndReportStringsLength function. The function then prints the results for the sample list.

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int cmp(const int* a, const int* b)
{
    return *b - *a; // reverse sort!
}

void compareAndReportStringsLength(const char* strings[], const int n)
{
    if (n > 0)
    {
        char* has_length = "has length";
        char* predicate_max = "and is the longest string";
        char* predicate_min = "and is the shortest string";
        char* predicate_ave = "and is neither the longest nor the shortest string";

        int* si = malloc(2 * n * sizeof(int));
        if (si != NULL)
        {
            for (int i = 0; i < n; i++)
            {
                si[2 * i] = strlen(strings[i]);
                si[2 * i + 1] = i;
            }
            qsort(si, n, 2 * sizeof(int), cmp);

            int max = si[0];
            int min = si[2 * (n - 1)];

            for (int i = 0; i < n; i++)
            {
                int length = si[2 * i];
                char* string = strings[si[2 * i + 1]];
                char* predicate;
                if (length == max)
                    predicate = predicate_max;
                else if (length == min)
                    predicate = predicate_min;
                else
                    predicate = predicate_ave;
                printf("\"%s\" %s %d %s\n",
                    string, has_length, length, predicate);
            }

            free(si);
        }
        else
        {
            fputs("unable allocate memory buffer", stderr);
        }
    }
}

int main(int argc, char* argv[])
{
    char* list[] = { "abcd", "123456789", "abcdef", "1234567" };

    compareAndReportStringsLength(list, 4);

    return EXIT_SUCCESS;
}


  

You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the E programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Julia programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the Retro programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the Oforth programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the SQL PL programming language