Programs on Strings
1. WAP to input a string and depending on user choice convert into required format:
Sample Run:
Enter String: Please keep the Bags on the Table
Action Required:
1. Capitalize
2. Uppercase
3. Lowercase
4. Togglecase
5. Title case
Enter choice: 4
Toggle Case: pLEASE KEEP THE bAGS ON THE tABLE
2. WAP to input a string and convert to lowercase or uppercase without using lower() and upper() functions.
3. WAP to input two strings and a number x. Form a new string with the first x letters of string 1 and last x letters of string 2.
Sample Run:
Enter string 1: Dinosaur
Enter String 2: Elephant
Enter a number: 4
New string: Dinohant
4. WAP to input a string. Obtain another string from the original by replacing every occurrence of the first character by a ‘*’. The first character must remain same.
Sample Run:
Enter a string: amalgamation
New String: am*lg*m*tion
5. WAP to input a line of text. Count the number of words starting with the letter ‘a’ or 'A' and also display them..
Sample Run:
Enter a string : An Apple a day keeps the doctor away
An
Apple
a
away
No of words starting with A or a 4
6. WAP to input a string and display the words whose length is more than 4 from it.
Sample Run:
Enter line: An apple a day keeps the doctor away.
apple
keeps
doctor
7. WAP to add the substring ‘ment’ to an input string. If the input string is already ending with ‘ment’, then add 'ly to it.
Sample Run:
Enter String: Parch
New string: Parchment
Enter String: Parchment
New String:Parchmently
8. WAP to input n strings and display the length of each string.
9. WAP to input a string and check whether it is a palindrome. A palindrome is a string which reads the same from either side,
Example: MADAM, OPPO
10. WAP to input a string and count the number of vowels, digits, spaces, special characters and consonants in it.
Sample Run:
Enter a string : Good morning ! Welcome to Super @99
No of vowels 10
No of consonants 15
No of digits 2
No of spaces 6
No of special character 2
11. WAP to input a string and display the word with maximum length.
12. WAP to input a string and reverse each of its word.
Sample Run:
Enter a string : we are the best
ew era eht tseb
13. WAP to input a string and a substring. Check if the substring is present in the string and also return its position.
Solution
14. WAP to remove the characters with odd numbered index in a string.
Sample Run:
Enter a String: Mesopotamia Runs
New String: MsptmaRn
15. WAP to input a string and replace every occurrence of a vowel with an '#' sign.
Sample Run:
Enter a String: Mesopotamia Runs
New String: M#s#p#t#m## R#ns
16. WAP to input a sentence and display the words in it in an alphabetical order.
17 . WAP to remove punctuation marks if any in a sentence.
Sample Run:
Enter a string: The boy's friends hid him in the cupboard (cabinet) and ran away !!
The string without Punctuation : The boys friends hid him in the cupboard cabinet and ran away