How to count the number of time a name appears in array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to count the number of time a name appears in array?

I've to write a c++ program where I've to print the number of times "JAVA" appears in say a sample input of JAVA JAVA DataStructures DBMS JAVA JAVA Python DataStructures. When I try to run this code: #include <bits/stdc++.h> using namespace std; int main(){ int arr[8]; for(int i=0;i<=7;i++){ cin>>arr[i]; } int count=0; for(int i=0;i<=7;i++){ if(arr[i]=="JAVA"){ count+=1; } } cout<<count; } it is showing me an error

8th Sep 2022, 2:46 PM
ss7
2 Answers
+ 3
if (strcmp(arr[i], "JAVA") == 0) count += 1;
8th Sep 2022, 3:15 PM
Maxwell D. Dorliea
Maxwell D. Dorliea - avatar
+ 2
You defined <arr> as an array of type `int`, a data type for integers. You should've defined an array of type `string` string arr[ 8 ]; // <- an array of string
8th Sep 2022, 5:16 PM
Ipang