Skip to main content

Posts

Check if two strings are anagrams of each other

  An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Ex:-  "code"  and "oced"  ✔          "heist" and "sheit"    ✔            "lane" and  "line"     ✖ Algorithm:-   1.  check both strings are of equal length if yes then proceed to step 2 otherwise print "NO"                            2. sort the characters of both strings either in ascending or descending order.                            3. Check if both string are equal or not.   Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.Arrays; import java.util...

Check if two strings are a rotation of each other

Ex: -  "code"  "odec"   ( rotating "code" 1 time left gives "odec" ) Algorithm : To check for "code" and "deco" Choose a string and concatenate it to itself ( for ex:- here , we choose "deco" you can choose "code" also.) ==>  ("deco" + "deco" =="decodeco") Now take another string and check whether it is present in the string generated in step 1. ( 2nd string is "code" we have to check whether "code" is present in "decodeco". if present print "YES" otherwise "NO". here "code" is present in "de code co" so the answer is "YES". The same approach is followed to solve this question See below. Java 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.util.Scanner; public class CheckTwoStringRotation { public static void main (Strin...

Count the occurrence of a given character in a string

  Ex:-   str = "codeheist"   c = 'e'        occurence = 2   Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.util.Scanner; public class CountOccurence { public static void main (String[] args) { int count = 0 ; Scanner sc = new Scanner(System. in ); System. out . println ( "Enter the String" ); String str = sc. nextLine (); System. out . println ( "Enter the Character" ); char c = sc. next (). charAt ( 0 ); for ( int i = 0 ; i < str. length (); i++) { if (c==str. charAt (i)) count++; } System. out . println (c+ " Occurred " + count + " time." ); } } Python input_string = input ( "Enter a string:" ) ch = input ( "Enter a character:" ) ch_count = input_string . count(ch) print ( "count of" ,ch, "is...

Count the number of Consonants and Vowels in a string

Vowels:-   a,e,i,o,u,A,E,I,O,U  In this question, we have to count the number of consonants and vowels. How to Approach :-  For this, we must traverse the whole string while traversing take a character and check whether it is a consonant or vowel. If it is a consonant increase the count of consonants by 1 otherwise increase the count of vowels by 1. Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.util.Scanner; public class NumbersOfVowelsAndConsonants { public static void main (String[] args) { Scanner sc = new Scanner(System. in ); String str = sc. nextLine (); int vowels = 0 , consonants = 0 ; String lowerCase = str. toLowerCase (); for ( int i = 0 ; i <str. length (); i++) { char c = lowerCase. charAt (i); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) ...

Check if a string contains only digits

 In this program, we have to check whether a string consists of only digits (i.e. 0,1,2.....9). eg:- "qwerty" ( the given string contains characters, not digits ) print No. "123e" ( there is a character present in the given string ) print No. "995765" (the given string contains only digits, no any characters are present ) print Yes. How to approach :-   Traverse through each character of the string and for each character check whether it is a digit or not. Now the question is " How to check a character in the given string is alphabet or digit ? " We can check this by 2 methods :-- 1. Check using ASCII value:--      If the ASCII value of a character is between 48 to 57 (including both 48 and 57)  then it is a digit (0,1,2,3,....,8,9).           If the ASCII value of a character is between 65 to 90 (including both 65 and 90)  then it is a uppercase alphabet (A,B,C,D,....,Y,Z)....

Print the first unique or non-repeated character from a string

Here, we have to print the first character which is not repeated in the given string. eg:- google  ( moving from left to right 'g' is repeated, 'o' is also repeated, 'l' and 'e' are not repeated. but  'l' comes first. So, 'l' is the required answer.) facebook ( moving from left to right first we have to check for character 'f', in the given string "facebook"  'f' comes only one time. So, 'f' is the correct answer.). Note:- In Java, to find the position of a character in a string we have two methods  (i) index( ) and (ii) lastIndexOf( ). 1. index( ) :- It finds the first index position of a character. 2. lastIndexOf( ) :- It finds the last index position of a character.  Solution:- For Java solution we can use the above two methods. If the first position and last position of a character is same it means that character is not repeated in that string. For Pyth...

Print all duplicate characters from a string

 In this question, we have to print duplicate characters i.e. the characters which come more than once in the given string. ex:- java (in "java",  'a' is the only duplicate character as it comes more than once, other than that 'j' and 'v' comes only one time in the given string ). python ( in "python" all the characters come only on time so there is no duplicate character in the given string "python"). Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import java.util.Scanner; public class PrintDuplicateCharacters { public static void main (String[] args) { Scanner sc = new Scanner(System. in ); String duplicate_str = "" ; String str = sc. next (); for ( int i = 0 ; i < str. length (); i++) { char c = str. charAt (i...