Java Programs


Search a character in a string

  • Program that will accept a string and character to search. The program will call a function, which will search for the occurrence position of the character in the string and return its position.


Program

import java.io.IOException;
import java.util.Scanner;

public class CharSearch {

            private static Scanner sc;
            public static void main(String[] args) throws IOException {
                       
                        char key;
                        sc=new Scanner(System.in);
                        System.out.println("Enter the String:");
                        String str=sc.nextLine();
                        System.out.println("Enter the character to be searched:");
                        key=(char) System.in.read();
                        CharSearch s=new CharSearch();
                        s.search(str, key);
            }
           
            public void search(String str,char key) {
                       
                        int flag=0;
                        for(int i=0;i<str.length()-1;i++) {
                                    if(str.indexOf(key,i)!=-1) {
                                                if(flag==0) {
                                                            System.out.println("The character \'"+key+"\' is present at positions:");
                                                            flag=1;
                                                }
                                                i=str.indexOf(key,i);
                                                System.out.println(i+1);
                                                }
                                    }
                        if(flag==0) {
                                    System.out.println("The character \'"+key+"\' is not there in the string.\n"+(-1));
                       
                        }
            }

}

Output

Enter the String:
This is java programming.
Enter the character to be searched:
g
The character 'g' is present at positions:
17
24


Number to Words

  • Function, which prints a given number in words.

Program

import java.util.Scanner;

public class NumberToWords {

            private static String[] ones={"zero","One","Two","Three","Four","Five","Six","Seven", "Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen", "Seventeen","Eighteen","Nineteen"};
            private static String[] tens={"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy", "Eighty","Ninety"};
            private static Scanner sc;
            public static void main(String[] args) {
                       
                        int num;
                        System.out.println("Enter the number:");
                        sc=new Scanner(System.in);
                        num=sc.nextInt();
                        if(num>0) {
                                    System.out.println("The number in words:");
                        }
                        NumberToWords.wordConvert(num);

            }
           
            public static void wordConvert(int num) {
                       
                        if(num<0) {
                                    System.out.println("Enter a positive number");
                                    return;
                        }
                        if(num<20) {
                                    System.out.print(ones[num]);
                                    }
                                    else if(num<100) {
                                                System.out.print(tens[num/10]+" ");
                                                if(num%10>0) {
                                                            NumberToWords.wordConvert(num%10);
                                                }
                                    }
                                    else if(num<1000) {
                                                System.out.print(ones[num/100]+" Hundred");
                                                if(num%100>0) {
                                                            System.out.print(" and ");
                                                            NumberToWords.wordConvert(num%100);
                                                }
                                    }
                                    else if(num<100000) {
                                                NumberToWords.wordConvert(num/1000);
                                                System.out.print(" Thousand ");
                                                if(num%1000>0) {
                                                            NumberToWords.wordConvert(num%1000);
                                                }
                                    }
                                    else if(num<10000000) {
                                                NumberToWords.wordConvert(num/100000);
                                                System.out.print(" Lakh ");
                                                if(num%10000>0) {
                                                            NumberToWords.wordConvert(num%10000);
                                                }
                                    }
                                    else {
                                                System.out.println("Enter a number below 1 Crore!!");
                                    }
                       
            }

}

Output

Enter the number:
9999
The number in words:
Nine Thousand Nine Hundred and Ninety Nine

No comments:

Post a Comment