Question

Java An int variable n contains a positive integer and a String variable s contains a...

Java

An int variable n contains a positive integer and a String variable s contains a String. Write the Java code needed to print "YES" if the first n characters of s appear more than once in s, and print "NO" otherwise.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class FirstNContains {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String line = in.nextLine();
        System.out.print("Enter a positive integer: ");
        int n = in.nextInt();
        String s = line.substring(0, n);

        boolean found = false;
        for (int i = 1; i < line.length(); i++) {
            if (line.substring(i).startsWith(s)) {
                found = true;
            }
        }
        if (found) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }

        in.close();
    }
}
Add a comment
Know the answer?
Add Answer to:
Java An int variable n contains a positive integer and a String variable s contains a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (25 points) Factorize Write a String method factorize(int n, int m) that when given a positive...

    (25 points) Factorize Write a String method factorize(int n, int m) that when given a positive integer n and an integer m, returns a String representation of how many times n factors into m. Must use a while loop to get full credit. (25 points) Print Product Table Write a void method printProductTable(int n, int m) that when given two positive integers n and m, prints an n (rows) by m (columns) product table. Note that entries in the table...

  • static public int[] Question() // retrieve an integer 'n' from the console // the first input...

    static public int[] Question() // retrieve an integer 'n' from the console // the first input WILL be a valid integer, for 'n' only. // then read in a further 'n' integers into an array. // in the case of bad values, ignore them, and continue reading // values until enough integers have been read. // this question should never print "Bad Input" like in lab // hint: make subfunctions to reduce your code complexity return null; static public int...

  • in Java please (a) Write a static method abbreviate( ) which is passed a String s...

    in Java please (a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters . . .). For example, if s is "Too bad Spongebob’s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too...

  • java : Given an int variable n that has been initialized to a positive value and,...

    java : Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.

  • Implement the function hasDuplicates. Input will be given as a line containing a positive integer n,...

    Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...

  • Write in Java Write code which checks validity of a 3-digit positive integer entered by the...

    Write in Java Write code which checks validity of a 3-digit positive integer entered by the user. The number is considered valid (true) if the sum of the first two digits is less than the last. Otherwise it is invalid (false).

  • Java Input Format For Custom Testing The first line contains an integer, n, that denotes the...

    Java Input Format For Custom Testing The first line contains an integer, n, that denotes the number of elements in s. Each line i of the n subsequent lines (where 0 si<n) contains a string that describes s[i]. Sample Case 0 Sample Input For Custom Testing 4 code aaagmnrs anagrams doce Sample Output aaagmnrs code Explanation aaagmnrs and anagrams are anagrams, code and doce are anagrams. After sorting aaagmnrs comes first. Sample Case 1 Sample Input For Custom Testing 4...

  • JAVA: What is the Big-O cost of the following code (n is a large positive integer)?...

    JAVA: What is the Big-O cost of the following code (n is a large positive integer)? int count = 0; for (int i = 1; i < n; i *= 2) {    for (int k = i; k <= i + 8; k ++ ) {        count ++;    } }

  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

  • write a Node.js function searchString() that receives three parameters: a positive integer in the range of...

    write a Node.js function searchString() that receives three parameters: a positive integer in the range of 1 to 20, a target string of characters of that length, and a single character. It then checks whether or not the target string contains the given character. If it does, this function returns the index of the target string where the character is first found. Otherwise, it returns -1.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT