String Interview Question: Count the occurrences of a character in a String

  Рет қаралды 50,627

Naveen AutomationLabs

Naveen AutomationLabs

Күн бұрын

Пікірлер: 50
@akhileshhalkarni8192
@akhileshhalkarni8192 3 жыл бұрын
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-gj6qw
@Ravikumar-gj6qw 3 жыл бұрын
Hi Naveen ,Thanks for refering below jdk 8 videos link as i am learning on how to code on java 8
@udaybenake206
@udaybenake206 3 жыл бұрын
Hi Naveen... When you entered samll 'i' then it has not counted uppercase 'I' any reason or solution for this
@FactGuru2108
@FactGuru2108 3 жыл бұрын
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.a5148
@f.a5148 2 жыл бұрын
i have the same question
@jyotidarekar8006
@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
@akashrevanna5327
@akashrevanna5327 3 жыл бұрын
Clean explaination 👍
@sasidharreddy5975
@sasidharreddy5975 3 жыл бұрын
Hi Naveen For selenium are youtube videos enough ? Or its needed to buy paid course of you
@manjunathuma2623
@manjunathuma2623 2 жыл бұрын
Any magicial power to write a program to transfer knowledge from "Naveen AutomationLabs" to "me".
@artimandale8286
@artimandale8286 8 ай бұрын
Awesome 👌
@SharonRueben
@SharonRueben 3 жыл бұрын
Hello Naveen..Are there any videos on streams. As a beginner I would like to start streams from beginning
@naveenautomationlabs
@naveenautomationlabs 3 жыл бұрын
Yes please refer this full series. kzbin.info/aero/PLFGoYjJG_fqqHMYWY-rW554LdNKl_jAIj
@maheswarani5485
@maheswarani5485 Жыл бұрын
Vera Level
@Piotr_G112
@Piotr_G112 3 жыл бұрын
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+".");
@sweetthirty2
@sweetthirty2 3 жыл бұрын
Sir how to print "I love java" into "java love I" plzzz make a video on this frequently getting this problem
@pavankumar-xb7nw
@pavankumar-xb7nw 3 жыл бұрын
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?
@ashwiniv7053
@ashwiniv7053 3 жыл бұрын
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 } }
@gurunadhmitikela2918
@gurunadhmitikela2918 3 жыл бұрын
@@ashwiniv7053 Thank you 🙏. I tried this working fine. but are you getting the output in order of characters?
@gopinathm.p1924
@gopinathm.p1924 3 жыл бұрын
@@gurunadhmitikela2918 in hash map , the order won't be maintained.
@ashwiniv7053
@ashwiniv7053 3 жыл бұрын
@@gurunadhmitikela2918 if you want the output in order the sort it first. I hope I answered your question
@DaveRegan
@DaveRegan 3 жыл бұрын
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()));
@DaveRegan
@DaveRegan 3 жыл бұрын
var NumberOfOccurences = "this is a test".Count(c=>c=='i');
@Shradha6979
@Shradha6979 3 жыл бұрын
Each and every word count in a given string....
@crazyabchackerakatechanima4098
@crazyabchackerakatechanima4098 3 жыл бұрын
Can u make part 5 of star patterns 😀
@naveenautomationlabs
@naveenautomationlabs 3 жыл бұрын
Sure, more patterns are coming soon
@crazyabchackerakatechanima4098
@crazyabchackerakatechanima4098 3 жыл бұрын
@@naveenautomationlabs :D
@vinith2320
@vinith2320 3 жыл бұрын
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
@kumarrdy5420
@kumarrdy5420 3 жыл бұрын
No.
@vinith2320
@vinith2320 3 жыл бұрын
@@kumarrdy5420 No means ... ?
@ShinAkuma
@ShinAkuma 3 жыл бұрын
@@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)
@ShinAkuma
@ShinAkuma 3 жыл бұрын
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
@vinith2320
@vinith2320 3 жыл бұрын
@@ShinAkuma Thank you for your feedback 😊
@shaikasifhussain6730
@shaikasifhussain6730 3 жыл бұрын
like same interview Question want to reverse only one word in a sentence? Ex: input: "Naveen Automation Labs" Output: "Naveen noitamotuA Labs"
@naveenautomationlabs
@naveenautomationlabs 3 жыл бұрын
Which word? Middle one? Or second one?
@shaikasifhussain6730
@shaikasifhussain6730 3 жыл бұрын
@@naveenautomationlabs thanks for reply 😊 middle one!
@ShinAkuma
@ShinAkuma 3 жыл бұрын
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.
@shaikasifhussain6730
@shaikasifhussain6730 3 жыл бұрын
@@ShinAkuma yes by using split concept
@shasankkumar7246
@shasankkumar7246 3 жыл бұрын
@@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!!
Alphabetic Pattern Logic - PART 5 - By Naveen AutomationLabs
11:15
Naveen AutomationLabs
Рет қаралды 13 М.
How many people are in the changing room? #devil #lilith #funny #shorts
00:39
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 20 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 20 МЛН
Find Duplicate Elements in An Array || Important Java Interview Questions
22:36
Naveen AutomationLabs
Рет қаралды 52 М.
Java Program To Find Occurrence Of Each Character | Ashok IT
18:15
Write Java program to count Character Occurrences in given string
8:00
Crack Testing Interview
Рет қаралды 37 М.
How many people are in the changing room? #devil #lilith #funny #shorts
00:39