How to count number of characters in a string ?
- To count the amount of ‘x’ characters that are in a string, and print out the number using java program.
class Wikitechy
{
// Method that return count of the given
// character in the string
public static int count(String s, char c)
{
int res = 0;
for (int i=0; i<s.length(); i++)
{
// checking character in string
if (s.charAt(i) == c)
res++;
}
return res;
}
// Driver method
public static void main(String args[])
{
String str= "Welcome to Wikitechy";
char c = 'W';
System.out.println(count(str, c));
}
}
Output
2
Categorized in:
Tagged in:
Accenture interview questions and answers, Applied Materials interview questions and answers, Atos interview questions and answers, Capgemini interview questions and answers, CASTING NETWORKS INDIA PVT LIMITED interview questions and answers, CGI Group Inc interview questions and answers, char to string, character count, check string length, Chetu interview questions and answers, Ciena Corporation interview questions and answers, Collabera Technologies interview questions and answers, count characters in string, count characters in string java, count number of characters in a string, count number of characters in a string in java, count occurrences of character in string java using hashmap, count repeated characters in string using java, count words in string, dark horse digital interview questions and answers, Dell International Services India Pvt Ltd interview questions and answers, excel count characters in string, FIS Global Business Solutions India Pvt Ltd interview questions and answers, Flipkart interview questions and answers, how to count characters in string array in java, how to count the number of occurrences of a character in a string in java, IBM interview questions and answers, Indecomm Global Services interview questions and answers, Infosys Technologies interview questions and answers, java program to count number of alphabets in a string, java program to count number of characters in a string, L&T Infotech interview questions and answers, Mphasis interview questions and answers, NetApp interview questions and answers, number of characters in a string, Oracle Corporation interview questions and answers, PeopleStrong interview questions and answers, Persistent Systems interview questions and answers, RBS India De interview questions and answers, Reliance Industries Ltd interview questions and answers, SAP Labs India Pvt Ltd interview questions and answers, Tech Mahindra interview questions and answers, UnitedHealth Group interview questions and answers, Virtusa Consulting Services Pvt Ltd interview questions and answers, Wells Fargo interview questions and answers, Wipro Infotech interview questions and answers, word counter