Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

output is not coming please help

 
harish thiru
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for help but output is not coming though no runtime exceptions

import java.util.*;
public class Rev {
static void rev(String jik){
int i,j;

char one[]=jik.toCharArray();
char two[]=new char[one.length];
for(i=0,j=one.length-1;i<one.length&&j>0;i++,j--){
two[i]=one[j];
}
String b = new String(two);

System.out.print(b+" ");
}
public static void main(String[] arg){
int i,start,fin;
start=0;

String str;
Scanner in = new Scanner(System.in);
str=in.nextLine();
for(i=0;i<str.length();i++){
if(str.charAt(i)==' '){
fin=i-1;
String ut=str.substring(start, fin);
rev(ut);
start=i+1;

}


}
}
}


>
 
harish thiru
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oooh i am sorry i just read posting rules actually i thought they won't look after two replies i was still having problem so i again posted

i had help and now i revised my code fin=i; and j>=0;

java is good programming language ----- sample input

avaj si doog gnimmargorp ---- output

i am having problem in reversing last word please help

import java.util.*;
public class Rev {
static void rev(String jik){
int i,j;

char one[]=jik.toCharArray();
char two[]=new char[one.length];
for(i=0,j=one.length-1;i<one.length&&j>=0;i++,j--){
two[i]=one[j];
}
String b = new String(two);

System.out.print(b+" ");
}
public static void main(String[] arg){
int i,start,fin;
start=0;

String str;
Scanner in = new Scanner(System.in);
str=in.nextLine();
for(i=0;i<str.length();i++){
if(str.charAt(i)==' '){
fin=i;
String ut=str.substring(start,fin);
rev(ut);
start=i+1;

}


}
}
}


>
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is, making it less likely that people will take the time to do so.

if (str.charAt(i)==' ') {

This if condition is too strict. The body should also be executed when the end of the input string is reached. You'll possibly need to adapt indexes in that case.
 
harish thiru
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks dittmer i got it but my code looks very shabby any better way i could do it

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic