• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

View

# Exercises - String Operations

last edited by 2 years, 9 months ago

1. Write a program that asks the user to type in a word.  The program will then display the number of characters in the word. (solution)

(a) Use a loop to keep prompting the user for words and displaying the number of characters.  Stop the program when the user enters a specific word, such as "done". (solution)

2. Write a program that asks the user for two integers as strings.  Convert the strings to integers and output their product (i.e., multiply them together). (solution)

(a) Create a string that contains the question and the answer using concatenation.  Output the answer as a single string. (solution)

3. Write a program that allows the user to enter a sentence (including spaces).  Ask the user for a range of numbers and display the characters in that range.  Make sure you check that both numbers are within the string (from 1 to stringLength). (solution)

4. Write a program that asks the user for a word.  Randomly display a character from that word. (solution)

(a) Input a sentence instead of a word.  Randomly display a character, but don't display a space. (solution)
(b) [challenging] Input a sentence, each word separated by a space.  Randomly display a WORD from that sentence.

5. Write a program that reads a six-digit integer and prints the sum of the six digits.  Use string commands to extract each digit. (Note:  This problem was also asked in Exercises - Converting Reals and Integer Values, but it is much simpler using strings).

(a)  Modify the program so the user can enter any integer value (i.e., any length at all, not just 6 digits).

6. Consider the following math problem:  How many four-digit positive integers (x) are there with the property that x and 3x have only even digits?  For example, one such number is x=8002, since 3x=24006 and each of the digits in x and 3x is even (divisible by 2).  (Here is another problem from Exercises - Converting Reals and Integer Values).

7. Ask the user for one letter at a time. For each letter, "add" the new letter and start building a word. After you add each letter, tell the user the current word they are buildings. Keep going until the user enters a period, and then tell them the final word.
Sample Output (user input in blue):
Letter? a
Current Word: a
Letter? p
Current Word: ap
Letter? p
Current Word: app
Letter? l
Current Word: appl
Letter? e
Current Word: apple
Letter? .
Final Word: apple

8. Write a program that asks the user to enter a word and a letter.  Your program should count how many times the letter appears in the word.  If the letter is not in the word, output a message saying so.

9. Write a program that asks the user to enter a word and a letter.  Your program should find the location of the LAST occurrence of that letter.  If the letter is not in the word, output a message saying so.

10. Write a program that asks the user for a word.  As the program runs, output an increasing sequence of letters, starting at the first character and ending with the entire word.  For example, if the user gave an input of "apple", the output would be:
a
ap
app
appl
apple

11. Write a program that asks the user for a word.  As the program runs, output an decreasing sequence of letters, starting with the word and ending with the first letter.  For example, if the user gave an input of "apple", the output would be:

apple
appl
app
ap
a

12. Write a program that asks the user to enter an integer number (recommend input as a string).  Add each of the digits together.  For example, if the user inputs "123", the total of the digits is 6 (which is 1+2+3).

13. [challenging] Write a program that asks the user to enter an integer number (recommend input as a string).  Add each of the digits together ONLY IF the digits are either the same or larger.  If the digits get smaller, subtract the digits.  For example:
input = "123455" would give an output of 20 (1 + 2 + 3 + 4 + 5 + 5)
input = "123115" would give an output of 11 (1 + 2 + 3 - 1 + 1 + 5)
input = "553215" would give an output of 9 (5 + 5 - 3 - 2 - 1 + 5)

14. (a) Write a program that asks the user to enter a word (string variable) and a letter (string variable).  Find the first location of the letter in the word and save it in a variable (integer).  Output the result for the user.
(b) Find all locations of the letter, and output them for the user.  You will need a loop.

15. (a) Write a program that will ask the user to enter a sentence (string variable), and then output the total number of characters (including spaces and punctuation) in the sentence.  The program will then ask the user a starting number and ending number (integer variables), and display the substring between those start and end values.
(b) Add error checking to your program, and do not allow the user to enter invalid values for the start or end of the string.

16. When creating variables, we try to use the camelCase naming convention.  The first word is lowercase, and all following words start with a capital letter, and there are no spaces between words.  Write a program that asks the user for one or more words and the creates a camelCase variable name.  For example,
"Welcome to Class" becomes "welcomeToClass"
"THE QUICK BROWN FOX" becomes "theQuickBrownFox"
"HellO HOW aRe you" becomes "helloHowAreYou"

17. A palindrome is a word that is spelled the same forwards and backwards.  For example, "noon" and "radar" are palindromes, while "hello" and "goodbye" are not.
(a) Write a program to accept a single word as input and determine if it is a palindrome.  If you have learned about subroutines, create a subroutine called "isPalindrome" that accepts a single word as a parameter, and returns a boolean (true/false).
(b) A palindrome can always be created by reversing the letters in the original word and adding those letters to either the beginning or the end.  Write a program, or subroutine, that will accept a string as input and automatically produce a palindrome (but make sure you check if it's already a palindrome first).
(c) The method in (b) for creating a palindrome is called a "brute force" method, which means that it works, but may not be the best solution.  Sometimes you don't need to add every reversed letter to create the palindrome.  For example, "anagram" only requires the last 4 letters to be reversed (creating "marganagram".  Write a program, or subroutine, that will create the palindrome using the fewest extra characters possible.

18. Building Strings: Write a program which asks the user for one word at a time. After the user enters their word, you will concatenate the new word with all previous words. When the user enters the word "quit", output the final string. The final string should not include the word "quit". You will need a loop for this program.
Extend: Whenever the user enters a new word, also put a space so the final string looks better.
For example:
User input? hello
Current string: Hello
User input? How
Current string: HelloHow
User input? Are
Current string: HelloHowAre
User input? You?
Current string: HelloHowAreYou?
User input? quit
Program terminated by user.
Final string: HelloHowAreYou?