OCCURENCE OF A CHARACTER.
This program takes one character as input and find frequency of entered character in given string. It give integer input that provides frequency of entered character.
JAVA CODE :)
import java.util.Scanner;
public class FisrOccurence
{
public static void main(String[] args)
{
String str;
char ch;
int i,flag=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a String:");
str=sc.nextLine();
System.out.print("Enter a Character:");
ch=sc.next().charAt(0);
sc.close();
for(i=0;i<str.length();i++)
{
if(str.charAt(i)==ch)
{
flag=1;
break;
}
}
if(flag==1)
{
System.out.print("Charcter "+ch+" is found at position "+(i+1));
}
else
{
System.out.print("Character "+ch+" is not found.");
}
}
}
0 Comments