Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (8 votes)
See more:
write a program to reverse a string except last and first two characters
Posted
Updated 7-Sep-11 19:32pm
v2
Comments
AspDotNetDev 7-Sep-11 16:39pm    
Hello Rameswara. I have just sent your teacher a link to this question.
TorstenH. 8-Sep-11 1:33am    
added homework tag

1 solution

Java
String source = "Hello world";
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);

for (i = (len - 1); i >= 0; i--)
{
  if (i == 0)
  {
    dest.append(source.charAt(len - 1));
  }
  else
  {
    if (i == (len - 1))
    {
      dest.append(source.charAt(0));
    }
    else
    {
      dest.append(source.charAt(i));
    }
  }
}

System.out.println(dest.toString());
 
Share this answer
 
Comments
TorstenH. 8-Sep-11 1:34am    
we don't do homework Prerak. There is no effort from this - beside of keeping someone from learning to code.
Prerak Patel 8-Sep-11 2:08am    
Yes I agree. Most of the time I vote them 2 and move ahead.
But being a .Net programmer, it's been a long seeing java, so just curiously I was checking how can we do that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900