asked 178k views
4 votes
write code that accepts two inputs from the user (a string, followed by an integer, n) that prints out the first n letters of the input string, followed by the last n letters of the input string (on the same line). you may assume that the integer, n, will not be greater than or equal to the length of the string. sample run: enter a string: dictionary enter a number: 1 dy hint - we saw in the lesson how to get the first letter of a string using the substring method. think how you could use the .length() string method to find the index of the last character, and how you could use this index in the substring method to get the last character.

asked
User Motoko
by
8.1k points

2 Answers

2 votes

Final answer:

To achieve the task, you can write code in Java that uses Scanner to handle user input and then applies the substring and length methods of the String class to print the first and last 'n' letters of the input string.

Step-by-step explanation:

To solve this problem, you can write a simple program in a language like Java. This program will use the substring method and the length method to achieve the desired results. Here is an example code snippet:

import java.util.Scanner;

public class StringManipulator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string:");
String inputString = scanner.nextLine();

System.out.println("Enter a number:");
int n = scanner.nextInt();

// Make sure to handle cases where n is not valid (negative or larger than string length)
String firstPart = inputString.substring(0, n);
String lastPart = inputString.substring(inputString.length() - n);

System.out.println(firstPart + lastPart);
}
}

This code will accept user input, extract the desired parts of the string using the substring and length methods, and then print them out concatenated.

answered
User Marurban
by
7.8k points
3 votes

Final answer:

To solve this problem, you can use the substring method in Java to get the first n letters of the input string and the last n letters of the input string.

Step-by-step explanation:

To solve this problem, you can use the substring method in Java to get the first n letters of the input string and the last n letters of the input string. Here is an example code:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
System.out.print("Enter a number: ");
int n = scanner.nextInt();

String firstNLetters = inputString.substring(0, n);
String lastNLetters = inputString.substring(inputString.length() - n);

System.out.println(firstNLetters + lastNLetters);
}
}

In this code, we use the Scanner class to get input from the user. We then use the substring method to extract the first n letters and the last n letters of the input string. Finally, we print out the combined result.

answered
User Keuminotti
by
8.8k points