0% found this document useful (0 votes)
359 views8 pages

Icse 10 Computer Chinni TR Note

Strings in Java can contain text enclosed in double quotes. There are many string methods that can be used to manipulate and analyze string data. These include methods to get the length, check characters at indexes, find indexes of characters, convert case, compare strings, extract substrings, and more. The document provides examples of using various string methods like length(), charAt(), indexOf(), toLowerCase(), substring(), and others.

Uploaded by

AD SOLUTIONS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
359 views8 pages

Icse 10 Computer Chinni TR Note

Strings in Java can contain text enclosed in double quotes. There are many string methods that can be used to manipulate and analyze string data. These include methods to get the length, check characters at indexes, find indexes of characters, convert case, compare strings, extract substrings, and more. The document provides examples of using various string methods like length(), charAt(), indexOf(), toLowerCase(), substring(), and others.

Uploaded by

AD SOLUTIONS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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 occurrence of a character in a string.
Eg: s. index(‘l’)= 2 Return Type:-int
5) lastIndexOf()- It will return the index of last occurrence 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.equalsIgnoreCase(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

Question 1
Write a program to enter a string and print the number of occurrences of each
vowel in the string.
import java.util.*;
public class Q1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int a=0,e=0,i=0,o=0,u=0;
char ch;
int l=s.length();
s=s.toUpperCase();
int j;
for(j=0;j<l;j++)
{
ch=s.charAt(j);
if(ch=='A')
a++;
if(ch=='E')
e++;
if(ch=='I')
i++;
if(ch=='O')
o++;
if(ch=='U')
u++;
}
System.out.println("The number of a's="+a);
System.out.println("The number of e's="+e);
System.out.println("The number of i's="+i);
System.out.println("The number of o's="+o);
System.out.println("The number of u's="+u);

}
}
Sample output
Enter the string
apple a day keeps the doctor away
The number of a's=5
The number of e's=4
The number of i's=0
The number of o's=2
The number of u's=0
HW
Question 2
Write a program to find the total number of vowels in a string.
public class VowelCount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int v=0;
char ch;
int l=s.length();
s=s.toUpperCase();
int j;
for(j=0;j<l;j++)
{
ch=s.charAt(j);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
v++;
}
System.out.println("The no of vowels="+v);
}
}
Question 3
Write a program to input five names in an array and display the names begin
with a vowel?
import java.util.*;
public class strin1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String a[]=new String[5];
int j;
char c;
for(j=0;j<5;j++)
{
System.out.println("Enter String"+(j+1)+": ");
a[j]=sc.nextLine();
}

for(j=0;j<5;j++)
{
c=a[j].charAt(0);
if (c=='A'||c=='a'||c=='E'||c=='e'||c=='O'||c=='o'||c=='I'||
c=='i'||c=='U'||c=='u')
System.out.println("The first char in String"+(j+1)+" is vowel:"+a[j]);
}
}
}

Question 4
Write a program to enter a string and find the total number of characters in a string and also
reverse the string?

import java.util.*;
public class Reverse
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int l=s.length();
int i;
char c;
String rev=" ";
for(i=l-1;i>=0;i--)
{
c=s.charAt(i);
rev=rev+c;
}
System.out.println("No of characters="+l);
System.out.println("The reversed string="+rev);
}
}

HW
Question 5
Write a program to check whether a string is palindrome or not.

import java.util.*;
public class striPali
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the string");
s=sc.nextLine();
int l=s.length();
int i;
char c;
String rev=" ";
for(i=l-1;i>=0;i--)
{
c=s.charAt(i);
rev=rev+c;
}
if(rev.compareTo(s)==0)
System.out.println("The given string is a Palindrome");
else
System.out.println("The given string is not a Palindrome");
}
}

Chinni. R. Pillai
Computer Department
Mob:9620184111

You might also like