# Count How Many Times a Character is Present in a String

by Dinesh Thakur Category: array

Algorithm for CharacterInString:

step 1: create an integer array a[] of size 26

step 2: initialize i=0

step 3: repeat through step-5 while (i < 26)

step 4: a[i]= 0

step 5: i=i+ 1

step 7: set string st1=st.toLowerCase()

step 8: set a character array bt=stl.toCharArray()

step 9: initilaize j=0

step 10: repeat through step-l6 while (j < stl.length())

step 11: reset i=0

step 12: repeat through step-l5 while (i < 26)

step 13: set k=i+97

step 14: if(byte)bt[j]==k then a[i]=a[i]+ 1

step 15: i=i+ 1

step 16: j= j+ 1

step 17: initialize i=0

step 18: : repeat through step-20 while (i < 26)

step 19: if a[i]!=0 then print (char)(i+97) is present in string for a[i] times

step 20: i=i+ 1

step 21: Exit

Here is the Java Example for CharacterInString:

```import java.util.Scanner;

public class CharacterInString

{

public static void main(String args[])

{

int a[]=new int[26];

for(int i=0;i<26;i++)

{

a[i]=0;

}

Scanner s=new Scanner(System.in);

System.out.print("Enter the String : ");

String st=s.nextLine();

String st1=st.toLowerCase();

char bt[]=st1.toCharArray();

for(int j=0;j<st1.length();j++)

{

for(int i=0;i<26;i++)

{

int k=i+97;

if((byte)bt[j]==k)

{

a[i]++;

}

}

}

for(int i=0;i<26;i++)

{

if(a[i]!=0)

System.out.println((char) (i+97)+ " is present ! "+ a[i]+" times");

}

}

}

```

Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content

Popular Article