943,617 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3682
  • Java RSS
Mar 25th, 2009
0

Finding alphabets in a string

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
real thinkers is offline Offline
1 posts
since Dec 2008
Mar 25th, 2009
0

Re: Finding alphabets in a string

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
Last edited by Killer_Typo; Mar 25th, 2009 at 3:49 pm.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Mar 25th, 2009
0

Re: Finding alphabets in a string

^ 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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Mar 25th, 2009
0

Re: Finding alphabets in a string

^ 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

Quote ...
it count character only once
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Mar 25th, 2009
0

Re: Finding alphabets in a string

Or just step through and add every letter to a Set.
Last edited by Ezzaral; Mar 25th, 2009 at 10:17 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 30th, 2010
-2

try this code to print only alphabet

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){}
}
}
Reputation Points: 9
Solved Threads: 0
Newbie Poster
varungupta456 is offline Offline
2 posts
since Oct 2010
Oct 30th, 2010
0
Re: Finding alphabets in a string
to check it is alphabet or number ,use this String function
Character.isLetter('A')
Reputation Points: 9
Solved Threads: 0
Newbie Poster
varungupta456 is offline Offline
2 posts
since Oct 2010
Jul 22nd, 2011
-2
Re: Finding alphabets in a string
Java Syntax (Toggle Plain Text)
  1. public class Stringtest {
  2.  
  3. public Stringtest() {
  4. }
  5. static String s2;
  6. public static void main(String args[])
  7. {
  8. String input="My name is billa";
  9. String buffer=new String();
  10. String generated=new String();
  11. boolean flag;
  12. for(int i=0;i<input.length();i++)
  13. {
  14. flag=true;
  15.  
  16. for(int j=0;j<buffer.length();j++)
  17. {
  18. if(input.charAt(i)==buffer.charAt(j))
  19. {
  20. flag=false;
  21. }
  22. }
  23. if(flag)
  24. generated=generated+input.charAt(i);
  25.  
  26. buffer=buffer+input.charAt(i);
  27.  
  28. }
  29.  
  30. System.out.println(generated);
  31. }
  32. }
Reputation Points: 5
Solved Threads: 3
Newbie Poster
murali_quest is offline Offline
14 posts
since Jul 2011
Jul 22nd, 2011
0
Re: Finding alphabets in a string
why using 26 booleans and waste memory..?

Store the string into an char array and check the repetition using a loop..
Reputation Points: 78
Solved Threads: 33
Posting Whiz
harinath_2007 is offline Offline
318 posts
since Aug 2010
Jul 22nd, 2011
1
Re: Finding alphabets in a string
I don't think the OP is still working on this 2 years later. Closing.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: jtable problem...............
Next Thread in Java Forum Timeline: ResultSet to List<String>





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC