Click here to Skip to main content
15,916,412 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to receive a String as its parameter and return the number of letters count in the String.

example:
String: "The price is $70."
Alphabets count: 10

this my code, but how to count only alphabets?

C#
public class MyRecursion
{
    //driver method
    public static void main(String[] args)
    {
        //Example of a string to test with isPalindrome
        String str1 = "Malam";

        if( isPalindrome(str1))
        System.out.println("Word: " + str1 + " is a Palindrome");
        else
        System.out.println("Word: " + str1 + " is NOT a Palindrome");
    }//end main

    //isPalindrome method
    public static boolean isPalindrome(String str)
    {
        if (str.length() <= 1)
        return true;
        else
        {
            str = str.toLowerCase();
            char first = str.charAt(0);
            char last = str.charAt(str.length()-1);
            if (first == last )
            return isPalindrome(str.substring(1, str.length()-1));
            else
            return false;
        }
    }//end isPalindrome method
}//end class
Posted
Updated 12-Sep-11 18:10pm
v3
Comments
CPallini 12-Sep-11 15:39pm    
Why should it be recursive (it smells of homework...)?
CPallini 12-Sep-11 17:48pm    
Your code actually checks recursively if the string is a palindrome. What's your problem with it?

I'm with CPallini - there is a strong sense of homework here. So, no code!

I wouldn't make this recursive - there is nothing here that requires recursion. Just loop through each of the characters in the string and - if it is alphabetic - add one to a count.

Simples!
 
Share this answer
 
Comments
TorstenH. 13-Sep-11 0:11am    
It's homework - that's why he's asking for recursion. I added a tag.
Recursion explained with examples[^]

The trick is to check the character if it is a int or char or something else.

You can accomplish the given task by trying to convert the character into a int - a NumberFormatException will appear if it is not a int. I think this is what your teacher has in mind.

Other solution (more elegant): convert the character into a int and check if it is within the range (ANSI Printable Characters[^] of a letter. Check the "Dec" (decimal) column on this table for reference.
 
Share this answer
 
v2
Comments
OriginalGriff 13-Sep-11 3:51am    
Why would you want to do any of that? You would get a NumberFormatException for '*',':',',', but they aren't alphabetic...
You would be better off not converting it at all, and using the Char.IsLetter method.
TorstenH. 13-Sep-11 4:32am    
It's a student's task. homework.
Nobody does really want to count the characters. It's just for the learning purpose. And therefor you need to do it recursive - just because the teacher said so.
OriginalGriff 13-Sep-11 4:40am    
No, I meant, "why would you want to convert the characters to anything else when you can process them perfectly happily, more easily, and more correctly as characters?" :laugh:
TorstenH. 13-Sep-11 5:57am    
...for the learning purpose? Did you figure out that this it's a student's homework?
You would do well to look here[^] at the Java documentation. Then, perhaps, using the index link, try looking at functions beginning with "is".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900