+ 2

How can I count the number of a letter in a string

for example,, input a string and count the number of 'A' in it.

16th Mar 2018, 11:38 PM
Thon Tor Chol
Thon Tor Chol - avatar
8 Réponses
+ 14
#include <iostream> using namespace std; int main() { string str="hello #world"; int i,l=0; for(i=0;i<str.length();i++){ if(str[i]!=' '&&(str[i]>='a'&&str[i]<='z')) l++; } cout << l; return 0; } //haha , every language is similiar to java & easy ☺
17th Mar 2018, 2:40 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 12
@Samrad , he asked for C++ //not for javascript , btw still I found no link of it with the question needs
17th Mar 2018, 3:27 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
in java: char[] letters = string.split(""); int count = 0; for(char c : letters){ if(c == 'A') count++; }
16th Mar 2018, 11:41 PM
Jeremy
Jeremy - avatar
+ 1
@Gaurav Ok, I thought Question in JS for loops
18th Mar 2018, 1:37 AM
Sarmad Saad
Sarmad Saad - avatar
0
in c++ if u please and also,, what if the string has no A thanks
16th Mar 2018, 11:44 PM
Thon Tor Chol
Thon Tor Chol - avatar
0
i dont know much c++ but its rather similar. and if there is no 'A' the count would obviously be 0.
16th Mar 2018, 11:46 PM
Jeremy
Jeremy - avatar
0
OK thanks
16th Mar 2018, 11:46 PM
Thon Tor Chol
Thon Tor Chol - avatar
0
thanks
17th Mar 2018, 12:24 PM
Thon Tor Chol
Thon Tor Chol - avatar