Question

In: Computer Science

Given an int variable k, write an expression whose value is the k-th character in the String variable word.


1. Given an int variable k, write an expression whose value is the k-th character in the String variable word. Assume thatword has been initialized and that the value ofk is non-negative and less than the length ofword.

  • Example 1: if k's value is 3 andword is "spring", the value of the expression would be 'i'.

  • Example 2: if k's value is 0 andword is "fall", the value of the expression would be 'f'.

2. Given an int variable k, write an expression whose value is character that immediately preceds the k-th character in the String variableword. Assume that word has been initialized and that the value of k is positive and less than the length of word.

  • Example 1: if k's value is 3 andword is "spring", the value of the expression would be 'r'.

  • Example 2: if k's value is 1 andword is "fall", the value of the expression would be 'f'.

3. Given a String variablename. that has been initialized to value whose length is 2 or more, write an expression that istrue if an only if the first two character of theString are identical.

  • Example 1: if name is "bob", the value of the expression would be false.

  • Example 2: if name is "oona", the value of the expression would be true.

4. Given a String variablename. that has been initialized to value that is not empty, write an expression that is true if an only if the first and the last characters of theString are identical.

  • Example 1: if name is "bob", the value of the expression would be true.

  • Example 2: if name is "oona", the value of the expression would be false.

5. Given a String variablename. that has been initialized to value that is not empty, write an expression that is true if an only if the the last two characters of the Stringare identical.

  • Example 1: if name is "Roo", the value of the expression would be true.

  • Example 2: if name is "Kanga", the value of the expression would be false.

Solutions

Expert Solution

1.

import java.util.*;

class chegg {

    public static void main(String[] args) {

        // EXPRESSION 1st

        String word = "spring";

        int k = 3;

        char value = word.charAt(k); // SIMPLY USING THE charAt inbuilt to get the Kth value

        System.out.println(value);

    }

}

2.

import java.util.*;

class chegg {

    public static void main(String[] args) {

        // EXPRESSION 2nd

        String word = "spring";

        int k = 3;

        char value = word.charAt(k - 1);  // SIMPLY USING THE charAt inbuilt to get the K-1th value

        System.out.println(value);

    }

}

3.

import java.util.*;

class chegg {

    public static void main(String[] args) {

        // EXPRESSION 2nd

        String word = "oona";

        boolean result = word.charAt(0) == word.charAt(1) ? true : false;

// using the ternary operator that will return true if 0th and 1st index char are same else false

        System.out.println(result);

    }

}

4.

import java.util.*;

class chegg {

    public static void main(String[] args) {

        String word = "bob";

        boolean result = word.charAt(0) == word.charAt(word.length() - 1) ? true : false;

// using the ternary operator that will return true if 0th and the last index char are same else false

System.out.println(result);

    }

}

5.

import java.util.*;

class chegg {

    public static void main(String[] args) {

        String word = "Roo";

        boolean result = word.charAt(word.length() - 2) == word.charAt(word.length() - 1) ? true : false;

// using the ternary operator that will return true if the last and the second last index char are same else false System.out.println(result);

    }

}


Related Solutions

a) Given a variable word that has been assigned a string value,write a string expression...
a) Given a variable word that has been assigned a string value, write a string expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the string "(sadly)"b) Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Write a statement that prints the value of price in the form "X dollars and Y cents" on a line by itself. So,...
Given a String variable named sentence that has been initialized, write an expression whose value is...
Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence. write it in python
Given a String variable address, write a String expression consisting of the string "http://" concatenated with...
Given a String variable address, write a String expression consisting of the string "http://" concatenated with the variable's String value. So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com". in java
a. Assume that x is a variable that has been given a string value. Write an expression whose value is True if and only if x is an octal (Base 8) digits (0-7)
In Python: 1 line short expressionsa. Assume that x is a variable that has been given a string value. Write an expression whose value is True if and only if x is an octal (Base 8) digits (0-7)b. Assume that x is a variable that has been given a string value. Write an expression whose value is True if and only if x is an letter.c. Assume that x is a variable that has been given a string value. Write...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
This is C++ programming Given an int variable k, an int array incompletes that has been...
This is C++ programming Given an int variable k, an int array incompletes that has been declared and initialized, an int variable nIncompletes that contains the number of elements in the array, an int variable studentID that has been initialized, and an int variable numberOfIncompletes, Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes. I tried this code...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT