ACM (TJU) : 2674


#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;

bool error ( char a [] )
{
    int length = strlen (a);

    bool underscore = false;
    bool upper = false;

    if ( a [0] == '_' || a [length - 1] == '_' )
    return true;

    if ( a [0] > 64 && a [0] < 91 )
    return true;

    for ( int i = 0; i < length; i++ ) {
        if ( a [i] > 64 && a [i] < 91 )
            upper = true;
        else if ( a [i] == '_' )
            underscore = true;

        if ( a [i] == '_' && a [i+1] == '_' )
        return true;
    }

    if ( underscore && upper )
    return true;
    return false;
}

bool cTest ( char a [] )
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if ( a [i] == '_' )
        return true;

        else if ( a [i] > 64 && a [i] < 91 )
        return false;
    }

    return true;

}


int main ()
{
    char a [105];

    while ( gets (a) ) {

        if ( error (a) )
            cout << "Error!";

        else if ( cTest (a) ) {

            int length = strlen (a);

            bool underscore = false;
            for ( int i = 0; i < length; i++ ) {
                if ( a [i] == '_' )
                    underscore = true;

                else if ( underscore ) {
                    underscore = false;
                    cout << (char)toupper (a [i]);
                }

                else
                    cout << a [i];
            }
        }

        else {

            int length = strlen (a);

            for ( int i = 0; i < length; i++ ) {

                if ( a [i] > 64 && a [i] < 91 )
                    cout << "_" << (char)tolower (a[i]);
                else
                cout << a [i];
            }
        }

        cout << endl;

    }

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s