954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Finding alphabets in a string

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.

real thinkers
Newbie Poster
1 post since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

^ 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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

^ 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 hisit count character only once

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

Or just step through and add every letter to a Set.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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" +count);
}
catch(Exception e){}
}
}

varungupta456
Newbie Poster
2 posts since Oct 2010
Reputation Points: 9
Solved Threads: 0
 

to check it is alphabet or number ,use this String function
Character.isLetter('A')

varungupta456
Newbie Poster
2 posts since Oct 2010
Reputation Points: 9
Solved Threads: 0
 
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);
       }
}
murali_quest
Newbie Poster
14 posts since Jul 2011
Reputation Points: 5
Solved Threads: 3
 

why using 26 booleans and waste memory..?

Store the string into an char array and check the repetition using a loop..

harinath_2007
Posting Whiz
355 posts since Aug 2010
Reputation Points: 78
Solved Threads: 36
 

I don't think the OP is still working on this 2 years later. Closing.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You