How to calculate the average length of the strings in string array with C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

int main() {
    const char* arr[] = {"c#", "c", "java", "python", "c++"};
    int totalLength = 0;
    int size = sizeof(arr) / sizeof(arr[0]);

    for (int i = 0; i < size; i++) {
        totalLength += strlen(arr[i]);
    }

    double avg = (double)totalLength / size;

    printf("%.2f\n", avg);
    
    return 0;
}

 
 
 
/*
run:
 
3.20
 
*/

 



answered Aug 20, 2024 by avibootz
...