0% found this document useful (0 votes)
30 views

Java Programs

The document contains 7 code snippets related to string and array processing in Java. The first snippet counts the number of words in a string. The second splits a string into words and prints each word and its length. The third finds the frequency of each character in a string. The fourth determines if two strings are anagrams by comparing character frequencies. The fifth also determines if strings are anagrams by sorting their characters and comparing. The sixth sorts the words in a string. The seventh finds the number of pairs in an integer array.

Uploaded by

Sai Matsa
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Java Programs

The document contains 7 code snippets related to string and array processing in Java. The first snippet counts the number of words in a string. The second splits a string into words and prints each word and its length. The third finds the frequency of each character in a string. The fourth determines if two strings are anagrams by comparing character frequencies. The fifth also determines if strings are anagrams by sorting their characters and comparing. The sixth sorts the words in a string. The seventh finds the number of pairs in an integer array.

Uploaded by

Sai Matsa
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1)import java.util.

Scanner;
public class Sample
{
public static int findNumOfWords(String s)
{
int count=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==' '&& s.charAt(i+1)!=' ')
count++;
}
return count+1;
}
public static void main(String args[])
{
Scannner sc=new Scanner(System.in);
String str;
str=sc.nextLine();
System.out.println(findNumOFWords(str));
       }
}
================================================================
2)import java.util.Scanner;
class sample
{
public static void findwords(string s)
{
String words[]=s.split(" ");
for(int i=0;i<words.length;i++)
{
System.out.println(words[i]+" "+words[i].length());
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str;
str=sc.nextline();
findwords(str);
}
}
================================================================
3)import java.util.Scanner;
public class findfrequency
{
public static void findcharacterfrequency(String s)
{
s=s.toLowercase():
int x[]=new int[26];
for(int i=0;i<s.length();i++)
{ char ch=s.charAt(i);
if(ch!=' ')
{
x[ch-'a']++;
}
}
for(int i=o;i<26;i++)
{
if(x[i]!=0)
System.out.println((char)(i+97)+" - "+x[i]);
}
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String str=s.nextline();
findcharfreq(str);
}
}
==================================================================
4)import java.util.Scanner;
class sample
{
public static void findcharacterfrequency(String s)
{
s=s.toLowercase():
int x[]=new int[26];
for(int i=0;i<s.length();i++)
{ char ch=s.charAt(i);
if(ch!=' ')
{
x[ch-'a']++;
}
}
for(int i=o;i<26;i++)
{
if(x[i]!=0)
System.out.println((char)(i+97)+" - "+x[i]);
}
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String str1,str2;
str1=sc.nextLine();
str2=sc.nextLine();
int m[]=findcharacterFrequency(str1);
int n[]=findcharacterFrequency(str2);
for(int i=0;i<26;i++)
{
if(m[i]!=n[i])
{
System.out.println("Not Anagrams");
System.exit(0);
}
}
System.out.println("Anagrams");
}
}
=================================================================
5)import java.util.Scanner;
import java.util.Arrays;
public class Anagrams2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1,str2;
str1=sc.nextLine();
str2=sc.nextLine();
char ch1[]=str1.tocharArray();
char ch2[]=str2.tocharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
if(Arrays.equals(ch1,ch2)
System.out.println("Anagrams");
else
System.out.println("Not Anagrams");
}
}
==================================================================
6)import java.util.Scanner;
import java.util.Arrays;
class Stringsort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1,words[];
str1=sc.nextLine();
words=str1.split(" ");
System.out.println("Before sorting");
for(int i=0;i<words.length;i++)
System.out.println(words[i]);
Arrays.sort(words);
System.out.println("After sorting");
for(int i=0;i<words.length;i++)
System.out.println(words[i]);
}
}
===================================================================
7)import java.util.scanner;
public class FindNumFreq
{
public static int findpairs(int arr[],int size)
{
int count=0,pairs=0;
for(int i=0;i<size;i++)
{
if(arr[i]!=-1)
{
count=1;
for(int j=i+1;j<size;j++)
{
if(arr[i]==arr[j])
{
count++;
arr[j]=-1;
}
}
pairs=pairs+count/2;
}
}
return pairs;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,x[],i;
n=sc.nextInt();
x=new int[n];
for(i=0;i<n;i++)
x[i]=sc.nextInt();
System.out.println(findpairs(x,n));
}
===================================================
import java.util.scanner;
public class FindNumFreq
{
public static int FindFreq(int arr[],int size)
{
int count=0,pairs=0;
for(int i=0;i<size;i++)
{
if(arr[i]!=-1)
{
count=1;
for(int j=i+1;j<size;j++)
{
if(arr[i]==arr[j])
{
count++;
arr[j]=-1;
}
}
if(count==1)
System.out.println(arr[i]);
else
System.out.println();
}
}
return count;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,x[],i;
n=sc.nextInt();
x=new int[n];
for(i=0;i<n;i++)
x[i]=sc.nextInt();
System.out.println(FindFreq(x,n));
}

You might also like