Harry and String

Harry was studying a magic book that categorizes the magic spells into 3 categories - Good, Worst and Bad. If any spell contains all the vowels in alphabetical order then that spell is categorized as Good. If it contains the vowels in reverse alphabetical order, then that spell is categorized as Worst. All the other spells that do not fall in any of the categories before are categorized as Bad. 

Now Harry tries to evaluate himself by solving a spell categorization exercise at the end of the book, but since he is confused can you help him by solving the problems.

Note: The spell is a word of lower case English alphabets only. If there are no vowels in the string, then the spell is classified as "Good".

Example:

Input:  s ="discount"
Output: Good

Approach

Java

import java.util.Vector;

public class HarryString {
    public static void main(String[] args) {

        String s = "discount";

        harryString(s);

    }

    static void harryString(String s) {
        int cnt = 0;
        Vector<Characterv = new Vector<Character>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'a' || s.charAt(i) == 'e' 
|| s.charAt(i) == 'i' || s.charAt(i) == 'o'
                    || s.charAt(i) == 'u') {

                v.add(s.charAt(i));
                cnt++;
            }
        }
        if (cnt == 0)
            System.out.println("Good");
        else {
            int i = 0;
            int flag1 = 0, flag2 = 0;
            while (i < v.size() - 1) {
                if (v.get(i) <= v.get(i + 1))
                    i++;
                else {
                    flag1 = 1;
                    break;
                }
            }
            if (flag1 == 0)
                System.out.println("Good");
            else {
                i = 0;
                while (i < v.size() - 1) {
                    if (v.get(i) >= v.get(i + 1))
                        i++;
                    else {
                        flag2 = 1;
                        break;
                    }
                }
                if (flag2 == 0)
                    System.out.println("Worst");
                else
                    System.out.println("Bad");
            }
        }
    }

}


C++

#include <bits/stdc++.h>
using namespace std;

void harryString(string s)
{
    int cnt = 0;
    vector<charv;
    for (int i = 0i < s.size(); i++)
    {
        if (s[i] == 'a' || s[i] == 'e' ||
 s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
        {

            v.push_back(s[i]);
            cnt++;
        }
    }
    if (cnt == 0)
        cout << "Good\n";
    else
    {
        int i = 0;
        int flag1 = 0flag2 = 0;
        while (i < v.size() - 1)
        {
            if (v[i] <= v[i + 1])
                i++;
            else
            {
                flag1 = 1;
                break;
            }
        }
        if (flag1 == 0)
            cout << "Good\n";
        else
        {
            int i = 0;
            while (i < v.size() - 1)
            {
                if (v[i] >= v[i + 1])
                    i++;
                else
                {
                    flag2 = 1;
                    break;
                }
            }
            if (flag2 == 0)
                cout << "Worst\n";
            else
                cout << "Bad\n";
        }
    }
}
int main()
{

    string s = "discount";

    harryString(s);

    return 0;
}


No comments:

Post a Comment