Write a java program to find the first and last word in the sentence. Write a program to read a string containing multiple words find the first and last words, if they are the same, return the length, and if not return the sum of the length of the two words.
The return type is the length as per the problem.
Note: Create a Class Main which would be used to accept the string and call the static method present in UserMainCode.
Input and Output Format:
- Input consists of a string.
- Output consists of an integer.
Refer sample output for formatting specifications:
Sample Input 1:
This is demo demo
Sample Output 1:
8
Sample Input 2:
Hello World Hello
Sample Output 2:
10
Java program to find the first and last word in the sentence
Following are the steps to find the length of the first and last word:
- Declare and initialize a string “this is demo demo”.
- Create an empty array list.
- Break the string into tokens by separating it with the space by using the StringTokenizer class.
- Now, get each token from the string and add it to the list.
- Now, store the first and last element of the list in the strings s3 and s4.
- Check if the value in strings s3 and s4 are equal. If they are equal, then get the length and print it else return the sum of the length of the two words.
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub String s1="this is demo demo"; List<String> l=new ArrayList<String>(); StringTokenizer t=new StringTokenizer(s1," "); while(t.hasMoreTokens()) { String s2=t.nextToken(); l.add(s2); } String s3=l.get(0); String s4=l.get(l.size()-1); if(s3.equals(s4)) { int n=s3.length(); System.out.println(n); } else { int n1=s3.length(); int n2=s4.length(); System.out.println(n1+n2); } } }
Output
Swap First and Last Characters of Word in the String in Java
Write a java program to swap the first and last character of a word in the sentence. Write a program to read a string containing multiple words find the first and last character of words, and swap them.
Input and Output Format:
- Input consists of a string.
- Output consists of a string.
Refer sample output for formatting specifications:
Sample Input 1:
This is demo demo
Sample Output 1:
shit si oemd oemd
Sample Input 2:
Hello World Hello
Sample Output 2:
oellH dorlW oellH
Following are the steps to swap words in a string:
- Declare a string and initialize it. Next, convert the string into a character array by using the toCharArray() method.
- Now, we will iterate the character array by using for loop.
- Inside the loop, initialize the last variable with the index element.
- If an alphabet is found then increase the value of i and whenever we reach at space, then swap the first and last character of the word.
package com.demo; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class FirstAndLastWord { public static void main(String[] args) { String s1 = "Hello World Hello"; char c[] = s1.toCharArray(); for(int i = 0;i<c.length;i++){ int last = i; while (i < c.length && c[i] != ' ') i++; char temp = c[last]; c[last] = c[i - 1]; c[i - 1] = temp; } System.out.println(new String(c)); } }
Output
Thus, in this way, we learn how to find the length of the first and last word in the string.