Strings (1)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Strings

Anything enclosed in double quotes is called a String.


String functions
1) length()- It is used to find the total no: of characters that is present in a string.
Eg:- String s=”Hello”;
s.length=5; Return Type:-int
2) capacity()- it determines the total number of characters that can be accommodated
in a string.
3) charAt()- It return the character at a specified index eg: s.charAt(1)=’e’ Return
Type=char.
4) indexOf()- It will return the index of the first occurrance of a character in a string.
Eg: s. index(‘l’)= 2 Return Type:-int
5) lastIndexOf()- It will return the index of last occurance of a character in a string.
Eg:-s.lastIndex(‘l’)=3 Return Type:-int
6) toLowerCase()- It will convert the string to lower case. Eg: s.toLowerCase()=”hello”
Return Type:-String
7) toUpperCase()-It will convert the string to uppercase eg:-s.toUpperCase()=”HELLO”
Return Type:-String
8) startsWith()- It is used to check whether a string starts with a particular prefix. Eg:
s.startsWith(‘H’)=true Return Type:-boolean
9) endsWith()-It is used to check whether a string ends with a particular suffix Eg:
s.endsWith(‘o’)=true Return Type:-boolean
10) replace()- It is used to replace an old character with a new character eg:-
s.replace(‘e’,’a’)=Hallo
11) trim()- It is used to remove whitespaces from both ends of a string.
12) valueOf()- It is used to get the string representation of a number. Eg: int n=12;
n.valueOf()=”12” Return Type:-String.
13) toString()-Returns the string itself. It is used instead of System.out.println()
eg: s.toString()=Hello Return Type:-String.
14) concat()- It is used to concatenate(join) two strings. Eg:-String s1=”Good”; String
s2=”Morning” s1.concat(s2)=”GoodMorning” or s1+s2=”GoodMorning”
ReturnType=String
15) compareTo()- It is used to compare two strings lexicographically. (According to
alphabetical order/According to the words in the dictionary)
Eg:1) String s1=”Delhi”;
String s2=”Mumbai”;
2)S1.compareTo(s2)=-9( a negative value if first string is less than the second string).
S2.compareTo(s1)=+9(a positive value if first string is greater than the second string).
3)String s1=”Sarvodaya”;
String s2=”Sarvodaya”;
s1.compareTo(s2)=0(if both strings are equal)
16) equals()- It is used to check whether two strings are equal or not considering the
case.
Eg:- String s1=”Sarvodaya”;
String s2=”Sarvodaya”;
s1.equals(s2)=true Return Type=boolean
17) equalsIgnoreCase()-It is used to check whether two strings are equal or not ignoring
the case.
Eg:- String s1=”Sarvodaya”;
String s2=”SARVODAYA”;
s1.qualsIgnoreCase(s2)=true Return Type=Boolean
18) substring()-It is used to extract certain characters from a string.
Eg: s.substring(0,2)=He
s.substring(1,2)=e
19) compareToIgnoreCase()- It compares two strings without considering the case.

Programs
1. Write a program to input a sentence and convert it into uppercase and count and
display the total number of words starting with a letter ‘A’.

Output
2.Special words are those words which starts and ends with the same letter.
Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and vice
versa
Examples:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC
All palindromes are special words, but all special words are not palindromes. Write a
program to accept a word, check and print whether the word is a palindrome or only
special word.

Output
3. Write a program to initialize the seven Wonders of the World along with their
locations in two different arrays. Search for a name of the country input by the user.
If found, display the name of the country along with its Wonder, otherwise display
“Sorry Not Found!”.
Seven wonders — CHICHEN ITZA, CHRIST THE REDEEMER, TAJMAHAL, GREAT WALL
OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations — MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example — Country Name: INDIA Output: INDIA-TAJMAHAL
Country Name: USA Output: Sorry Not Found!

4.Write a program to assign a full path and file name as given below. Using library
functions, extract and output the file path, file name and file extension separately as
shown.
Input C: / Users / admin / Pictures / flower.jpg
Output path: C: / Users/admin/Pictures/
File name: flower
Extension: jpg
5.Write a program that encodes a word into Piglatin. To translate word into a Piglatin
word, convert the word into uppercase and then place the first vowel of the original
word as the start of the new word along with the remaining alphabets. The
alphabets present before the vowel being shifted towards the end followed by “AY”.
Sample input (1) : London, Sample output (1) : ONDONLAY
Sample input (2) : Olympics, Sample output (2) : OLYMPICSAY

6.Write a program to accept a string. Convert the string to uppercase. Count and
output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4
7. Write a program to accept a word and convert it into lowercase if it is in
uppercase, and display the new word by replacing only the vowels with the
character following it :
Example :
Sample Input : computer
Sample Output : cpmpvtfr

8. Write a program in Java to accept a string in lower case and change the first letter
of every word to upper case. Display the new string.
Sample input: we are in cyber world
Sample output: We Are In Cyber World

9.Define a class to accept a string and print the characters with the upper case and
lowercase reversed, but all other characters should remain the same as before.
Example : WelCoMe_2023
Output: wELcOmE_2023
10. Define a class to accept two strings of same length and form a new word in such
a way that , the first character of the first word is followed by the first character of
the second word and so on.
Example:
Input string 1 – BALL
Input string 2 - WORD
Output
BWAOLRLD
Binary Search in descending order array

Bubble Sort in descending order

Soumia Prasanth
9400841344

You might also like