PRF192 – Programming Fundamentals Using C Strings
Part 05 # Strings
------------------------------ COUNTING ------------------------------
1. Write a program that allows users to enter a sentence. The program will count and display
the number of uppercase letters , the number of lowercase letters, and the number of digits.
Example 1: .Please enter a sentence: The PRF192 subject is very importance.
.The number of uppercase alphabets is 4.........................
.The number of lowercase alphabets is 25 .......................
.The number of digits is 3. .......................
Example 2: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.The number of uppercase alphabets is 5.........................
.The number of lowercase alphabets is 13 .......................
.The number of digits is 4. .......................
2. Write a program that allows users to enter a sentence. The program will count and display
the number of vowel letters (includes a, e, i, o, u, y), and the number of consonant letters.
Example 1: .Please enter a sentence: The PRF192 subject is very importance.
.The number of vowel alphabets is 10........................
.The number of consonant alphabets is 19 .......................
Example 2: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.The number of vowel alphabets is 7.........................
.The number of consonant alphabets is 11 .......................
3. Write a program that allows users to enter a sentence and a letter A. The program will count
and display the number of occurrences of letter A in the entered sentence.
Example 1: .Please enter a sentence: ThE PRF192 subject is vEry importance.
.Letter to find: $. ...... ............... ..
.Character '$' is not a letter. Accept letter only! ........
Example 2: .Please enter a sentence: ThE PRF192 subject is very Easy .
.Letter to find: e ...................... ..
.The letter 'e' appears 4 times in the entered sentence ........
Example 3: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.Letter to find: x.. .................... ..
.The character 'x' was not found in the entered sentence ......
4. Write a program that allows users to enter a sentence and a letter A. The program will count
the number of occurrences of letter A in the sentence and show where the letter A is found.
Example 1: .Please enter a sentence: ThE PRF192 subject is vEry importance
.Letter to find: $. ..................... .
.Character '$' is not a letter. Accept letter only! . ......
Example 2: .Please enter a sentence: ThE PRF192 subject is very Easy
.Letter to find: e ...................... .
.The letter 'e' appears 4 times in the sentence in the indexes:
.[2], [15], [23], [27] ..... ..
Example 3: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.Letter to find: x.. .................... ..
.The character 'x' was not found in the entered sentence ......
5. Write a program that allows users to enter a sentence. The program will count the number of
words of the sentence.
Example 1: .Please enter a sentence: ThE PRF192 subject is vEry importance
.There are 6 words in the sentence .......
Example 2: .Please enter a sentence: The Result Of 2 Power 5 Is 32!
.There are 5 words in the sentence ...................... .
Võ Hồng Khanh khanhvh@fe.edu.vn Page 1 / 6
PRF192 – Programming Fundamentals Using C Strings
------------------------------ CONVERTING ------------------------------
6. Write a program that allows the user to enter a sentence. The program normalizes the
sentence according to the following rules:
Converts all letters to lowercase letters.
Example 1: .Please enter a sentence: The PRF192 subject is very easy .
.The normalized sentence is "the prf192 subject is very easy"..
Example 2: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.The normalized sentence is "the result of 2 power 5 is 32!". .
7. Write a program that allows the user to enter a sentence. The program normalizes the
sentence according to the following rules:
Converts all letters to uppercase letters.
Example 1: .Please enter a sentence: The PRF192 subject is very easy .
.The normalized sentence is "THE PRF192 SUBJECT IS VERY EASY"..
Example 2: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.The normalized sentence is "THE RESULT OF 2 POWER 5 IS 32!"...
8. Write a program that allows the user to enter a sentence. The program normalizes the
sentence according to the following rules:
Converts all uppercase letters to lowercase letters.
Converts all lowercase letters to uppercase letters.
Example 1: .Please enter a sentence: The PRF192 subject is very easy .
.The normalized sentence is "tHE prf192 SUBJECT IS VERY EASY"..
Example 2: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.The normalized sentence is "tHE rESULT oF 2 pOWER 5 iS 32!"...
9. Write a program that allows the user to enter a sentence. The program normalizes the
sentence into capitalize sentence according to the following rules:
Converts the first letter of all words to uppercase letter.
Converts all other letters to lowercase letters.
Example 1: .Please enter a sentence: The PRF192 subject is very easy .
.The normalized sentence is "The Prf192 Subject Is Very Easy"..
Example 2: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.The normalized sentence is "The Result Of 2 Power 5 Is 32!"...
------------------------------ REMOVING & REPLACING ------------------------------
10. Write a program that allows the user to enter a sentence. The program normalizes the
sentence according to the following rules:
Removes all spaces on the left side of sentence.
Removes all spaces on the right side of sentence.
Removes all extra spaces inside sentences (keeps only 01 space between words).
Example 1: .Please enter a sentence: C language is easy .
.The normalized sentence: C language is easy .
Example 2: .Please enter a sentence: But the algorithm is not.
.The normalized sentence: But the algorithm is not . .
Võ Hồng Khanh khanhvh@fe.edu.vn Page 2 / 6
PRF192 – Programming Fundamentals Using C Strings
11. Write a program that allows the user to enter a sentence. The program normalizes the
sentence according to the following rules:
Removes all digits of the sentence.
Example 1: .Please enter a sentence: The PRF192 subject is very easy .
.The normalized sentence is "The PRF subject is very easy" ...
Example 2: .Please enter a sentence: The Result Of 2 Power 5 Is 32! .
.The normalized sentence is "The Result Of Power Is !". . .
12. Write a program that allows the user to enter a sentence and a letter A. The program
normalizes the sentence according to the following rules:
Removes all letter A of the sentence.
Example 1: .Please enter a sentence: The PRF192 subject is very easy .
.Letter to remove: e ...................... .
.The normalized sentence is "Th PRF192 subjct is vry asy" ..
Example 2: .Please enter a sentence: The Result Of 0011 plus 0111 is 1010
.Letter to remove: 1 ...................... .
.The normalized sentence is "The Result Of 00 plus 0 is 00 ..
Example 3: .Please enter a sentence: The Result Of 2 Power 5 Is 32!
.Letter to remove: * ...................... .
.The character '*' was not found in the entered sentence ..
13. Write a program that allows the user to enter a sentence and letter old, and letter new. The
program normalizes the sentence according to the following rules:
Replaces all letter old by letter new.
Example 1: .Please enter a sentence: The PRF192 subject is very easy .
.Letter to find: e ...................... .
.Letter to replace: 0 ...................... .
.The normalized sentence: Th0 PRF192 subj0ct is v0ry 0asy . .
Example 2: .Please enter a sentence: The Result Of 0011 plus 0111 is 1010
.Letter to find: 0 ...................... .
.Letter to replace: 9 ..................... .
.The normalized sentence: The Result Of 9911 plus 9111 is 1919
Example 3: .Please enter a sentence: The Result Of 2 Power 5 Is 32!
.Letter to find: 8 ...................... .
.The character '8' was not found in the entered sentence ..
14. Write a program that allows the user to enter a sentence. The program normalizes the
sentence according to the following rules:
Reverses all characters of the sentence.
Example: .Please enter a sentence: The FPT University campus Can Tho .
.The normalized sentence: ohT naC supmaC ytisrevinU TPF ehT . .
15. Write a program that allows the user to enter a sentence. The program normalizes the
sentence according to the following rules:
Reverses all words of the sentence.
Example: .Please enter a sentence: We love U <3
.The normalized sentence: <3 U love We
16. Write a program that allows the user to enter a text. The program will check if the entered
text is a palindrome text or not.
Example 1: .Please enter any text: ab1221ab .
.The text "ab1221ba" is a palindrome text .
Example 2: .Please enter any text: 12ada21000 .
.The text "12ada21000" is not a palindrome text .
Võ Hồng Khanh khanhvh@fe.edu.vn Page 3 / 6
PRF192 – Programming Fundamentals Using C Strings
------------------------------ ADVANCED ------------------------------
17. Full name extracting
Write a program that allows the user to enter their full name. The program will extract first
name and middle name, and last name base on the full name.
Example 1: .Please enter your full name: Ronaldo Luis Nazario de Lima
.The first name is Ronaldo .
.The middle name is Luis Nazario de .
.The last name is Lima .
Example 2: .Please enter your full name: Tom Hanks .
.The first name is Tom .
.The last name is Hanks .
18. The giddy boy
Leonardo often goes out but does not ask his family's permission.
One day LeO went all day and got lost. Because he was lost many times, he tried to remember
the way he has passed.
He messaged his family a line containing the characters N (north), S (south), W (west), E (east).
LeO’s house is located at coordinates (0,0). Please help the poor parent determine location of
the giddy boy.
Example 1: .Please enter the message: NNNEESSSSSEE .
.The location of LeO is (4, -2) .
Example 2: .Please enter the message: EESSEESSWWNNNNWWNNS .
.The location of LeO is (0, 1) . .
19. The King and the girl
In a certain Arab kingdom there was a king who was very kind and wise. One day a pretty girl
came to the palace and claimed to be the king's daughter. The courtiers wanted to kill her to avoid
damaging the king's reputation. But since he was a good king, he wanted to know if she really was his
daughter and then made his decision.
A doctor was invited into the palace to conduct a DNA test of the king and that girl. DNA is a
sequence of nucleotides A, T, C, G. Two people are considered to be related if the DNA sequence of the
first person appears at least 03 times in the DNA sequence of the second person.
Write a program that checks how many times the girl's DNA sequence appears in the king's
DNA sequence. The results will help to conclude whether the girl is the king's daughter or not. For
example, after analyzing a hair sample, the girl's DNA is ATAT and the king's DNA is
GCATATCACGTATATATGCATATATCC. The analysis result is 05 times, so the girl is exactly the
king's biological daughter.
Example 1: .Please enter the king's DNA: GCATATCACGTATATATGCATATATCC
.Please enter the girl's DNA: ATAT
.Analysis results: 5 times
.The girl is the king's biological daughter
Example 2: .Please enter the king's DNA: ATXCDF
.Only accept A, T, C, G for king's DNA!
Example 3: .Please enter the king's DNA: ACTCACTCACTCACTCACTC
.Please enter the girl's DNA: CFTDFG .
.Only accept A, T, C, G for girl's DNA! .
Example 4: .Please enter the king's DNA: ACGTGCACGTGCACGTGCA .
.Please enter the girl's DNA: GCACG .
.Analysis results: 2 times
.She lied and was sentenced to prison .
Võ Hồng Khanh khanhvh@fe.edu.vn Page 4 / 6
PRF192 – Programming Fundamentals Using C Strings
20. Backspace (https://open.kattis.com/problems/backspace)
Shortly before the programming contest started, Bjarki decided to update his computer. He
didn’t notice anything strange until he started coding in his favorite editor, Bim (Bjarki IMproved).
Usually when he’s writing in an editor and presses the backspace key a single character is erased to the
left. But after the update pressing that key outputs the character <. He’s tested all the editors on his
machine, Bmacs, Neobim, bjedit, NoteBjad++ and Subjark Text, but they all seem to have the same
problem. He doesn’t have time to search the web for a solution, and instead decides to temporarily
circumvent the issue with a simple program.
Help Bjarki write a program that takes as input the string that was written in the text editor, and
outputs the string as Bjarki intended to write it. You can assume that Bjarki never intended to write the
character <, and that Bjarki never pressed the backspace key in an empty line.
Input: the message (1≤ |s| ≤ 106), and it will only contain lowercase letters from the English
alphabet as well as the character <.
Output: One line containing the string as Bjarki intended to write it.
Example 1: .Please enter the message: a<bc< .
.The final message is "b" .
Example 2: .Please enter the message: foss<<rritun .
.The final message is "forritun" .
Example 3: .Please enter the message: a<a<a<aa<< .
.The final message is "" .
21. Apaxiaaaaaaaaaaaans! (https://open.kattis.com/problems/apaxiaaans)
The ancient and mysterious Apaxian civilization, which we most certainly did not make up,
continues to confound the researchers at the Oriental Institute. It turns out that the Apaxians had a
peculiar naming system: the more letters in your name, the higher your status in society. So, in Apaxian
society, robert was probably a lowly servant, and robertapalaxiamethostenes was likely a High Priest or
Minister. Even more than that, Apaxians valued the number of adjacent letters that were the same in a
name. So, while robert continues to be an unimpressive name, roooooooooobert probably elicited
cheers and applause wherever he went.
Unfortunately, this makes the task of reading Apaxian scrolls very cumbersome, especially
when you consider that a particularly famous Apaxian queen had ten thousand consecutive a’s in her
name. Legend has it that she was already two years old by the time the Royal Herald finished
announcing her birth.
To make the Oriental Institute’s life easier, the Department of Computer Science has offered to
convert the Apaxian scrolls into a more readable format. Specifically, we will be taking Apaxian names
and replacing all consecutive runs of the same letter by a single instance of such letter.
So, for example, the compact version of roooobert would be robert, where the four consecutive
o’s have been replaced with a single o. Similarly, the compact version of rrrooobbbert would also be
robert. On the other hand, the compact version of robert is still robert.
Input: a single name (1≤ |s| ≤ 250) which contains only lowercase letters (a–z), no whitespace.
Output: the compact version of the name: any time the same letter appears two or more times in
sequence, it must be replaced by a single instance of that letter.
Example 1: .Please enter the name: robert .
.The compact version of the name: robert .
Example 2: .Please enter the name: rooobert
.The compact version of the name: robert .
Example 3: .Please enter the name: roooooobertapalaxxxxios .
.The compact version of the name: robertapalaxios .
Võ Hồng Khanh khanhvh@fe.edu.vn Page 5 / 6
PRF192 – Programming Fundamentals Using C Strings
22. Quick Brown Fox (https://open.kattis.com/problems/quickbrownfox)
A pangram is a phrase that includes at least one occurrence of each of the 26 letters, ‘a’…‘z’.
You’re probably familiar with this one: “The quick brown fox jumps over the lazy dog.”
Your job is to recognize pangrams. For phrases that don’t contain every letter, report what
letters are missing. We’ll say that a particular letter occurs in the phrase if it occurs as either upper case
or lower case.
Input: a single phrase (1≤ |s| ≤ 100), possibly containing upper and lower case letters, spaces,
decimal digits and punctuation characters ‘.’, ‘,’, ‘?’, ‘!’, ‘’’ and ‘"’.
Output: output “pangram” if it qualifies as a pangram. Otherwise, output the word “missing”
followed by a space and then the list of letters that didn’t occur in the phrase. The list of missing letters
should be reported in lower case and should be sorted alphabetically.
Example 1: .Please enter the phrase: The quick brown fox jumps over the lazy dog..
.The test result: pangram .
Example 2: .Please enter the phrase: ZYXW, vu TSR Ponm lkj ihgfd CBA. .
.The test result: missing eq .
Example 3: .Please enter the phrase: .,?!'" 92384 abcde FGHIJ .
.The test result: missing klmnopqrstuvwxyz .
23. Caesar cipher
In the heyday of the Roman empire, Caesar was a very good military king. He was the creator
of cipher which making the army invincible.
With the alphabet referred to numbers according to the following rules:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
a b c d e f g h i j k l m n o p q r s t u v w x y z
With x is the original character and integer K is the encryption key, the encryption is done
simply based on the formula f(x) = (x + K) % 26
To make encryption quickly, you must write a program that allows the user to enter the original
message and an encryption key K. The program will encrypt and display the encrypted message.
Example 1: .Please enter the original message: the prf192 subject is very easy .
.Please enter the encryption key : 1 .
.The encrypted message: uif qsg192 tvckfdu jt wfsz fbtzl .
Example 2: .Please enter the original message: 1..2..3.. a pokemon is flying! .
.Please enter the encryption key : -2 .
.The encrypted message: 1..2..3.. y nmickml gq djwgle! .
Example 3: .Please enter the original message: uif qsg192 tvckfdu jt wfsz fbtzl .
.Please enter the encryption key : -1 .
.The encrypted message: the prf192 subject is very easy .
Võ Hồng Khanh khanhvh@fe.edu.vn Page 6 / 6