How to count the number of characters in array of strings with C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>
  
int main() {
    const char *arr[] = {"c", "c++", "c#", "java", "python"};
      
    int rows = sizeof (arr) / sizeof (const char *);
    int totalCharacters = 0;
  
    for (int i = 0; i < rows; i++) {
        totalCharacters += strlen(arr[i]);
    }
     
    printf("%d\n", totalCharacters);
 
}
  
  
  
/*
run:
  
16
  
*/

 



answered Dec 1, 2020 by avibootz
edited Aug 20, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
int totoal_characters(const char **arr, int rows) {
    int totalCharacters = 0;
     
    for (int i = 0; i < rows; i++) {
        totalCharacters += strlen(arr[i]);
    }

    return totalCharacters;
}
  
int main() {
    const char *arr[] = {"c", "c++", "c#", "java", "python"};
      
    int rows = sizeof (arr) / sizeof (const char *);
 
    printf("%d\n", totoal_characters(arr, rows));
}
  
  
  
/*
run:
  
16
  
*/

 



answered Dec 1, 2020 by avibootz
edited Aug 20, 2024 by avibootz

Related questions

...