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

String

Uploaded by

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

String

Uploaded by

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

1. How to reverse each word in string?

public class Demo {


public static void main(String[]args) {

String str="rama subbareddy";


String reverseString="";
String[] words = str.split(" ");
for(int i=0; i<words.length;i++) {
String word=words[i];
String reverseword="";
for(int j=word.length()-1;j>=0;j--) {

reverseword=reverseword+word.charAt(j);
}
reverseString=reverseString+reverseword+"
";

System.out.println(reverseString);
}
}

public class Demo {


public static void main(String[]args) {

String str="rama subbareddy";


StringBuffer reverseString=new StringBuffer();
String[] split=str.split(" ");
for(int i=split.length-1;i>=0;i--) {
reverseString.append(split[i]).append("
");
}
System.out.println(reverseString.reverse());
}
}
2. How to remove duplicates from String?

public class Demo {


public static void main(String[]args) {

String str="rama subbareddy";


Set<Character> lhSet=new LinkedHashSet<>();
char[] charArray = str.toCharArray();

for(char ch:charArray) {
if(ch !=' ') {
boolean check = lhSet.contains(ch);
if(!check) {
lhSet.add(ch);
}
}
}
System.out.println(lhSet);

}
}
public class Demo {
public static void main(String[]args) {

String str="rama subbareddy";


String ans="";
char[] charArray = str.toCharArray();
int i,j;
for(i=0; i<charArray.length;i++) {
for(j=0; j<i;j++) {
if(charArray[i]==charArray[j]) {
break;
}
}
if(j==i) {
ans=ans+charArray[i];
}
}
System.out.println(ans);
}
}

You might also like