write a program in java to input a string and check that how many times a alphabet is repeated
Answers
Answered by
3
Hola friend!!
Rajdeep here!!
Here's your program using Scanner:
import java.util.*;
class Alpha
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
String str;
char ch, inp;
int j, ctr = 0;
System.out.print("Enter the string: ");
str = sc.nextLine();
System.out.print (" Enter the character which is to be searched: ");
inp = sc.next().charAt(0);
int len = str.length();
for (j = 0; j < len; j++)
{
ch = str.charAt(j);
if (ch == inp)
{
ctr++;
}
}
System.out.println(" Number of times the character " + inp + " is present in the String is: " + ctr);
}
}
Thanks!!
Rajdeep here!!
Here's your program using Scanner:
import java.util.*;
class Alpha
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
String str;
char ch, inp;
int j, ctr = 0;
System.out.print("Enter the string: ");
str = sc.nextLine();
System.out.print (" Enter the character which is to be searched: ");
inp = sc.next().charAt(0);
int len = str.length();
for (j = 0; j < len; j++)
{
ch = str.charAt(j);
if (ch == inp)
{
ctr++;
}
}
System.out.println(" Number of times the character " + inp + " is present in the String is: " + ctr);
}
}
Thanks!!
Similar questions
Economy,
8 months ago
India Languages,
8 months ago
Math,
8 months ago
Math,
1 year ago
Math,
1 year ago