Question

Complete this program so that it prints the given word with the first and last character interchanged

 Complete this program so that it prints the given word with the first and last character interchanged. For example, if word is "tool", then produce "loot".

 Words.java

image.png

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

public class Words {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String word = in.next();

        if(word.length()>=2){
            char first = word.charAt(0);
            char last = word.charAt(word.length()-1);
            word = word.substring(1,word.length()-1);
            word = last+word+first;
        }

        System.out.println(word);
    }
}
Add a comment
Know the answer?
Add Answer to:
Complete this program so that it prints the given word with the first and last character interchanged
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
  • Write a Java program that reads a word and prints its bigrams substrings. A character bigram...

    Write a Java program that reads a word and prints its bigrams substrings. A character bigram is defined as a continuous sequence of two characters in a word. For example, if the user provides the input "rum", the program prints ru um Hint: 1. set two pointers, one always moves from the beginning of the string and the other moves from i+2, in which i is the current position of the first pointer. 2. print an error message if the...

  • Using Python: Given a word, write one line of command that print “True” if the first...

    Using Python: Given a word, write one line of command that print “True” if the first letter is the same as the last letter, and the length of the word is greater than 2. Otherwise, the program prints a “False”. For example, if x='dad', then we expect that line of command to return a “True”.

  • Write a program that Reads a string from cin Prints the first and last letter without...

    Write a program that Reads a string from cin Prints the first and last letter without space in between, followed by endl For example, For an input seattle your program prints se(endl). For an input newyork your program prints nk (endl). You can assume that the length of the string is always greater than 1. LAB 0/10 ACTIVITY 1.9.1: Week2-4 Zip main.cpp Load default template. #include <iostream> using namespace std; 4int mainO 6 /* 8return Type your code here. /...

  • complete a program so that it asks the user to enter a number and then prints...

    complete a program so that it asks the user to enter a number and then prints out the user's number, 2x the user's number, and 10x the user's number

  • P1) Write a complete C program that prints out the word YES, if its string command...

    P1) Write a complete C program that prints out the word YES, if its string command line argument contains the sequence the somewhere in it. It prints out the word NO otherwise. Both the word the and partial sequences like in the words theatre or brother qualify. Note: You can use string functions or not but if you do the only ones allowed are strcpy and strlen. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P2) What is the output of the following program (one answer per...

  • Write a Java program that reads in a word from the user and outputs each character...

    Write a Java program that reads in a word from the user and outputs each character of the word on a separate line. For example, if the user enters the input "Joe", your program should output: J o e You need to use a for-loop.

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Can anyone please solve this for me in java. Thanks Complete the program below that prints...

    Can anyone please solve this for me in java. Thanks Complete the program below that prints a square. The program will read the size (an integer value) of the square and a character. It will then generate a square with a number of rows and columns that corresponds to size, and where the * character is used for the square border. The provided character will be used for the rest of the diagram. Use the message “Enter size:” and “Enter...

  • Write a C program that prints the numbers from 1 to 100, but substitutes the word...

    Write a C program that prints the numbers from 1 to 100, but substitutes the word “fizz” if the number is evenly divisble by 3, and “buzz” if the number is divisible by 5, and if divisible by both prints “fizz buzz” like this: 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizz buzz 16 17 fizz 19 buzz ... and so on

  • Parallelogram Program Write a program that prints the following parallelogram pattern given the the following two...

    Parallelogram Program Write a program that prints the following parallelogram pattern given the the following two inputs. Here are the rules: Your program must work with any length greater than 1. Your program must use the character the user inputs to draw the parallelogram. Your program must not use global variables or global code other than a call to main(). Your program output must match my out exactly. I have provided you with the strings you need for the output...

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
ADVERTISEMENT