Example
Program: Using StringBuilder
The input
to your program will be a word. Your program should implement the following:
a) If the
input word has letters that repeat more than once, only the first occurrence of
the letter should be retained.
b) If the
input word contains only distinct letters, your program should say, “No
repeated letters”.
Examples:
Run#1:
Input
your word
mattress
The
output is:
matres
Run#2:
Input
your word
fine
The
output is:
No
repeated letters
Problem #1:
Your program will take a
sentence as the input and implement the following:
·
Use the split method to
extract individual words from the sentence.
·
Print all the words in the
input sentence that have the maximum length.
Run #1:
Input the sentence:
have a nice day
The words with the maximum
length:
have
nice
Problem #2:
Your program will take a
sentence as the input. Use the split method to extract individual words from
the sentence. Your program should treat uppercase and lowercase letters
identically. Your program has to find and print the
longest palindrome in the sentence. If the input sentence has no words that are
palindromes, your program should say “No palindromes in the input sentence”.
Examples:
#1:
My racecar performs at a
superior level
racecar
#2:
Where are you
No palindromes in the input
sentence
Problem #3:
Your program will take a
sentence as the input. Use the split method to extract individual words from
the sentence. For every word in the sentence, your program has
to print the number of letters between ‘a’ and ‘m’ contained in the
word.
Example:
It was too good to be true
Count for it = 1
Count for was = 1
Count for too = 0
Count for good = 2
Count for to = 0
Count for be = 2
Count for true = 1
Problem #4:
The
input to your program will be a word. Your program should do the following:
·
If the input word has letters that repeat more than once, only the
first occurrence of the letter should be retained.
·
If the input word has distinct letters, only retain the vowels.
Examples:
a)
Input: mattress
Output: matres
b)
Input: Massachusetts
Output: Maschuet
c)
Input: Computer
Output: oue
Problem #5:
The input to your program will be a sentence. Your
program must print the longest word in the sentence whose letters are distinct.
If there is tie, pick any one. If the input sentence has no word with distinct
letters, say “No such word”. Your program should treat uppercase and lowercase
letters identically.
Run #1:
Input your line:
It was a fine day
fine
Run #2:
Input your line:
Animation! Multimedia!
No such word
Problem #6:
Your program will take a sentence
as the input. Use the split method to extract individual words from the
sentence. Your program should treat uppercase and lowercase letters
identically. Your program has to find and print the word in the input sentence
which has the least number of distinct letters. If more than one
word in the sentence contains the least number of distinct letters, your
program should print all such words.
It was too good to be true
too
to
be
Problem
#7:
Write a program to delete excess blanks from an input sentence so
that the revised string will not contain two consecutive blanks anywhere and no
blanks either at the beginning or the end of the string. You can assume that
words in the input sentence will be all lower case and are separated only by
spaces and the sentence ends with a period. The resulting string should have a
period at the end.
Example:
Input: here
are a lot
of spaces .
Output: here are a lot of
spaces.
Problem #8:
Two words are considered anagrams if they are
composed of the same characters that are rearranged differently. For example,
spear and pears are anagrams of each other. Your program will take words, W1
and W2 as input. It will output if W1 and W2 are anagrams of each other. The
letters in W1 and W2 are both uppercase or both lowercase.
Run
#1:
Input Your First Word
pears
Input Your Second Word
spear
The two input words are anagrams
Run
#2:
Input Your First Word
pear
Input Your Second Word
spear
The two input words are not anagrams
Problem #9:
The soundex algorithm is used by spell checking programs to
suggest words that sound like the word that the user typed. It works by encoding the original word. If a word in the spell check dictionary has
the same code then it is a word to suggest to the user.
Your program
will take as input two words. It will
calculate the soundex encoding for each. Then it will tell whether the two words have
the same soundex encoding. Finally it will tell the
length of the common prefix.
The algorithm:
1.
The first character in the encoding is the first letter of the word.
2.
The following encoding is used on subsequent letters
a. If a letter in the input word is
identical to the first letter in the encoded word, discard that letter in the
input word.
b. discard all vowels and vowel-like
letters: a,e,i,o,u,h,w,y
c. The remaining letters are mapped to digit
characters:
b,p = 1 |
g,j = 4 |
l = 7 |
f,v = 2 |
q,x,z = 5 |
m,n = 8
|
c,k,s = 3 |
d,t = 6 |
r = 9 |
d. if there are two or more of the same digit next to each other, discard all but the first.
Examples
input: APPLESAUCE APPLAUSE
output:
soundex of
APPLESAUCE is A173
soundex of
APPLAUSE is A173
they have the same encoding
they share a prefix of length 4 for their
soundex codes
------------------------------------------------------------
input: BANANABOAT BANNER
output:
soundex of
BANANABOAT is B86
soundex of
BANNER is B89
they do not have the same encoding
they share a prefix of length 2 for their
soundex codes
------------------------------------------------------------
input: MOTHER FATHER
output:
soundex of
MOTHER is M69
soundex of
FATHER is F69
they do not have the same encoding
they share a prefix of length 0 for their
soundex codes
Problem #10:
Given
two unequal strings s1 and s2, we say that s1
dominates s2 if the (multi)set of characters in s1
is a superset of the (multi)set of characters in s2. For
instance, "acmicpc" dominates
"camp", but it does not dominate "chimp" or "macpac". Write a program which takes s1
and s2 as inputs and outputs if s1
dominates s2 or s2 dominates s1
or neither dominates the other.
Examples:
#1:
Input s1: namesake
Input s2: seek
Output: s1 dominates s2
#2:
Input s1: room
Input s2: moreover
Output: s2 dominates s1
#3:
Input s1: car
Input s2: cat
Output: Neither dominates the
other
Problem #11: Archaeologists often find scraps of old texts and have to determine whether two texts are (could be) the same
or related to each other. Because the
texts are damaged, the paper or parchment may be missing or damaged or the ink
may have become illegible. The problem
of this question is to find out whether lines that have been transcribed from
two damaged texts may be related to each other.
Your program will compare lines
that might be related. The input to the program will be two lines consisting of
words from damaged texts. These lines
consist of only the readable parts of the text so some words are likely to be
missing from one or both lines. The
lines will contain distinct words. Your program is to compare the lines of
text, identify the words which are common to both lines, and determine how many
words are common and whether they are in the same order or not.
The input to your program will be
two lines of text. The output will be
one line with a count of the number of common words and the word SAME if the
common words are in the same order or the word DIFFERENT if the common words are
not in the same order.
EXAMPLES:
Input:
brillig and the wabe troves did gimble in gyre
twas and the slithy
troves did gimble wabe
Output:
6 DIFFERENT
Input:
dotes dozy iddle
amzy divvy
marzy dotes dozy amzy
divvy
Output:
4
SAME
Problem #12: Given two input words, S1 and S2, write a
program which finds and prints the largest substring that is common to S1 and
S2.
Example:
Input the First String: together
Input the Second String: niether
The largest common substring: ether
Problem #13: http://www.cs.duke.edu/courses/fall00/cps149s/assign/pacnw99/g.pdf
Problem
#14: Typically
when we write arithmetic expressions, we use what is called infix notation:
For example, we write ``1 + 2'' to indicate that we are adding the two
parameters 1 and 2. This is infix notation because we place the operator (`+')
in between the two parameters (1 and 2). Prefix notation is an
alternative way of writing expressions, where the operator comes before
the parameters. Here, ``+12'' would compute to 3 and ``*+123'' would compute to
9.
Here are some prefix expressions and their infix equivalents and the
value they compute to:
Prefix notation Infix equivalent Output
+-+1298 ((1 + 2) - 9) + 8 2
/9-74
9 / (7 - 4) 3
*+81-+123 (8 + 1) * ((1 + 2) - 3) 0
/9-7+34 9/ ((3+4) –
7)
ILLEGAL EXPRESSION
Write
a program to evaluate prefix expressions involving addition, subtraction,
multiplication, and division. You can assume the input will be a string of only
single digit characters (0, 1...9) and operators (+,-, /,*) without any spaces.
The intermediate values will always be a single digit integer. The final output
will be at most a two digit integer. If you ever encounter a division by zero
while evaluating the expression, your program should state “ILLEGAL EXPRESSION” and terminate
itself. The input to your program will be a string and the output will be the
evaluated value (integer) of the prefix expression.