1. Write a program to input a string (word). Convert it into lowercase letters. Count and print the frequency of each alphabet present in the string. The output should be given as:
Sample Input: Alphabets Sample Output: ========================== Alphabet Frequency ========================== a 2 b 1 e 1 h 1 l 1 p 1 s 1 t 1 import java.io.*; class Alphabet_Freq { public static void main(String ss[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter any string: "); String s = br.readLine(); s=s.toLowerCase(); //converting the string into lowercase int l=s.length(); //finding the length of the string char alph[]=new char[26]; //array for storing alphabets from 'a' to 'z' int freq[]=new int[26]; //array for storing frequency of all alphabets char c='a'; for(int i=0; i<26; i++) { alph[i]=c; //storing all alphabets from 'a' till 'z' in alph[] array freq[i]=0; //intializing the count of every alphabet with '0' c++; } char ch; System.out.println("Output:"); System.out.println("=========================="); //this is just for styling the look of the output System.out.println("Alphabet\tFrequency"); System.out.println("=========================="); /* Counting frequency of alphabets begins below */ for(int i=0; i<26; i++) { for(int j=0; j<l; j++) { ch=s.charAt(j); //extracting characters of the string one by one if(ch==alph[i]) //first checking the whole string for 'a', then 'b' and so on freq[i]++; //increasing count of those aplhabets which are present in the string } } for(int i=0; i<26; i++) { if(freq[i]!=0) //printing only those alphabets whose count is not '0' System.out.println(" "+alph[i]+"\t\t "+freq[i]); } } } 2. Write a program that encodes a word into Piglatin. To translate a word into a Piglatin word, convert the word into uppercase and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel being shifted towards the end followed by “AY”. Sample Input (1) : London, Sample Output (1) : ONDONLAY Sample Input (2) : Olympics, Sample Output (2) : OLYMPICSAY import java.io.*; class Piglatin { public static void main(String ss[])throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.print("Enter any word: "); String s=br.readLine(); s=s.toUpperCase(); //converting the word into Uppercase int l=s.length(); int pos=-1; char ch; for(int i=0; i<l; i++) { ch=s.charAt(i); if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { pos=i; //storing the index of the first vowel break; } } if(pos!=-1) //printing piglatin only if vowel exists { String a=s.substring(pos); //extracting all alphabets in the word beginning from the 1st vowel String b=s.substring(0,pos); //extracting the alphabets present before the first vowel String pig=a+b+"AY"; //adding "AY" at the end of the extracted words after joining them System.out.println("The Piglatin of the word = "+pig); } else System.out.println("No vowel, hence piglatin not possible"); } } 3. Java programming code written in BlueJ which swaps the values of two Strings without using any third (temp) variable. import java.io.*; class Swap_Strings { public static void main(String ss[])throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader (System.in)); System.out.print("Enter the 1st String : "); String s1=br.readLine(); int len1=s1.length(); System.out.print("Enter the 2nd String : "); String s2=br.readLine(); System.out.println("-------------------------------"); System.out.println("Strings Before Swapping : "); System.out.println("1st String = "+s1); System.out.println("2nd String = "+s2); /*Swapping Process Begins*/ s1=s1+s2; s2=s1.substring(0,len1); s1=s1.substring(len1); /*Swapping Process Ends*/ System.out.println("-------------------------------"); System.out.println("Strings After Swapping : "); System.out.println("1st String = "+s1); System.out.println("2nd String = "+s2); } } 4. Write a program to find the shortest and the longest word in a sentence and print them along with their length.Sample Input: I am learning Java Sample Output: Shortest word = I Length = 1 Longest word = learning Length = 8 import java.io.*; class Short_long_word { public static void main(String ss[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter any sentence : "); //inputting the sentence String s=br.readLine(); s=s+" "; //adding a space at the end, to extract the last word also int len=s.length(); //finding the length of the sentence String x="",maxw="",minw=""; char ch; int p,maxl=0,minl=len; for(int i=0;i<len;i++) { ch=s.charAt(i); //extracting characters of the string one at a time if(ch!=' ') { x=x+ch; //adding characters to form word if character is not space } else { p=x.length(); if(p<minl) //checking for minimum length { minl=p; minw=x; } if(p>maxl) //checking for maximum length { maxl=p; maxw=x; } x=""; //emptying the temporary variable to store next word } } System.out.println("Shortest word = "+minw+"nLength = "+minl); System.out.println("Longest word = "+maxw+"nLength = "+maxl); } } 5. Write a program to accept a sentence and print only the first letter of each word of the sentence in capital letters separated by a full stop. Example : INPUT SENTENCE : “This is a cat” OUTPUT : T.I.A.C. import java.io.*; class Initials { public static void main(String ss[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String s; char x; int l; System.out.print("Enter any sentence: "); s=br.readLine(); s=" "+s; //adding a space infront of the inputted sentence or a name s=s.toUpperCase(); //converting the sentence into Upper Case (Capital Letters) l=s.length(); //finding the length of the sentence</span> System.out.print("Output = "); for(int i=0;i<l;i++) { x=s.charAt(i); //taking out one character at a time from the sentence if(x==' ') //if the character is a space, printing the next Character along with a fullstop System.out.print(s.charAt(i+1)+"."); } } } 6. Write a program to input a word from the user and remove the duplicate characters by replacing the sequence of duplicate characters by its single occurrence. Example: INPUT – Jaaavvvvvvvvaaaaaaaaaaa OUTPUT – Java INPUT – Heeeiiiissggoiinggg OUTPUT – Heisgoing import java.io.*; class RemoveDupChar { public static void main(String ss[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter any word: "); // Inputting the word String s = br.readLine(); s = s + " "; // Adding a space at the end of the word int l=s.length(); // Finding the length of the word String ans=""; // Variable to store the final result char ch1,ch2; for(int i=0; i<l-1; i++) { ch1=s.charAt(i); // Extracting the first character ch2=s.charAt(i+1); // Extracting the next character // Adding the first extracted character to the result if the current and the next characters are different if(ch1!=ch2) { ans = ans + ch1; } } System.out.println("Word after removing repeated characters = "+ans); // Printing the result } } 7. Program to accept any string: Count and print the number of pairs of consecutive letters present in words in the forward direction only. Assume that all the letters in the string are in the same case, consecutive letters in two different words are not counted and ‘za’ or ‘ZA’ in any word is not a consecutive pair. For example: INPUT: Enter String: ABSTRACT STRING IS BEING COUNTED EDUCATIONALLY. OUTPUT: Pairs of consecutive letter: 3 import java.io.*; class cst { public static void main (String ss[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str; System.out.println("enter the string "); str=br.readLine()+" "; String str2=""; int len1=str.length(); int l,j,cnt=0,l1,sp=0,len; char i; for(l1=0;l1<len1;l1++) { i=str.charAt(l1); if(i==' ') { str2=str.substring(sp,l1); len=str2.length(); sp=l1+1; for(l=0;l<len-1;l=l+2) { char x=str2.charAt(l); int z=(int)(x); int d=l+1; char y=str2.charAt(d); int a=(int)(y); int c=a-z; if(c==1) { cnt++; } } } }System.out.println(cnt); } } 8. Program to input a sentence and print the smallest word of the given sentence. For example: if s=”India is my country” then the output should be is. import java.io.*; class sword { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); String str; System.out.println("enter string"); str=br.readLine()+" "; int l,len,len2; int big=100,n=0,sp=0; String str2=""; String str3=""; char x; len=str.length(); for(l=0;l<len;l++) { x=str.charAt(l); if(x==' ') { str2=str.substring(sp,l); len2=str2.length(); sp=l+1; if(big>len2) { big=len2; str3=str.substring(n,l); n=l; } } } System.out.println(str3); System.out.println(big); } } 9. Consider the string as: India wins cricket world cup. Program to accept a sample string and delete all the vowels present in the string. The program also counts deleted vowels and the new string is displayed as: nd wns crckt wrld cp Number of vowel deleted = 8 import java.io.*; class vowel { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); String str; System.out.println("enter string"); str=br.readLine(); int l,len,cnt=0; char x,y; String str2=""; len=str.length(); for(l=0;l<len;l++) { x=str.charAt(l); if((x=='a')|(x=='e')|(x=='i')|(x=='o')|(x=='u')) { cnt++; } else { str2=str2+x; } } System.out.println(str2); System.out.println(cnt); } } |
10. Program to input the string and print the longest word along with its length. For eg if input string is “That Is A Question” then output the longest word is Question and its length is 8. import java.io.*; class lword { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); String str; System.out.println("enter string"); str=br.readLine()+" "; int l,len,len2; int big=0,sp=0; String str2=""; String str3=""; String str4=""; char x; len=str.length(); for(l=0;l<len;l++) { x=str.charAt(l); if(x==' ') { str2=str.substring(sp,l); len2=str2.length(); sp=l+1; str3=str2; if(len2>big) { big=len2; str4=str3; } } } System.out.println(str4); System.out.println(big); } } 11. To input a word and print its Pig Latin form? Pig Latin word is formed by splitting the original word into two parts from the first vowel of the word. The new word is 2nd part-1st part+ay e.g. simply its Pig Latin form is implysay import java.io.*; class pig { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); String str; System.out.println("enter string"); str=br.readLine(); String str2=""; String str3=""; String str4=""; int l,l1,len,len2,sp=0; char x,y; len=str.length(); for(l=0;l<len;l++) { x=str.charAt(l); if((x=='a')|(x=='e')|(x=='i')|(x=='o')|(x=='u')) { str2=str.substring(l,len); str3=str.substring(0,l); break; } } str4=str2+str3+"ay"; System.out.println(str4); } } 11. To input two words and check whether two words are anagrams or not? The characters of the two words are same excluding spaces. e.g. parliament & partial men or william shakespeare & i am a weakish speller import java.io.*; class ana { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); int l,j,sp=0; int t=0; String str; System.out.println("enter string "); str=br.readLine(); str=str.toLowerCase(); String str2; System.out.println("enter string2"); str2=br.readLine(); str2=str2.toLowerCase(); int len=str.length(); int len2=str2.length(); char x,y; if(len==len2) { for(l=0;l<len;l++) { x=str.charAt(l); t=0; for(j=0;j<len2;j++) { y=str2.charAt(j); if(x==y) t=1; } if(t==0) break; } if(t==0) System.out.println("not anagram"); else System.out.println("are anagram"); } else System.out.println("ops!try with new words"); } } 12. Input two words and find out the set that will be formed by the intersection of the characters found in both the string. e.g; Input 1 : DISARI Input 2 : HALDIA Output will come as: D,I,A import java.io.*; class i { public static void main (String jjs[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str; System.out.println("enter the string no spaces"); str=br.readLine(); int len=str.length(); String str1; System.out.println("enter the string no spaces"); str1=br.readLine(); String str2=""; int len2=str1.length(); int l,j,cnt=0; for(l=0;l<len;l++) { for(j=0;j<len2;j++) { char x=str.charAt(l); char y=str1.charAt(j); if(x==y) { str2=str2+y; } } } System.out.println(str2); } } 13. Write a program to input a sentence and print its each word separately along with its length E.G. Input:- Aman Kumar Sharma Output:- Aman - 4 Kumar- 5 Sharma- 6 import java.io.*; class eword { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); String str; System.out.println("enter string"); str=br.readLine()+" "; int l,len,len2=0; int sp=0; String str2=""; char x; len=str.length(); for(l=0;l<len;l++) { x=str.charAt(l); if(x==' ') { str2=str.substring(sp,l); len2=str2.length(); sp=l+1; System.out.println(str2); System.out.println(len2); } } } } 14. This is a simple program in cryptography. Given a string of only uppercase lettrs, we can encode it in many ways. One simple way is to replace each letter by a letter which is at a fixed distance ahead or behind in the alphabet, where the alphabet is assumed to wrap around. Similarly, we can decode an encoded string. Define a class type Crypto which has instance variable String s and a method encode which is described below. String encode ( int move)-replaces each character in s by the character which is at a distance of nove ahead, if move is positive. If move is 0 the orginal string is unchanged, If move is negative, then each charcter is replaced by a charcter at a distance move behind it. For E.G When s=”ABCXYZ”, move (2) returns “CDEZAB” When s=”ABCDE”, move (-3) returns “XYZAB” import java.io.*; class cry { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); String str; System.out.println("enter string"); str=br.readLine()+" "; System.out.println("enter value"); int n=Integer.parseInt(br.readLine()); int l,len,y; String str2=""; char x; len=str.length(); for(l=0;l<len;l++) { x=str.charAt(l); int c=Integer.valueOf(x); if(n>0) { if((c>=65)&&(c<=90-n)) { c=c+n; str2=str2+(char)(c); } else { y=c+n; c=(y-90)+64; str2=str2+(char)(c); } } }System.out.println(str2); } } |