Interview Question
Country: India
Interview Type: In-Person
from this question not getting full data.can u explain with sum more data or tell me that Sting contain only two words or many words .if it contain more then two words then what is the sequence of output string .
private String reverseString(String input){
StringBuilder str = new StringBuilder();
int index = 0;
int temp_index=0;
boolean bfirstword = false;
// reverse last word
for (int i = input.length() - 1; i > 0; i--) {
str.append(input.charAt(i));
if (input.charAt(i) == ' ') {
index = i;
break;
}
}
// now reverse string other than last word
while (index > 0) {
temp_index = index;
index = index-1;
bfirstword = true;
for (int i = index; i > 0; i--) {
if (input.charAt(i) == ' ') {
index = i;
bfirstword = false;
break;
}
}
if (bfirstword) {
for (int j = 0; j < temp_index+1; j++) {
str.append(input.charAt(j));
}
break;
} else {
for (int j = index + 1; j < temp_index+1; j++) {
str.append(input.charAt(j));
}
}
}
return str.toString();
}
public class CarrerCup1 {
public static void main(String[] args) {
StringBuilder s=new StringBuilder("Welcome Bangalore");
String str="";
for(int i=(s.length()-1);i>=0;i--)
{
if(s.charAt(i)==' '){
str=str+s.charAt(i);
for(int j=0;j<i;j++){
str=str+s.charAt(j);
}
i=0;
}
else{
str=str+s.charAt(i);
}
}
System.out.println(str);
}
}
import java.util.Scanner;
public class TestProgram {
public static final String SPACE = " ";
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
String inString = input.nextLine();
System.out.println(new StringBuilder(inString.split(SPACE)[1]).reverse() + SPACE + inString.split(SPACE)[0]);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null)
try {
input.close();
} catch (Exception e2) {
// ignore
}
}
}
}
import java.util.Scanner;
public class TestProgram {
public static final String SPACE = " ";
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
String inString = input.nextLine();
System.out.println(new StringBuilder(inString.split(SPACE)[1]).reverse() + SPACE + inString.split(SPACE)[0]);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null)
try {
input.close();
} catch (Exception e2) {
// ignore
}
}
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50] , revstr[50];
int i=0,j=0;
printf("enter the string for reversing:");
scanf("%s", str);
for(i=strlen(str)-1;i>=0;i--)
{
revstr[j] = str[i];
j++;
}
revstr= "\0";
printf("input string %s :" ,str);
printf("reversed string %s:", revstr);
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50] , revstr[50];
int i=0,j=0;
printf("enter the string for reversing:");
scanf("%s", str);
for(i=strlen(str)-1;i>=0;i--)
{
revstr[j] = str[i];
j++;
}
revstr= "\0";
printf("input string %s :" ,str);
printf("reversed string %s:", revstr);
}
}
import java.util.Scanner;
public class TestProgram {
public static final String SPACE = " ";
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
String inString = input.nextLine();
System.out.println(new StringBuilder(inString.split(SPACE)[1]).reverse() + SPACE + inString.split(SPACE)[0]);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null)
try {
input.close();
} catch (Exception e2) {
// ignore
}
}
}
}
public class Sreverse{
public static void main(string[] args)
{
string s="Welcome Banglore";
Char[] arr= s.toCharArray();
//reverse string
Srevers.reversefunction(arr,0,arr.lenght/2,arr.length);
System.out.println(new string(arr));
}
public static void reversefunction(char[] arr, i,m,l)
{
for(;i<m;i++)
{
char temp=arr[i];
arr[i]=arr[l];
arr[l]=temp;
l--;
}
}
}
public class ReverseWordsInStrings {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Welcome Bangalore Ssk";
String []iStr = str.split(" ");
for(int j = iStr.length - 1; j >=0; j--){
String s = iStr[j];
String rev = "";
if((iStr.length - j) % 2 == 0)
rev = s;
else
for(int i = s.length() - 1; i >= 0; i--){
rev += s.charAt(i);
}
System.out.print(rev + " ");
}
}
}
class Rev
{
public static void main(String[] args)
{
String str="Welcome Banglore";
String str1;
str1=str.substring(str.indexOf("Banglore"));
StringBuffer str2=new StringBuffer(str1);
System.out.println(str2.reverse()+" "+str.substring(str.indexOf("Welcome"),str.indexOf("Banglore")));
}
}
package com.lakshma;
public class StringReverseExample {
public static void main(String[] args) {
String str = "WELCOME BANGALORE";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
Character ch = str.charAt(i);
if (ch.toString().equals(" ")) {
System.out.print(sb.toString());
sb = new StringBuffer();
} else {
sb.append(ch);
}
}
System.out.print(" " + sb.reverse().toString());
}
}
package com.lakshma;
public class StringReverseExample {
public static void main(String[] args) {
String str = "WELCOME BANGALORE";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
Character ch = str.charAt(i);
if (ch.toString().equals(" ")) {
System.out.print(sb.toString());
sb = new StringBuffer();
} else {
sb.append(ch);
}
}
System.out.print(" " + sb.reverse().toString());
}
}
Reverse the whole sentence first. Then leave the first word and reverse the second word. If the question is really asking to have this pattern of reversed word and a non-reversed word, continue this processing of reversing alternate words till the end.
- Murali Mohan June 30, 2014