Write a program to read a string and a positive integer n as input and construct a string with first n and last n characters in the given string. The return type of the output should be a string (value) of the first n character and last n character.
Input and Output Format
- Input consists of a string of even length.
- The output is a string.
Note: The given string length must be >=2n.
Refer sample output for formatting specifications
Sample Input 1:
California
3
Sample Output 1:
Calnia
Sample Input 2:
this
1
Sample Output 2:
Ts
Forming New Word from a String in Java.
Following are the steps to form a new word from a given string:
- Input string from the user and a positive integer n.
- Call the subStringOfgivenString() method.
- Inside the method, multiply the number to 2 and store it in a variable n.
- Now, create a new string. Now, check if the length of the input string is greater than the value in variable n then add the substring of the string of the first n character with the last n characters.
- At last, return the string.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); int n1 = sc.nextInt(); System.out.println(subStringOfgivenString(s1,n1)); } public static String subStringOfgivenString(String s1, int n1) { int n = 2*n1; String s = new String(); if(s1.length()>n) { s = s1.substring(0,n1) + s1.substring(s1.length()-n1, s1.length()); return s; } else return null; } }
Output
Count Number of words in a string
Write a program to count the number of words in a string. Here, consider the sentence “Old is gold” there is a total of 3 words in this sentence. So, the program should print 3.
Input and Output Format
- Input consists of a string.
- The output is an integer.
Refer sample output for formatting specifications
Sample Input 1:
Old is gold
Sample Output 1:
3
Program to count words in a string in Java
Following are the steps to count how many words are there in a sentence:
- Input a string from the user. Declare and initialize the count variable to 0.
- extract words from the string and increment count value.
- At last, print the value of count.
package com.company; import java.util.Scanner; public class Solution4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int count = 0; String str = scanner.nextLine(); for(String word:str.split(" ")){ count++; } System.out.println(count); } }
Output
Combine two String and Form a new word
Write a program to form a string from two strings by combining the character of the first string from left to right and of the second string from right to left. Note: Assume the length should be the same.
Input and Output Format
- Input consists of two strings.
- The output is a string.
Refer sample output for formatting specifications
Sample Input 1:
English
Marathi
Sample Output 1:
EinhgtlairsahM
Program to Combine two String and Form a new word
- Input two strings from the user.
- Call combineChar() method with input strings.
- Inside the method, declare an empty string.
- Iterate over the first string, extract each character of the first string, and store it in the c1 variable. Extract the character of the second string from the last position and store it in c2 variable.
- Now, add both the character from c1 and c2 and store it in the new string created.
- At last, just return the newly formed string.
package com.company; import java.util.Scanner; public class Solution5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String first = scanner.next(); String second = scanner.next(); System.out.println(combineChar(first,second)); } private static String combineChar(String first, String second) { String third = ""; for (int i =0; i < first.length();i++){ char c1 = first.charAt(i); char c2 = second.charAt(first.length() - 1- i); third = third + c1 + c2; } return third; } }
Output
Thus, in this way, we learn the few ways we can combine words in a string.