Even though the solution might appear complex but using regex is one of the efficient ways to solve these kind of problems Once you get the syntax, validation of IP addresses, emails and websites becomes possible My solution :- import java.util.regex.*; import java.util.Scanner; class First { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter the String : "); String input = scan.nextLine(); System.out.println("Enter the character you want to count the occurrence of : "); //Reading the character char ch = scan.next().charAt(0); //Casting from character to string String regex = Character.toString(ch); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) count++; System.out.println("The number of occurrences of character '" + regex + "' is " + count); } }
@Ravikumar-gj6qw3 жыл бұрын
Hi Naveen ,Thanks for refering below jdk 8 videos link as i am learning on how to code on java 8
@udaybenake2063 жыл бұрын
Hi Naveen... When you entered samll 'i' then it has not counted uppercase 'I' any reason or solution for this
@FactGuru21083 жыл бұрын
How will we print occurrence of all character in one go rather then checking each character occurrence from main method? i.e. I -->2 O-->1 t--->2 e--->1
@f.a51482 жыл бұрын
i have the same question
@jyotidarekar8006 Жыл бұрын
public static void main(String[] args) { String str = "String Buffer ClaSs"; String str1 = str.replaceAll(" ", "").toLowerCase(); System.out.println(str1); Map map = new HashMap(); char[] arr=str1.toCharArray(); for(int i = 0; i
@akashrevanna53273 жыл бұрын
Clean explaination 👍
@sasidharreddy59753 жыл бұрын
Hi Naveen For selenium are youtube videos enough ? Or its needed to buy paid course of you
@manjunathuma26232 жыл бұрын
Any magicial power to write a program to transfer knowledge from "Naveen AutomationLabs" to "me".
@artimandale82868 ай бұрын
Awesome 👌
@SharonRueben3 жыл бұрын
Hello Naveen..Are there any videos on streams. As a beginner I would like to start streams from beginning
@naveenautomationlabs3 жыл бұрын
Yes please refer this full series. kzbin.info/aero/PLFGoYjJG_fqqHMYWY-rW554LdNKl_jAIj
@maheswarani5485 Жыл бұрын
Vera Level
@Piotr_G1123 жыл бұрын
What do you think about this solution? String searchedCharacter="a"; String inputString1="I love Javaa"; List lettersInaList = new ArrayList(Arrays.asList(inputString1.split(""))); int numberOfOccurrences = Collections.frequency(lettersInaList,searchedCharacter); System.out.println("A letter "+searchedCharacter+ " occurs "+numberOfOccurrences+" times in a text: " +inputString1+".");
@sweetthirty23 жыл бұрын
Sir how to print "I love java" into "java love I" plzzz make a video on this frequently getting this problem
@pavankumar-xb7nw3 жыл бұрын
Actually in interview they asked me to print each character with count in string. And that time we need to pass only String and not matching character. How to make it?
@ashwiniv70533 жыл бұрын
import java.util.*; class Main { public static void main(String[] args) { String str= "aabbbcc"; getCharCount(str); } public static void getCharCount(String str){ Map map = new HashMap(); for(char ch: str.toCharArray()){ if(map.containsKey(ch)){ map.put(ch, map.get(ch)+1); } else map.put(ch,1); } System.out.println(map); // to print character with count of occurance // to print only the count and not the } }
@gurunadhmitikela29183 жыл бұрын
@@ashwiniv7053 Thank you 🙏. I tried this working fine. but are you getting the output in order of characters?
@gopinathm.p19243 жыл бұрын
@@gurunadhmitikela2918 in hash map , the order won't be maintained.
@ashwiniv70533 жыл бұрын
@@gurunadhmitikela2918 if you want the output in order the sort it first. I hope I answered your question
@DaveRegan3 жыл бұрын
var NumberOfOccurences = "This is a test string".GroupBy(c=>c).OrderBy(c=>c.Key.ToString()).ToList(); NumberOfOccurences.ForEach(c => Console.WriteLine("'"c.Key + "' " + c.Count()));
@DaveRegan3 жыл бұрын
var NumberOfOccurences = "this is a test".Count(c=>c=='i');
@Shradha69793 жыл бұрын
Each and every word count in a given string....
@crazyabchackerakatechanima40983 жыл бұрын
Can u make part 5 of star patterns 😀
@naveenautomationlabs3 жыл бұрын
Sure, more patterns are coming soon
@crazyabchackerakatechanima40983 жыл бұрын
@@naveenautomationlabs :D
@vinith23203 жыл бұрын
This is also an easiest way to count occurrences of each character Naveen class string { public static void main(String[]args) { String s="Naveen Automation Labs"; s=s.toLowerCase(); for(char ch='a';ch
@kumarrdy54203 жыл бұрын
No.
@vinith23203 жыл бұрын
@@kumarrdy5420 No means ... ?
@ShinAkuma3 жыл бұрын
@@vinith2320 This is not a good way to do it. Don't use nested loops. You're doing it in O(n^2) time, while it can be done in O(n)
@ShinAkuma3 жыл бұрын
public class Test { public static void main(String[] args) throws IOException { String s = "Naveen Automation Labs"; s = s.toUpperCase(); s = s.replaceAll(" ", ""); int chr[] = new int[26]; for(int i=0; i
@vinith23203 жыл бұрын
@@ShinAkuma Thank you for your feedback 😊
@shaikasifhussain67303 жыл бұрын
like same interview Question want to reverse only one word in a sentence? Ex: input: "Naveen Automation Labs" Output: "Naveen noitamotuA Labs"
@naveenautomationlabs3 жыл бұрын
Which word? Middle one? Or second one?
@shaikasifhussain67303 жыл бұрын
@@naveenautomationlabs thanks for reply 😊 middle one!
@ShinAkuma3 жыл бұрын
Try doing it yourself instead of asing for code. I'll give you a logic. Implement it yourself. Take your sentence and break it into a String array using Space as the delimiter. Iterate over this array and pick every word and pass it through a reverse function and concatenate all words of the array using space. The reverse function is simple, if you dont want to use the StringBuffer reverse, just break your word into a array of character then swap the first character with last character and move inwards from there. Now you dont mention by what logic we are picking the words here, you figure that out yourself according to your requirements.
@shaikasifhussain67303 жыл бұрын
@@ShinAkuma yes by using split concept
@shasankkumar72463 жыл бұрын
@@shaikasifhussain6730 this is one solution : // Helper reverse function public static void rev(char[] str, int start, int end) { while(start < end) { char temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } //Main function public static void revWord(String str, String word) { if(str.isEmpty()|| word.isEmpty()|| str.length() < word.length()) { System.out.println("Invalid Input Error"); return; } int start = str.indexOf(word); int end = start + word.length() - 1; char[] strArray = str.toCharArray(); rev(strArray,start,end); for(char ch : strArray) { System.out.print(ch); } } public static void main (String[] args) { String main = "HelloWorldHello"; String word = "World"; revWord(main,word); } Hope it helped! test different scenarios of strings and let me know if there is any fault, I'd like to correct Thank You!!