Friday, August 26, 2016

UVA 492 - Pig-Latin solution

probelm link-click here

it's after all an easy problem to implement. its a string processing related problem. you have to just keep in mind that if in a word the 1st letter is vowel then you have to write the word appending "ay" with it. otherwise if you get 1st letter of a word consonant then you have to remove the 1st letter of the word appending it in the last along with "ay". for example suppose a word is "SOJOL". 1st letter S which is consonant so in output it should be "OJOLSay" and suppose a word "ISLAM" here 1st letter is I which is a vowel so in output it should be shown as "ISLAMay". thats it.

an accepted code is given below:

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

bool isvowel(char c)
{
    if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
    {
        return true;
    }
    else
    {
        return false;
    }
}

void processing(string str)
{
    vector<char> v;

    if(isvowel(str[0]))
    {
        int i=0;

        while(isalpha(str[i]))
        {
            cout<<str[i];

            i++;
        }

       if(i==str.size())
        {
        cout<<"ay.";
        }
        else
        {
            cout<<"ay";

        }
    }
    else if(!isvowel(str[0]) && ((str[0]>=65 && str[0]<=91) || (str[0]>=97 && str[0]<=122)) )
    {
        int i=1;

        while(isalpha(str[i]))
        {
            cout<<str[i];

            i++;
        }

        if(i==str.size())
        {
        cout<<str[0]<<"ay.";
        }
        else
        {
            cout<<str[0]<<"ay";

        }
    }
    else
    {
        cout<<str[0];
    }

    for(int k=1;k<str.length();)
    {
        if(!isalpha(str[k]))
        {
            cout<<str[k];
            k++;
        }
       else if(isalpha(str[k]) && !isalpha(str[k-1]))
        {
            if(isvowel(str[k]))
            {
                while(isalpha(str[k]))
                {
                    cout<<str[k];
                    k++;
                }
            cout<<"ay";
            }
            else
            {
                char v=str[k];
                k++;

                while(isalpha(str[k]))
                {
                    cout<<str[k];
                    k++;
                }

            cout<<v<<"ay";
            }

        }
        else
        {
            k++;
        }
    }

    cout<<endl;

}

int main()
{
    string str;

    while(getline(cin,str))
    {
        processing(str);
    }

    return 0;
}

No comments:

Post a Comment