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 L=S.length(),a=L;
for(int i=L-1; i>=0; i--)
{
if(S.charAt(i)==32)
{
System.out.print(S.substring(i+1,b)+" ");
b=i;
}
}
}
}
There are some errors... I am trying to fix them.
ReplyDeleteHere is the corrected code....
ReplyDeleteimport 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 L = S.length(), a = L;
for (int i = L - 1; i >= 0; i--)
{
if (S.charAt(i) == 32)
{
System.out.print(S.substring(i + 1, a) + " ");
a = i;
}
}
}
}
I am the corrector of the above code
Delete{my mail: voltaire101_5@yahoo.com}
better use the split() function in java.util (it is in course)
ReplyDelete[writing only the function]
void invert(String a)
{
String b[] = a.split(" ");
String c = " ";
for (int i = b.length-1; i >= 0; i--)
{
c += b[i];
if (i <= b.length-1) c += " ";
}
c = c.trim();
System.out.println("The inverted string is: ");
System.out.println(c);
}
http://thecprojectt.blogspot.in for ISC JAVA Pgms and much more.
ReplyDelete