חזור לתיקייה: בוחן אמצע

q1

חיפוש בתיקיה
Ctrl K
לחץ כאן או הקש Ctrl K כדי לחפש בכל התיקיה

    q1

    02340114

    מבוא למדמח מ

    רשימות הרצאה לקריאה בשבת

    q1

    הופק מתוך תקיית התואר של שקד

    uni-folder.vercel.app

    q1.c

    הערה (קובץ קוד)

    קובץ זה הוא קובץ קוד מסוג C.

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    #define DONT_KNOW "I_dont_know"
    #define NUMBER_OF_LETTERS ('z' - 'a' + 1)
    
    void printIDontKnow()
    {
        printf("%s", DONT_KNOW);
        exit(0);
    }
    
    int midterm_quiz_q1(char arr[], int n);
    
    // Do not change read_arr!!!
    char *read_arr(int *n)
    {
        if (scanf("%d", n) != 1)
            return NULL;
        char *arr = malloc((*n) * sizeof(char));
        if (!arr)
            return NULL;
        for (int i = 0; i < *n; i++)
        {
            if (scanf(" %c", arr + i) != 1)
            {
                free(arr);
                return NULL;
            }
        }
        return arr;
    }
    
    //Uncomment the first line in main if you don't know the answer.
    //Otherwise, do not change main!!!
    int main()
    {
        // printIDontKnow();
    
        int n;
        char *arr = read_arr(&n);
        if (!arr)
            return 1;
        printf("%d", midterm_quiz_q1(arr, n));
        free(arr);
        return 0;
    }
    
    /*
    * This function gets an array of small english letters and returns the number
    * of different letters in the array.
    * input:
    * 1. char arr[]: the array of letters.
    * 2. int n: the length of the array.
    * output(int):
    *   the number of different letters in the array.
     */
    int midterm_quiz_q1(char arr[], int n)
    {
        int histogram[NUMBER_OF_LETTERS] = {0};
        for (int i = 0; i < n; i++)
        {
            histogram[arr[i] - 'a']++;
        }
    
        int numOfLetters = 0;
        for (int i = 0; i < NUMBER_OF_LETTERS; i++)
        {
            if (histogram[i] > 0)
            {
                numOfLetters++;
            }
        }
    
        return numOfLetters;
    }