Question

1 – Write a subroutine that reads a word from the user.  The subroutine gets the input...

1 – Write a subroutine that reads a word from the user.  The subroutine gets the input from the keyboard and returns as output a string containing all the characters the user typed until either a space character is found or there are no more characters in the keyboard buffer.

For example:

If the user types the following input: Welcome to Disney World

The first call to the subroutine should return “Welcome”

The second time the subroutine is called, it returns “to”

The third time the subroutine is called, it returns “Disney”

The fourth time it should return “World”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You have not specified the language, so I'm going to go with python.

The following program does your job using global variables. We can access the global variables in the local scope of a function/subroutine using the "global" keyword.

count = 0
str = [x for x in input().split()]

def printWords():
global count
print(str[count])
count = count + 1

Now whenever you call printWords() each word is printed until a space is encountered. The split() function splits the input string by spaces and stores the words in str as a list.

Add a comment
Know the answer?
Add Answer to:
1 – Write a subroutine that reads a word from the user.  The subroutine gets the input...
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
  • Can someone fix the program below so it does what the picture says it won't work...

    Can someone fix the program below so it does what the picture says it won't work for me. import java.util.Scanner; public class Userpass { static String arr[]; static int i = 0; public static void main(String[] args) { String username, password; int tries = 0, result; do { System.out.print("Enter the username: "); username = readUserInput(); System.out.print("Enter the password: "); password = readUserInput(); result = verifyCredentials(username, password); tries++; if (result == -1) System.out.println("The username is incorrect!\n"); else if (result == -2)...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • 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.

  • The program needs to be written in C. Write a function void camelCase(char* word) where word...

    The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...

  • Write a C Program that asks the user to input a string and then the user...

    Write a C Program that asks the user to input a string and then the user is prompted back whether the input string is a “Palindrome Word” or not. (A Palindrome Word reads the same backward or forward, eg. madam, racecar, etc.) Your program should contain a function that accepts the input string from the main function and returns a value indicating whether the input string is a Palindrome or not. Use Pointers to traverse the string.

  • 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...

  • TASK Your task is to build a palindrome from an input string. A palindrome is a...

    TASK Your task is to build a palindrome from an input string. A palindrome is a word that reads the same backward or forward. Your code will take the first 5 characters of the user input, and create a 9- character palindrome from it. Words shorter than 5 characters will result in a runtime error when you run your code. This is acceptable for this exercise – we will cover input validation in a later class. Some examples of input...

  • 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...

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