English

Print the last letter of each word.

Advertisements
Advertisements

Question

Print the last letter of each word.

Code Writing
Advertisements

Solution

import java.util.*;
class clString
{
    public void main()
    {
        Scanner sc = new Scanner(System.in);
        String ts = "";
        System.out.print("Enter the string: ");
        ts = sc.nextLine();
        StringTokenizer stoken = new StringTokenizer(ts);
        int t1 = stoken.countTokens();
        for(int c = 1; c <= t1; c++)
        {
            String word = stoken.nextToken();
            int wlen = word.length();                       
            System.out.print(" " + word.charAt(wlen - 1));
        }
    }
}

Output:

Enter the string : Success comes to those who believe in it 

s s o e o e n t

shaalaa.com
  Is there an error in this question or solution?
Chapter 11: Programs Overall - String Handling [Page 252]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 11 Programs Overall
String Handling | Q 2. | Page 252
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×