Respuesta :
Answer:
s = 'hello world';
Wanted = swap(s) % function call
% function
function Wanted = swap(str)
S = regexp(str,'\S*','match');
S([1,end]) = S([end,1]);
Wanted = strjoin(S);
end
or better still
% Create sample string.
str = 'one two three four five';
% Split apart into individual words without spaces.
words = strsplit(str)
% Swap the first and last words.
[words(end), words(1)] = deal(words(1), words(end))
% String the words together with spaces between them.
outputString = ''; % Initialize output
for k = 1 : length(words)
outputString = sprintf('%s ', outputString, words{k});
end
% Trim off the leading and trailing spaces.
outputString = strtrim(outputString)
Explanation:
all codes are written in matlab programming language.
Answer:
// Scanner class is imported to allow program
// receive user input
import java.util.Scanner;
// class Solution is defined
public class Solution {
// main method that begin program execution
public static void main(String args[]) {
// Scanner object scan is defined
// to receive input via the keyboard
Scanner scan = new Scanner(System.in);
// prompt is display to user to enter two word
System.out.println("Enter your two-word: ");
// user input is assigned to user_input
String user_input = scan.next();
// user_input is splitted using space as separator
String[] splitted_input = user_input.split(" ");
// first word from the array is assigned to first_word
String first_word = splitted_input[0];
// second word from the array is assigned to second_word
String second_word = splitted_input[1];
// first character of first_word is assigned to temp
char temp = first_word.charAt(0);
// new_first_word is form by concatenating first character of second_word
// and substring of first_word
String new_first_word = second_word.charAt(0) + first_word.substring(1);
//new_second_word is form by concatenating the temp and
// substring of second_word
String new_second_word = temp + second_word.substring(1);
// the new words are displayed to the user
System.out.println("The new word is: \n" + new_first_word + " " + new_second_word);
}
}
Explanation:
The code is written in Java and well commented.