Code is copied!
Class 10 ICSE - Java
Class 10th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 10th java topics includes revision of class 9th, constructors, user-defined methods, objects and classes, library classes , etc.
String Methods Programs
Program 1: Write a program to find the frequency of character in a string
Solution:
import java.util.*; class freq { public static void main(String args[]) { int count=0; Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String str=new String((sc.nextLine())); System.out.println("Enter the character to find its frequency="); char ch=sc.next().charAt(0); for(int i=0;i< str.length();i++) { if(ch == str.charAt(i)) { ++count; } } System.out.println("Frequency of "+ch+" ="+count); } }
In the above program,When void main() is executing,it will first take input from user as
string,that is:
String str=new String((sc.nextLine()));
After that, we take character from user to find its frequency:
char ch=sc.next().charAt(0);
And then we used length() function to take length of string,and charAt(), to fetch
each character from the string,and check whether they are equal or not.
Program 2: Write a program to input a string and print vowel,consonant,numbers and whitespace.
Solution:
import java.util.*; class vowel { public static void main(String args[]) { int vowel=0,consonant=0,number=0,whitespace=0; Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String str=new String((sc.nextLine())); for(int i=0;i= '0' && (str.charAt(i)) <= '9') { ++number; } else{ ++consonant; } } System.out.println("Vowels="+vowel); System.out.println("Consonant="+consonant); System.out.println("Numbers="+number); System.out.println("Whitespace="+whitespace); } }
In the above program,When void main() is executing,it will first take input from user as
string,that is:
String str=new String((sc.nextLine()));
And then we used length() function to take length of string,and charAt(), to fetch
each character from the string,and find vowel,consonant,numbers and whitespace in a string
Program 3: Write a program to input a string and check whether it is pallindrome or not
Solution:
import java.util.*; class pallindrome { public static void main(String args[]) { String str1=""; Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String str=new String((sc.nextLine())); for(int i=0;i< str.length();i++) { str1=(str.charAt(i))+str1; } if (str.equals(str1)) { System.out.println("Pallindrome String"); } else{ System.out.println("Not a Pallindrome String"); } } }
In the above program,When void main() is executing,it will first take input from user as
string,that is:
String str=new String((sc.nextLine()));
And then we used length() function to take length of string,and charAt(), to fetch
each character from the string,to reverse it.After that we check whether string is pallindrome or
not.
Program 4: Write a program to input a string and convert it into uppercase and lowercase
Solution:
import java.util.*; class changecase { public static void main(String args[]) { String s1=""; int i; Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String s=new String((sc.nextLine())); System.out.println("String in uppercase is="+s.toUpperCase()); System.out.println("String in lowercase is="+s.toLowerCase()); } }
In the above program,When void main() is executing,it will first take input from user as
string,that is:
String s=new String((sc.nextLine()));
And then we used toUpperCase() function,which coverts string to upper case and then
toLowerCase() function,which converts string to lowercase
Program 5: Write a program to input 5names and print them alphabetically in reverse order.
Solution:
import java.util.*; class names { public static void main(String args[]) { String temp; Scanner sc=new Scanner(System.in); String names[]=new String[5]; for (int i=0;i< 5;i++) { System.out.println( "Enter the "+i+" name"); names[i]=sc.nextLine(); } for (int j=0;j< 5;j++) { for (int k=0;k< 5;k++) { if (names[j].compareTo(names[k]) > 0) { // swapping temp = names[j]; names[j] = names[k]; names[k] = temp; } } } //print alphabetically ordered array System.out.println( "\nNames in alphabetcal order is: \n"); for (int i = 0; i < 5; i++) { System.out.print(names[i]+ " "); } } }