C/C++, Codes

T9 Spelling

Language implementation : C++.

#include <iostream> 
#include <string>
using namespace std;

string T9[] = {"0", "0", "abc", "def", "ghi",
	         "jkl", "mno", "pqrs", "tuv", "wxyz"};
int main()
{
	string input;
	cout &lt;&lt; &quot;Please enter your text: &quot;;
	getline(cin,input);
	
	int i = 0, j, k, l;
	while (i &lt; input.length())
	{
		if (input[i] == ' ')
		{
			++i;
			continue;
		}	
		//j = counter variable to iterate T9 string arr
		for (j = 0; j &lt; 10; ++j)
		{
			//find the position of the char
			//within the mapped pad[T9]
			l = T9[j].find(input.substr(i,1));
			//break point
			if (l != -1)
				break;
		}
		
		//print out the number to press
		//l = how many times to press to get a key
		//j = the mapped number of key groups
		for (k = 0; k &lt;= l; ++k)
			cout &lt;&lt; j;
		cout &lt;&lt; &quot; &quot;; 
		i++;
	}

	return 0;
}

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