Function Name: acronym % Inputs (1): - (string) a 1xM string containing a phrase
ID: 3541752 • Letter: F
Question
Function Name: acronym
% Inputs (1): - (string) a 1xM string containing a phrase
% Outputs (1): - (string) A 1xN string containing an acronym of the given
% phrase
%
% Function Description:
% Write a function called "acronym" that takes in a phrase and outputs
% an acronym of the phrase. The output should contain only the first
% letter of every word and only if the first letter is capitalized.
% There will always be a phrase, and the phrase can contain any number of
% words. The phrase will always produce a valid acronym.
%
% Example:
% input: 'Boy Scouts of America'
% output: 'BSA'
%
% Note:
% You must use logical indexing to solve this problem.
%
%
% Test Cases:
% out1 = acronym('Game of Thrones');
% => 'GT'
%
% out2 = acronym('You Obviously Like Owls');
% => 'YOLO'
%
% out3 = acronym('Time And Relative Dimension In Space');
% => 'TARDIS'
Explanation / Answer
public class acronym {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.nextLine();
String str1[] = str.split(" ");
for (int i = 0; i < str1.length; i++) {
if (Character.isAlphabetic(str1[i].charAt(0))) {
if (Character.isUpperCase(str1[i].charAt(0))) {
System.out.print(str1[i].charAt(0));
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.