i need to study ur code for the issues u wanted to explain. rather one suggestion if you are using java then why dont u utilize string.split() api ....
On Fri, Mar 23, 2012 at 11:13 PM, Registered user <[email protected]>wrote: > yes as Amahdy said : > > > > > just introduce one more line n ur program will run correctly > > > > > it works.... someone plz explain why it is so.... > > > > > > > > > import java.util.Scanner; > > > > > /** > * > * @author Pardeep > */ > public class Reverse { > public void test(){ > int n=0,l=0,i=0; > int j=0,c=0; > Scanner sc=new Scanner(System.in); > n=sc.nextInt(); //count no of test > cases > while(n>0){ > String strn=""; > String str1=sc.nextLine(); > String str=sc.nextLine(); > //String str=sc.nex > > > > > l=str.length(); > String[] ar=new String[l]; > j=0; > int count=0; > for(i=0;i<str.length();i++){ //count no of blank > spaces in string > if(str.charAt(i)==' '){ > count++; > } > } > i=0; > for(i=str.length()-1;i>=0;i--){ //reverse the string > ar[j]=String.valueOf(str.charAt(i)); //store the value in > array ar[] > //System.out.println("while.."+ar[j]+j); > j+=1; //then increase > value in array element > if(str.charAt(i)==' '){ //if space occur > in string then pop up all array element and add in string variable > strn > for(int k=j-1;k>=0;k--){ > strn+=ar[k]; > } > j=0; // j is again > set to j=0 > count--;//System.out.println(count); //count is > decremented > } //now we have > reversed the string and store it in string (strn),but not the first > word of string > if(count==0){ //if count > is zero then > strn+=" "; //space is > added to string > for(int m=0;m<=i;m++){ //variable > m is initialised to 0 and upto i > strn+=str.charAt(m); //store > each char in the string (strn) > } > i=-1; > } > }c++; > System.out.println("Case #:"+c +strn); > //Error:-output is printed (but getting space at first position) > > > > > n--; > } > > > > > } > public static void main(String a[]){ > new Reverse().test(); > } > } > > > On Mar 23, 10:02 pm, Amahdy <[email protected]> wrote: > > I think your problem is here: > > > > n = sc.nextInt(); > > > > You read the integer (the count) but still at the same line, so you need > to > > move one more line by typing: > > > > sc.nextLine(); > > > > After it. > > > > I'm not sure if your code is correct though. > > > > -- AMahdy!www.amahdy.net > > > > > > > > > > > > > > > > On Wed, Mar 21, 2012 at 14:20, mandy <[email protected]> wrote: > > > here is a link to the problem... > > >http://code.google.com/codejam/contest/351101/dashboard#s=p1 > > > Input:-this is a test > > > Output:-test a is this > > > I'm getting error in output and in while loop is not executing > > > properly... > > > So pls tell me what's wrong with this code... > > > Thanks a lot. > > > > > Mine solution is this.. > > > > > /* > > > * To change this template, choose Tools | Templates > > > * and open the template in the editor. > > > */ > > > package frame; > > > > > import java.util.Scanner; > > > > > /** > > > * > > > * @author Pardeep > > > */ > > > public class Reverse { > > > public void test(){ > > > int n=0,l=0,i=0; > > > int j=0,c=0; > > > Scanner sc=new Scanner(System.in); > > > n=sc.nextInt(); //count no of test > > > cases > > > while(n>0){ > > > String strn=""; > > > String str=sc.nextLine(); > > > l=str.length(); > > > String[] ar=new String[l]; > > > j=0; > > > int count=0; > > > for(i=0;i<str.length();i++){ //count no of blank > > > spaces in string > > > if(str.charAt(i)==' '){ > > > count++; > > > } > > > } > > > i=0; > > > for(i=str.length()-1;i>=0;i--){ //reverse the string > > > ar[j]=String.valueOf(str.charAt(i)); //store the value > > > in array ar[] > > > //System.out.println("while.."+ar[j]+j); > > > j+=1; //then increase > > > value in array element > > > if(str.charAt(i)==' '){ //if space occur > > > in string then pop up all array element and add in string variable > > > strn > > > for(int k=j-1;k>=0;k--){ > > > strn+=ar[k]; > > > } > > > j=0; // j is again > > > set to j=0 > > > count--;//System.out.println(count); //count is > > > decremented > > > } //now we > > > have reversed the string and store it in string (strn),but not the > > > first word of string > > > if(count==0){ //if count > > > is zero then > > > strn+=" "; //space is > > > added to string > > > for(int m=0;m<=i;m++){ //variable > > > m is initialised to 0 and upto i > > > strn+=str.charAt(m); //store > > > each char in the string (strn) > > > } > > > i=-1; > > > } > > > }c++; > > > System.out.println("Case #:"+c > > > +strn); //Error:-output is printed (but getting > > > space at first position) > > > > > n--; > > > } > > > > > } > > > public static void main(String a[]){ > > > new Reverse().test(); > > > } > > > } > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Google Code Jam" group. > > > To post to this group, send email to [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]. > > > For more options, visit this group at > > >http://groups.google.com/group/google-code?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Google Code Jam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Google Code Jam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
