IQ

Ad SpeedyAds

Thursday, March 8, 2012

Javaprogram to print the first and last letters of all words in a program

import java.io.*;
class abc
{
public static void main(Stringa aa[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a sentence");
String S=br.readLine();
S=S.trim();
S=" "+S+" ";
int a=1, L=S.length();
for(int i=1; i {
if(S.charAt(i)==32)
{
System.out.print(S.charAt(a));
if((i-1)!=a)
System.out.print(S.charAt(i-1));
System.out.print(" ");
a=++i;
}
}
}
}

2 comments:

  1. Thank you for the code

    The corrected version is here:


    import java.io.*;

    class abc
    {

    public static void main(String aa[]) throws IOException
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter a sentence");
    String S = br.readLine();
    S = S.trim();
    S = " " + S + " ";
    int a = 1, L = S.length();
    for (int i = 1; i <L;i++)
    {
    if (S.charAt(i) == 32)
    {
    System.out.print(S.charAt(a));
    if ((i - 1) != a)
    {
    System.out.print(S.charAt(i - 1));
    }
    System.out.print(" ");
    a = ++i;
    }
    }
    }
    }


    {my mail: voltaire101_5@yahoo.com}

    ReplyDelete
  2. [Simplest logic involvement]
    import java.io.*;
    class yo12345
    {
    void FirLast(String a)
    {
    String b[] = a.split(" ");
    String j;
    char f, l;
    for (int i = 0; i < b.length; i++)
    {
    j = b[i];
    f = j.charAt(0);
    l = j.charAt((j.length() - 1));
    System.out.println("First letter of word " + (i+1) + " is " + f+".");
    System.out.println("Last letter of word " + (i+1) + " is " + l+".");
    System.out.println();
    }
    }
    public static void main(String args[]) throws Exception
    {
    BufferedReader ob = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a string.");
    String k = ob.readLine();
    yo12345 d = new yo12345();
    d.FirLast(k);
    }
    }

    ReplyDelete