top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Given two strings, append them together and return the result in Java? Conditions are ...

+2 votes
3,866 views

Given two strings, append them together (known as "concatenation") and return the result. However, if the concatenation creates a double-char, then omit one of the chars. If the inputs are "Mark" and "Kate" then the output should be "markate". (The output should be in lowercase).

posted Apr 15, 2017 by Prajwal C.m.

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
your questions are always making a confusion with data types.Make sure about correct ques statement and here is : if the last char of first word and first char of second word are same then omit one character.
try if(wordOne.charAt(wordOne.length()-1).toLowerCase() == wordTwo.charAt(0).toLowerCase())
print(wordOne.subString(0 to n-2).LowerCase()+wordTwo.toLowerCase())
else
print(wordOne.toLowerCase()+ wordTwo.toLowerCase())

1 Answer

+1 vote

Try this code

String str1; //First string
String str2; //Second string
if( str1.substring( str1.length()-1).equalsIgnoreCase( str2.substring(0,1)) )
                    System.out.println(  str1.concat( str2.substring(1, str2.length())).toLowerCase() );
                else
                    System.out.println(  str1.concat( str2) );
answer Apr 18, 2017 by Alivia Clark
Similar Questions
+2 votes

Given 2 strings, a and b, return a string of the form shorterString+longerString+shorterString, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0). If input is "hi" and "hello", then output will be "hihellohi".

+2 votes

Given a set of random strings, write a function that returns a set that groups all the anagrams together.

Ex: star, dog, car, rats, arc - > {(star, rats), (arc,car), dog}

+2 votes

Rohit wants to add the last digits of two given numbers.
For example, If the given numbers are 267 and 154, the output should be 11.

Below is the explanation -
Last digit of the 267 is 7
Last digit of the 154 is 4
Sum of 7 and 4 = 11

Write a program to help Rohit achieve this for any given two numbers.

Note: The sign of the input numbers should be ignored. i.e.
if the input numbers are 267 and 154, the sum of last two digits should be 11
if the input numbers are 267 and -154, the sum of last two digits should be 11
if the input numbers are -267 and 154, the sum of last two digits should be 11
if the input numbers are -267 and -154, the sum of last two digits should be 11

...