Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to print the number of palindrome words in a sentence?

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to print the number of palindrome words in a sentence?

    public static void main(String args[]){
    Scanner sc= new Scanner(System.in);
    System.out.println("INPUT:\t");
    String str;
    int count=0;
    str=sc.nextLine();
    str=str.replace("."," ");
    str=str.replace("?"," ");
    str=str.replace("!"," ");
    String answer="", ar[]= str.split(" ");
    for(int i=0;i < ar.length;i++){
    if(isPalindrome(ar[i])){
    count++;
    answer += ar[i] + " ";
    }
    }
    System.out.println("OUTPUT:\t\t"+answer+"\nNUMBER OF PALINDROMIC WORDS: "+count);
    }
    public static boolean isPalindrome(String str){
    char ch;
    int len=str.length(),half=len/2;
    for(int i=0;i < half;i++){
    if(str.charAt(i)!=str.charAt(len-i-1)) return false;
    }
    return true;
    }


    If we are not allowed to use split() function, then how should we proceed?

  2. #2
    Junior Member
    Join Date
    Sep 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to print the number of palindrome words in a sentence?

    You can code your own split() function for this task if the provided one is not allowed. You can split a string into it's words by iterating through the characters, adding them to a temporary string, adding this temporary string to a list when you hit a " " and repeating afterwards. It may look like this:

    import java.util.List;
    import java.util.ArrayList;
     
    ...
     
    public static String[] split(String s) {
    	List<String> output = new ArrayList<String>();
    	String word = "";
    	for (char c : s.toCharArray()) {
    		if (c == ' ') {
                            if(word.length() > 0)
    			output.add(word);
    			word = "";
    		} else
    			word += c;
    	}
    	output.add(word);
    	String[] array = new String[output.size()];
    	array = output.toArray(array);
    	return array;
    }

Similar Threads

  1. Replies: 2
    Last Post: February 10th, 2014, 06:24 AM
  2. Replies: 6
    Last Post: October 26th, 2013, 01:59 PM
  3. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  4. Can't figure out how to print out number of lines and words in a file in c++
    By javapenguin in forum Other Programming Languages
    Replies: 1
    Last Post: January 29th, 2012, 07:53 PM
  5. Arranging words in a sentence in alphabatical order.
    By J2000 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 24th, 2011, 10:04 AM