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.

Recommended Answers

All 9 Replies

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){}
  }
}
commented: bumped a year old thread with a weak solution. -1

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);
       }
}
commented: Why did you resurrect this old thread to post this awful piece of code? -3
commented: bad code, not following naming conventions and as usefull as I am in the kitchen .. -2

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.

commented: if he is working on this 2yrs later there might be something wrong! :) +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.