I want to make a program that find the alphabets present in the string using java language.
e-g
i hava a string " Hello World "
It display me that charactes present in it are
H
e
l
0
w
r
d
It count a character only once.
I want to make a program that find the alphabets present in the string using java language.
e-g
i hava a string " Hello World "
It display me that charactes present in it are
H
e
l
0
w
r
d
It count a character only once.
only the alpha characters?
so H3ll0 Wor7D would print out:
H
l
l
W
o
r
D
that's easy enough to do using regular expressions.
the [a-zA-Z] searches for all characters within the range of a-zA-Z. You can then use the regexp to output the matches...etc
^ No, he's saying only to print out the character the first time it is seen.
RealThinkers, what you should do is make an array of 26 booleans, one for each letter in the alphabet. True means it has been seen, false means it hasn't. Then when you see a character, if it is a letter, check the corresponding index to see whether or not it has been seen yet. Make sure to set it to 'seen'/true after you see the char for the first time.
^ No, he's saying only to print out the character the first time it is seen.
RealThinkers, what you should do is make an array of 26 booleans, one for each letter in the alphabet. True means it has been seen, false means it hasn't. Then when you see a character, if it is a letter, check the corresponding index to see whether or not it has been seen yet. Make sure to set it to 'seen'/true after you see the char for the first time.
hah I had missed his
it count character only once
Or just step through and add every letter to a Set.
import java.io.*;
class CharAt
{
public static void main(String[] args){
int count=0;
try{
BufferedReader object=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String");
String s=object.readLine();
int len=s.length();
System.out.println(len);
for(int i=0;i<len;i++)
{
char char1=s.charAt(i);
if(char1=='a')
{
System.out.println(char1);
count++;
}
}
System.out.println("Toatal a----->" +count);
}
catch(Exception e){}
}
}
to check it is alphabet or number ,use this String function
Character.isLetter('A')
public class Stringtest {
public Stringtest() {
}
static String s2;
public static void main(String args[])
{
String input="My name is billa";
String buffer=new String();
String generated=new String();
boolean flag;
for(int i=0;i<input.length();i++)
{
flag=true;
for(int j=0;j<buffer.length();j++)
{
if(input.charAt(i)==buffer.charAt(j))
{
flag=false;
}
}
if(flag)
generated=generated+input.charAt(i);
buffer=buffer+input.charAt(i);
}
System.out.println(generated);
}
}
why using 26 booleans and waste memory..?
Store the string into an char array and check the repetition using a loop..
I don't think the OP is still working on this 2 years later. Closing.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.