0% found this document useful (0 votes)
42 views3 pages

Strings in JAVA

The document describes various string methods in Java like length(), charAt(), compareTo(), concat(), contains(), equals(), getChars(), indexOf(), split(), substring(), toLowerCase(), and toUpperCase(). It also provides examples of reversing a string, checking for palindromes, finding character occurrences, maximum occurring character, substring occurrences, and maximum occurring substring.

Uploaded by

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

Strings in JAVA

The document describes various string methods in Java like length(), charAt(), compareTo(), concat(), contains(), equals(), getChars(), indexOf(), split(), substring(), toLowerCase(), and toUpperCase(). It also provides examples of reversing a string, checking for palindromes, finding character occurrences, maximum occurring character, substring occurrences, and maximum occurring substring.

Uploaded by

shubham yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

I.

length() ==> return length of string;


-----------------------------------------------------------------------------------
---------------------
II. charAt(i) ==> returns character at index i; 0 - n
-----------------------------------------------------------------------------------
---------------------
III. str1.compareTo(str2) ==> 0 if equal, >0 if str1>str2 , <0 if str1<str2
-----------------------------------------------------------------------------------
---------------------
IV. str1.concat(str2) ==> Concatenation (str1+str2) str1 = str1.concat(str2);
-----------------------------------------------------------------------------------
---------------------
V. str1.contains(str2) ==> true if str2 exists in str1 else flase
-----------------------------------------------------------------------------------
---------------------
VI. str1.equals(str2) ==> true if equal, flase is str1!=str2
-----------------------------------------------------------------------------------
---------------------
VII. str1.getChars(begIndx, endIndx, character_to_store, 0); ==> Extracts string as
per requirement

char[] ch = new char[10];


str1.getChars(0, 4, ch,0);
System.out.println(ch);

-----------------------------------------------------------------------------------
---------------------
VIII. indexOf(str) ==> Returns index of substring, -1 if not equal else position of
substring.
-----------------------------------------------------------------------------------
---------------------
IX. str1.split(" ") ==> split the string into parts

String str1 = "Shubham Vinod Yadav Is Shubham And Shubham is Shubham";


String[] str = str1.split(" ");
for(String str2:str)
System.out.println(str2);
-----------------------------------------------------------------------------------
---------------------
X. substring(beg,end) ==> return substring at present location

System.out.println(str1.substring(0, 5));
-----------------------------------------------------------------------------------
---------------------
XI. toLowerCase() ==> converts to lower case

XII. toUpperCase() ==> converts to upper case

===================================================================================
=====================
Q1) Reverse
===================================================================================
=====================
void Reverse(String str) {
StringBuilder str1 = new StringBuilder();
for(int i=str.length()-1;i>=0;i--)
str1.append(str.charAt(i));
str = str1.toString();
System.out.println(str);
}

===================================================================================
=====================
Q2) Palindrome
===================================================================================
=====================
int Palindrome(String str) {
int j=str.length()-1;
for(int i=0;i<str.length();i++) {
if(str.charAt(i)!=str.charAt(j))
return 0;
j--;
}
return 1;
}

===================================================================================
=====================
Q3) Occurance of alphabet in string
===================================================================================
=====================
int no_of_occurances(String str,char ele) {
int count = 0;
for(int i=0;i<str.length();i++) {
if(str.charAt(i)==ele)
count++;
}
return count;
}

===================================================================================
=====================
Q4) Maximum Occuring alphabet
===================================================================================
=====================
public void max_char_substring(String str) {
int count = 0,count1=0;
char val=' ';
for(int i=0;i<str.length()-1;i++) {
count1 = 0;
for(int j=i+1;j<str.length();j++) {
if(str.charAt(i)==str.charAt(j))
count1++;
}
if(count1>count) {
count = count1+1;
val = str.charAt(i);
}
}
System.out.println("Char = "+val+" ==> "+count);
}

===================================================================================
=====================
Q5) Given substring has occured how many times
===================================================================================
=====================
int substring_occurance(String str, String ele) {
int max=0;
for(int i=0;i<str.length()-ele.length();i++) {
String str1 = str.subSequence(i, i+ele.length()).toString();
if(str1.equals(ele))
max++;
}
return max;
}

===================================================================================
=====================
Q6) Maximum substring that has occured.
===================================================================================
=====================
void max_substring_occurance(String str) {
String[] ch = str.split(" ");
int count1=0,count=0;
int max_index=0;
for(int i=0;i<ch.length;i++) {
count1 = substring_occurance(str,ch[i]);
System.out.println(count1);
if(count1>count)
{
count = count1;
max_index = i;
}
}
System.out.println("Maximum Occuring String = "+ch[max_index]+" ==>
"+count);
}

You might also like