357 views
0 votes
0 votes

output:

Ssihtllun sillun ehtllun elbatllun

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
     Codechef c=new Codechef();
     String res=c.reverseword("this is the table");
     System.out.println(res);
    }
    String reverseword(String str)
    {   String test="";
        int count=1;
        //if(str.charAt(0)!=' ')
        //count++;
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)==' ')
            count++;
        }
        String[] arr=new String[count];
        int k=0;
        for(int j=0;j<str.length();j++)
        {
            if(str.charAt(j)!=' ')
            arr[k]=""+arr[k]+str.charAt(j);
            else
            k++;
        }
        for(int m=0;m<count;m++)
        {   
            test="";
            for(int p=arr[m].length()-1;p>=0;p--)
            {
                /*if(p==0 && (arr[0].charAt(p)>=92 && arr[0].charAt(p)<=122))
                {
                    test+=(char)(arr[m].charAt(p)-32);
                    break;
                }*/
                test+=arr[m].charAt(p);
            }
            arr[m] = test;
            
        }
        arr[0] = Character.toUpperCase(arr[0].charAt(0))+arr[0].substring(0, arr[0].length());
        String ans = "";
        for(int i=0;i<arr.length; i++){
            if(i>0) ans+=" ";
            ans+=arr[i];
        }
        return ans;
    }
    
}

1 Answer

0 votes
0 votes

try

if(str.charAt(j)!=' ')
            {
            if(arr[k] ==null)
            arr[k]=""+str.charAt(j);
            else
            arr[k]+=str.charAt(j);
            }
 
instead of
if(str.charAt(j)!=' ')
            arr[k]=""+arr[k]+str.charAt(j);

Related questions

0 votes
0 votes
1 answer
1
admin asked Apr 1, 2020
986 views
Is null an object?YesNoSometimes yesNone of these
0 votes
0 votes
1 answer
2
admin asked Mar 31, 2020
727 views
Earlier name of Java programming language was:OAKDNetbeanEclipse
0 votes
0 votes
1 answer
3
admin asked Mar 31, 2020
1,044 views
Which of the following is a platform free language?JAVACAssemblyFortran