Interview Question
Country: Bangladesh
Interview Type: Written Test
1. Perform in-place reversal of the original string.
e.g. "How arE you doinG today" => "yadot Gniod uoy Era woH"
Note that the words to be removed now have their first character in upper case.
2. Scan the resulting reversed string and reverse each term (word) in place, while
skipping those starting with upper case.
At this point, you will need to store the position of the last delimiter
encountered so far, because as soon as you skip any word starting with the
upper case, you will need to move it (copy over) the next valid word to the
position following the last encountered whitespace (space in this example)
The above example will result in the following string "today you How"
I some how missed that part - the "reverse the characters of each word". The problem is now even easier with my algo - at step #2, just do not do the in-place reversal of each word. Thanks for noting that, pal.
public class ReverseWords {
public static String removeIfEndsWithCapitalAndReverseOther(String query)
{
String[] words = query.split(" ");
String result = "";
for(int i=0; i<words.length; i++)
{
if(!isCapital(words[i].charAt(words[i].length()-1)))
{result = result + " "+ reverse(words[i]);}
}
return result;
}
public static boolean isCapital(char c)
{
return (c>='A' && c<='Z');
}
public static String reverse(String word)
{
String result="";
for(int i=word.length()-1; i>=0;i--)
{
result = result+ word.charAt(i);
}
return result;
}
public static void main(String[] args)
{
String query = "My attitude dependS on you";
String result = removeIfEndsWithCapitalAndReverseOther(query);
System.out.println(result);
}
}
package com.test.me;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class FindCaps {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader bufread = new BufferedReader(new InputStreamReader(System.in));
try {
String inp = bufread.readLine();
String[] str = inp.split(" ");
ArrayList<String> strArray = new ArrayList<String>();
ArrayList<String> newStrArray = new ArrayList<String>();
for(int i=0;i<str.length;i++){
char c = str[i].charAt(str[i].length()-1);
if(c>= 'A' && c<='Z'){
strArray.add(str[i]);
// System.out.print(str[i]);
}
}
System.out.print(strArray.size());
for(int i=strArray.size()-1;i>=0;i--){
String s = new StringBuffer(strArray.get(i)).reverse().toString();
newStrArray.add(s);
}
for(int k=0;k<newStrArray.size();k++){
System.out.print(newStrArray.get(k));
System.out.print("\t");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.util.*;
public class Captal1 {
public static void main(String[] args) {
String s;
String n;
char c;
char ch[]=new char[10];
int l;
Scanner sc=new Scanner(System.in);
System.out.print("enter the given string");
s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s," ");
while(st.hasMoreElements())
{
n=st.nextElement().toString();
//System.out.print(n);
l=n.length();
c=n.charAt(l-1);
//System.out.print(c);
if(!Character.isUpperCase(c))
{
ch=n.toCharArray();
for(int i=l-1;i>=0;i--)
System.out.print(ch[i]);
System.out.print(" ");
}
}
}
}
sample o/p:enter the given stringhello ram raki kittU keerthi
olleh mar ikar ihtreek
What I understand from this code is if input is "I am indiaN" then output should be "I ma". Now to solve this problem we can think of following algorithm=>
- sachinagarwala251 November 22, 2012scan the sentence and find words(word delimiter is white space)
For each word check last character is capital or not, if yes then remove the word otherwise reverse characters of the word.
repeat the process until all words are completed