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

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 เม.ย. 2021
  • In this video, I have explained how to get the count the occurrences of a character in the given String
    ~~~Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
    th-cam.com/users/Naveen%20Au...
    Follow me on my Facebook Page:
    / naveenqtpexpert
    Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:
    t.me/joinchat/Q8HTanc9Xi4os0tS
    Naveen AutomationLabs Paid Courses:
    Java & Selenium:
    naveenautomationlabs.com/manua...
    Java & API +POSTMAN + RestAssured + HttpClient:
    naveenautomationlabs.com/selen...
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 50

  • @akhileshhalkarni8192
    @akhileshhalkarni8192 3 ปีที่แล้ว +5

    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 2 ปีที่แล้ว +1

    Hi Naveen ,Thanks for refering below jdk 8 videos link as i am learning on how to code on java 8

  • @akashrevanna5327
    @akashrevanna5327 3 ปีที่แล้ว +1

    Clean explaination 👍

  • @udaybenake206
    @udaybenake206 2 ปีที่แล้ว +1

    Hi Naveen... When you entered samll 'i' then it has not counted uppercase 'I' any reason or solution for this

  • @sasidharreddy5975
    @sasidharreddy5975 3 ปีที่แล้ว

    Hi Naveen
    For selenium are youtube videos enough ?
    Or its needed to buy paid course of you

  • @manjunathuma2623
    @manjunathuma2623 ปีที่แล้ว +1

    Any magicial power to write a program to transfer knowledge from "Naveen AutomationLabs" to "me".

  • @artimandale8286
    @artimandale8286 4 หลายเดือนก่อน

    Awesome 👌

  • @maheswarani5485
    @maheswarani5485 ปีที่แล้ว

    Vera Level

  • @sweetthirty2
    @sweetthirty2 2 ปีที่แล้ว

    Sir how to print "I love java" into "java love I" plzzz make a video on this frequently getting this problem

  • @SharonRueben
    @SharonRueben 3 ปีที่แล้ว +1

    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. th-cam.com/play/PLFGoYjJG_fqqHMYWY-rW554LdNKl_jAIj.html

  • @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+".");

  • @FactGuru2108
    @FactGuru2108 3 ปีที่แล้ว +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 ปีที่แล้ว

      i have the same question

    • @jyotidarekar8006
      @jyotidarekar8006 9 หลายเดือนก่อน

      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

  • @Shradha6979
    @Shradha6979 3 ปีที่แล้ว

    Each and every word count in a given string....

  • @DaveRegan
    @DaveRegan 3 ปีที่แล้ว

    var NumberOfOccurences = "this is a test".Count(c=>c=='i');

  • @pavankumar-xb7nw
    @pavankumar-xb7nw 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +2

      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 ปีที่แล้ว +1

      @@gurunadhmitikela2918 in hash map , the order won't be maintained.

    • @ashwiniv7053
      @ashwiniv7053 3 ปีที่แล้ว +1

      @@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()));

  • @crazyabchackerakatechanima4098
    @crazyabchackerakatechanima4098 3 ปีที่แล้ว

    Can u make part 5 of star patterns 😀

  • @shaikasifhussain6730
    @shaikasifhussain6730 3 ปีที่แล้ว +2

    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 ปีที่แล้ว +2

      Which word? Middle one? Or second one?

    • @shaikasifhussain6730
      @shaikasifhussain6730 3 ปีที่แล้ว

      @@naveenautomationlabs thanks for reply 😊
      middle one!

    • @ShinAkuma
      @ShinAkuma 3 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@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!!

  • @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 ปีที่แล้ว +1

      @@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 😊