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

Java String Manipulation Techniques

Uploaded by

Tejas Khadke
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)
7 views3 pages

Java String Manipulation Techniques

Uploaded by

Tejas Khadke
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

Q.

find no occurance of character in given string:


import [Link];
class HelloWorld {
public static void main(String[] args) {
String str="Java";
int count=0;
char c='a';
char arr[]=[Link]();
//we can print arrays using for loop or toString method as shown below:
// for(int j=0;j<[Link];j++){
// [Link](arr[j]);
//}
[Link]([Link](arr));
for(int i=0;i<[Link];i++){
if(arr[i]=='a'){
count++;
}
}
[Link]("No. of occurance of character in given String is : "
+count);
}
}
OR
class HelloWorld {
public static void main(String[] args) {
String str="Java";
int count=0;
char c='a';
[Link]([Link]());
for(int i=0;i<[Link]();i++){
if([Link](i)=='a'){
count++;
}
}
[Link]("No. of Occurances of Given Character in Above String is: "
+ count);
}
}

[Link] Reverse the Given String:


import [Link];
class HelloWorld {
public static void main(String[] args) {
String str="Java";
char ch[]=[Link]();
[Link]([Link](ch));
StringBuffer str1=new StringBuffer(str);
[Link]();
[Link](" "+ str1);
}
}
OR
class StringReverse {
public static void main(String[] args) {
String str="Java";
String rev="";
for(int i=[Link]()-1;i>=0;i--){
rev=rev+[Link](i);
}
[Link](rev);
}
}

[Link] check given String is Palindrome or not( palindrome means Madam reverse Madam-
Palindrome)
class PalindromeString {
public static void main(String[] args) {
String str="AVA";
String rev="";
for(int i=[Link]()-1;i>=0;i--){
rev=rev+[Link](i);
}
[Link](rev);
if([Link](rev)){
[Link]("String is palindrome : " );
}
else{
[Link]("given String is not palindrome ");
}
}
}
OR
class PalindromeString {
public static void main(String[] args) {
String str="AVA";
StringBuffer s= new StringBuffer(str);
[Link]();
String s2=[Link]();
if([Link](s2)){
[Link]("palindrome");
}
else{
[Link]("not palindrome");
}
}
}

Q. To check given strings are anagrams or not (anagrams is if one string contains
all character that other string contains and their size should be same
irrespecctive of sequence.)
import [Link];
class AnagramsString {
public static void main(String[] args) {
String str1="care";
String str2="racl";

if([Link]()!=[Link]()){
[Link]("Given Strings length is invalid ");
return;
}
char[]ch1=[Link]();
char[]ch2=[Link]();
[Link](ch1);
[Link](ch2);
if([Link](ch1,ch2)){
[Link]("Given Strings Anagrams. ");
}
else{
[Link]("Given Strings are not Anagrams. ");
}
}
}
Q. to swap to String variables without using third variable?
i/p:String a="java";
string b="program";

o/p: string a="program";


string b="java";

import [Link];
class SwapString {
public static void main(String[] args) {
String str1="java";
String str2="program";
str1=str1+str2;
[Link]("length of str1 is: "+[Link]()+" "+str1);
str2=[Link](0,[Link]()-[Link]());
[Link]("length of str2 is: "+[Link]()+" "+str2);
str1=[Link]([Link]());
[Link]("length of str1 is: "+[Link]()+" "+str1);
}
}
Q. write java from to remove all vowels from string:
class RemoveVowelsString {
public static void main(String[] args) {
String str1="hello i love my india ";
str1=[Link]("[AEIOUaeiou]", "");
[Link](str1);
}
}
===================================================================
Q. reverse each word of string

class ReverseWordInString {
public static void main(String[] args) {
String str="hello my friend";
[Link]("Try [Link]");
String []str2=[Link](" ");
for(int i=0;i<[Link];i++){
String x=str2[i];
StringBuffer sb=new StringBuffer(x);
[Link]();
[Link](sb+" ");
}
}
}
I/p: hello my friend
O/p: olleh ym dneirf
===================================================================
[Link] count no of words in String
i/p: i love java programing
o/p: 4

You might also like