Sum Of Even Or Odd Numbers Based On The N value using do while Loop || MSK Technologies
Sum Of Even Or Odd Numbers Based On The N value using do while Loop || MSK Technologies
Break the loop which number divisible 2 and 3 || MSK Technologies
PreIncrement Program in java || MSK Technologies
Java Constructor -MSK Technologies
Bitwise OR Program || MSK Technologies
Count The Number of Words in a Given String
Count The Number of Words in a Given String
String : Sateesh kumar
Count words: 2
import java.util.*;
class TokenizerDemo
{
public static void main(String args[])
{
String s="Hello students ,welcome to msk tutorials";
StringTokenizer st=new StringTokenizer(s," ");
int count=0;
while(st.hasMoreTokens())
{
count++;
st.nextToken()
}
System.out.pritln("Number of Words are"+ count);
}
}
String : Sateesh kumar
Count words: 2
more programs visit http://sateeshm.com/
How to print duplicate characters from the string with its count.?
- How to print duplicate characters from the string with its count.?
public class Demo1 {
public static void main(String[] args) {
String s="sateesh";
int count=0,len=0;
while(len!=1)
{
char name[]=s.toCharArray();
len=name.length;
count=0;
for(int j=0;j<len;j++)
{
if((name[0]==name[j])&&((name[0]>=65&&name[0]<=91)|| (name[0]>=97&&name[0]<=123)))
count++;
}
if(count!=0)
System.out.println(name[0]+" "+count+" Times");
s=s.replace(""+name[0],"");
}
}
}
Output:
Char: S, Count: 2 times.
Char: a, Count: 1 times.
Char: t, Count: 1 times.
Char: e, Count: 2 times.
Char: h, Count: 1 times.
more programs visit http://sateeshm.com/
We can say if two strings are an anagram of each other if they contain the same characters but at different orders.
We can say if two strings are an anagram of each other if they contain the same characters but at different orders.
String str1="sateesh";
String str2="sateehs";
import java.util.Arrays;
public class Demo1 {
public static void main(String[] args) {
String str1="sateesh";
String str2="sateehs";
boolean [] char_set = new boolean[256];
boolean [] char_set1 = new boolean [256];
for(int i =0;i<str1.length();i++){
int val1 = str1.charAt(i);
int val2 = str2.charAt(i);
char_set[val1] = true;
char_set1[val2] = true;
}
if(Arrays.equals(char_set, char_set1)){
System.out.println("Same");
}
else
{
System.out.println("Not a same");
}
}
}
Output:
same
more programs visit http://sateeshm.com/
How to reverse the words from the given string sentence?
- How to reverse the words from the given string sentence?
public class Demo1 {
public static void main(String[] args) {
String s="MSK Technologies";
String[] words = s.split(" ");
int x,y;
String reverseString = "";
for ( x = 0; x < words.length; x++)
{
String word = words[x];
String reverseWord = "";
for ( y = word.length()-1; y >= 0; y--)
{
reverseWord = reverseWord + word.charAt(y);
}
reverseString = reverseString + reverseWord + " ";
}
System.out.println(s);
System.out.println(reverseString);
}
}
Output:
KSM seigolonhceT
more programs visit http://sateeshm.com/
remove first and last occurrence of string java
remove first and last occurrence of string java
public class Demo200 {
public static void main(String[] args) {
String s="codedecode";
char ch='e';
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == ch)
{
s = s.substring(0, i) +
s.substring(i + 1);
break;
}
}
for (int i = s.length() - 1; i > -1; i--)
{
if (s.charAt(i) == ch)
{
s = s.substring(0, i) +
s.substring(i + 1);
break;
}
}
System.out.println(s);
}
}
Output:
coddecod
more programs visit http://sateeshm.com/
Count the unique characters in a given String
Count the unique characters in a given String
public class Demo200 {
public static void main(String[] args) {
String s="sateesh";
StringBuffer str=new StringBuffer(s);
for (int i = 0; i < str.length(); i++)
{
int flag = 0;
for (int j = 0; j < str.length(); j++)
{
if (str.charAt(i) == str.charAt(j) && i != j)
{
flag = 1;
break;
}
}
if (flag == 0)
System.out.print(str.charAt(i));
}
}
}
Output:
3
more programs visit http://sateeshm.com/
Display unique characters in a given String
Display unique characters in a given String
public class Demo200 {
public static void main(String[] args) {
String s="sateesh";
StringBuffer str=new StringBuffer(s);
for (int i = 0; i < str.length(); i++)
{
int flag = 0;
for (int j = 0; j < str.length(); j++)
{
if (str.charAt(i) == str.charAt(j) && i != j)
{
flag = 1;
break;
}
}
if (flag == 0)
System.out.print(str.charAt(i));
}
}
}
Output:
ath
more programs visit http://sateeshm.com/