Saturday, June 27, 2009

58. SMS encoder

Problem : SMS Encoder
-----------------------

People who use SMS (Short Message Service) to send text messages on
their mobile telephones and other devices often abbreviate phrases to
squeeze more meaning into a message that is required to be short.

You must write a programme that takes an input string and creates an
SMS version of it by doing some simple substitutions. First you
should replace some words by their abbreviations. Below is a list of
the word substitutions you should make.

later -> l8r
at -> @
that -> th@
to -> 2
for -> 4
one -> 1
see -> c
you -> u
be -> b
not -> !
love -> luv

Note that you should *not* replace portions of words using the above rules.
For example, do not change "into" to "in2".

After making the word substitutions, you should replace double s by $,
double t by #. Thus, "kiss" becomes "ki$", "pretty" becomes "pre#y".

Input Format
------------

The first line of the input file will contain the number of words in
the passage to be translated. All input will be presented in
lower-case letters, and there will be no punctuation. Words will be
separated by spaces or carriage returns.

Output Format
-------------

You should output the encoded message with the same spacing as the
original message.

Sample Input (for Valentine's Day)
------------

63
to be or not to be

see you later

sometimes with one i love i fill myself with rage
for fear i effuse unreturnd love
but now i think there is no unreturnd love
the pay is certain one way or another
i loved a certain person ardently
and my love was not returnd
yet out of that i have written these songs

Output for Sample Input
-----------------------

2 b or ! 2 b

c u l8r

sometimes with 1 i luv i fill myself with rage
4 fear i effuse unreturnd luv
but now i think there is no unreturnd luv
the pay is certain 1 way or another
i loved a certain person ardently
and my luv was ! returnd
yet out of th@ i have wri#en these songs

1 comments:

Anurag said...

solution in c++:
#include iostream
#include string
#include sstream
#include cctype
using namespace std;



const int WORD = 0;
const int NON_WORD = 1;

void process_word(string);

int main() {
string s;
getline(cin, s);
while(getline(cin, s)) {
istringstream words_in(s);
string word;
bool first_word = true;
while (words_in >> word) {
if (!first_word) cout << ' ';
else first_word = false;
process_word(word);
}
cout << '\n';
}
}

void process_word(string s) {
if (s == "later") cout << "l8r";
else if (s == "at") cout << "@";
else if (s == "that") cout << "th@";
else if (s == "to") cout << "2";
else if (s == "for") cout << "4";
else if (s == "one") cout << "1";
else if (s == "see") cout << "c";
else if (s == "you") cout << "u";
else if (s == "be") cout << "b";
else if (s == "not") cout << "!";
else if (s == "love") cout << "luv";
else {
bool last_was_s = (s[0] == 's');
bool last_was_t = (s[0] == 't');
if (!(last_was_s || last_was_t)) cout << s[0];
for (int i = 1; i < s.size(); i++) {
if (last_was_s) {
if (s[i] == 's') cout << "$";
else cout << "s" << s[i];
last_was_s = false;
}
else if (last_was_t) {
if (s[i] == 't') cout << "#";
else cout << "t" << s[i];
last_was_t = false;
}
else {
last_was_s = (s[i] == 's');
last_was_t = (s[i] == 't');
if (!(last_was_s || last_was_t)) cout << s[i];
}
}
if (last_was_s) cout << "s";
else if (last_was_t) cout << "t";
}
}

Post a Comment