The String Problem

Bob's crush's name starts with a vowel. That's the reason Bob loves vowels too much. He calls a string "lovely string" if it contains either all the lowercase vowels or all the uppercase vowels or both, else he calls that string "ugly string". 

Example:

Input:  str = "omahgoTuRuLob";
Output: ugly string

Approach

Java


public class StringProblem {
    public static void main(String[] args) {
        String str = "omahgoTuRuLob";
        System.out.println(getResult(str));
    }

    static String getResult(String a) {
        boolean lovely = true, loop = true;

        while (loop) {
            if (a.contains("a") || a.contains("A")) {
            } else {
                lovely = false;
                break;
            }
            if (a.contains("e") || a.contains("E")) {
            } else {
                lovely = false;
                break;
            }
            if (a.contains("i") || a.contains("I")) {
            } else {
                lovely = false;
                break;
            }
            if (a.contains("o") || a.contains("O")) {
            } else {
                lovely = false;
                break;
            }
            if (a.contains("u") || a.contains("U")) {
            } else {
                lovely = false;
                break;
            }
            break;
        }

        if (lovely) {
            return "lovely string";
        } else {
            return "ugly string";
        }
    }
}

C++

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

string getResult(string a)
{
    bool lovely = trueloop = true;

    while (loop)
    {
        if (a.find("a") != string::npos || a.find("A") != string::npos)
        {
        }
        else
        {
            lovely = false;
            break;
        }
        if (a.find("e") != string::npos || a.find("E") != string::npos)
        {
        }
        else
        {
            lovely = false;
            break;
        }
        if (a.find("i") != string::npos || a.find("I") != string::npos)
        {
        }
        else
        {
            lovely = false;
            break;
        }
        if (a.find("o") != string::npos || a.find("O") != string::npos)
        {
        }
        else
        {
            lovely = false;
            break;
        }
        if (a.find("u") != string::npos || a.find("U") != string::npos)
        {
        }
        else
        {
            lovely = false;
            break;
        }
        break;
    }

    if (lovely)
    {
        return "lovely string";
    }
    else
    {
        return "ugly string";
    }
}

int main()
{

    string str = "omahgoTuRuLob";

    cout << getResult(str<< "\n";

    return 0;
}


No comments:

Post a Comment