Kirty Bhushan
Kirty Bhushan

Reputation: 147

Java program displays the result one less than desired result

I was doing this practice problem on a competitive coding site. WE have a scenario in which we have a smart browser in which we don't need to enter "www." and neither the vowels. The browser enters these two things by its own.

I am writing a program which displays the ratio of the no of characters in the smart web url and the complete web url. ie. for example smart url for www.google.com would be ggl.com. Hence display of program would be 7/14. I did that but my display is 6/14. i.e one less .It is for every test case. I don;t know where is the problem

Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();// no of testcases!

    while(t > 0)
    {
        String st = sc.next();
        int count = st.length();
        count = count-4;
        int count1 = st.length();
      for(char da:st.toCharArray())
        {
            switch(da)
            {
                case 'a':
                    count  = count -1;
                    break;

                    case 'e':
                    count = count -1;
                    break;

                    case 'i':
                    count = count-1;
                    break;

                    case 'o':
                    count = count -1;//System.out.println(da);
                    break;

                    case 'u':
                    count = count -1;
                    break;
            }
        }

        System.out.print((count ) +"/" +count1) ;
        System.out.println();
        t--;

    }   

Upvotes: 2

Views: 89

Answers (1)

Eran
Eran

Reputation: 393781

ggl.com still contains a vowel, so your loop would decrement count for the o, and your program would return 6 instead of 7.

Note that in general, the domain name of the url can have a different number of vowels - for example, com, gov and net have 1, edu has 2, fr has 0. Your code should ignore the vowels following the last ..

This can solve your problem :

  ....
  String st = sc.next();
  int count = st.length();
  count = count-4;
  int count1 = st.length();
  st = st.substring(0,st.lastIndexOf('.')); // get rid of the domain name
  for(char da:st.toCharArray())
      ...

This assumes that only the vowels following the last . should be kept in the count. If, for example, in a .co.il domain you wish to keep both the o and the i in the count, you will have to change the logic.

Upvotes: 9

Related Questions