Question

In: Computer Science

Given a string str of even length, print out the first half.   Methods to use: substring(),...

Given a string str of even length, print out the first half.  

Methods to use: substring(), length()

Testing cases:

WooHoo => Woo

HelloThere   => Hello

abcdef => abc

ab   => a

0123456789   => 01234

kitten   => kit

(Note that the sample input test cases are shown before the “=>” and the expected output each test case is after the “=>”.

Your program should print out the expected output only, not the input.)


this is for java language

Solutions

Expert Solution

Source Program:

import java.io.*;
import java.util.Scanner;
class subb
{
   public static void main(String args[])
   {
       Scanner input=new Scanner(System.in);
       String str=input.next();
       int len=str.length();
       String str1=str.substring(0,len/2);
       System.out.println("\033[1A\033["+len+"C"+"=>"+str1);
   }
}

Sample Input and Output:


Related Solutions

use java recursion find # of times a substring is in a string but it has...
use java recursion find # of times a substring is in a string but it has to be the exact same, no extra letters attached to it and capitalization matters. input is 2 strings, output is an int input: ("Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello", "hello") should output: 3 input: (" Hello it is hello it's me hellos" + "hi hellllloooo hi hello" + "hello", "Hello") should output: 1 input: (" Hello...
use java recursion find # of times a substring is in a string but it has...
use java recursion find # of times a substring is in a string but it has to be the exact same, no extra letters attached to it and capitalization matters. input: school is boring with schooling and School substring: school output: 1
Write a C++ function to print out all unique letters of a given string. You are...
Write a C++ function to print out all unique letters of a given string. You are free to use any C++ standard library functions and STL data structures and algorithms and your math knowledge here. Extend your function to check whether a given word is an English pangram (Links to an external site.). You may consider your test cases only consist with English alphabetical characters and the character.
I'm working on python code which is substring matching. For example, the given string is input...
I'm working on python code which is substring matching. For example, the given string is input "CTTGTGATCTCGTGTCGTGGGTAG", and a substring we want to find in the main one is "GTGG". So the code will print out the position and the substrings in the main which has exactly one different position. For example, start at position 4 the substring is GTGA since there is only one letter different, and so on. Even though we know that string index starts at 0,...
Implement a substring search function which searches a string for a given keyword. The function should...
Implement a substring search function which searches a string for a given keyword. The function should return a pointer to the address of the first character of the substring within the original string, if it exists. If the substring is not found within the string, return NULL. If the input is found, print "Key found." otherwise print "Key not found." Your program’s input and output should match what is seen in the example run. • You may only use stdio.h....
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str)...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str) that returns the starting position of the first substring of the text that matches str. Return –1 if str is not a substring of the text. For example, s.indexOf("Mississippi", "sip") returns 6. Hint: You must keep track of how far the match is from the beginning of the text. Make that value a parameter variable of a helper method.
Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
Use this code to answer the following questions:           Object obj;           String str;           Song...
Use this code to answer the following questions:           Object obj;           String str;           Song sng = new Song("Rockstar", 5);           obj = sng;              // #7           sng = str;              // #8           str = (String) obj;     // #9           sng = obj;              // #10 1a)  Write the start of the class declaration for a node in a linked list (give the name of the class and the instance variables). The name of the node should be SpecialNode. The data in each SpecialNode will...
Java Prorgramming Skills Looping Branching Use of the length(), indexOf(), and charAt() methods of class String...
Java Prorgramming Skills Looping Branching Use of the length(), indexOf(), and charAt() methods of class String Use of the static Integer.toHexString method to convert a character to its ASCII hex value Description In this assignment, you'll be URL encoding of a line of text. Web browsers URL encode certain values when sending information in requests to web servers (URL stands for Uniform Resource Locator). Your program needs to perform the following steps: Prompt for a line of input to be...
What is the best running time taken to print the first character of a string of...
What is the best running time taken to print the first character of a string of length n? O(1) O(log n) O(n) O(n log n) O(n 2) O(n k) where k is a constant > 2 O(2 n) What is the best running time taken to print the letter 'A'. O(1) O(log n) O(n) O(n log n) O(n 2) O(n k) where k is a constant > 2 O(2 n) What is the best running time taken to determine the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT