Conjugation program

I'm trying to create a program that will conjugate Japanese verbs. Japanese verbs are extremely regular (there's only two exceptions in the whole language) and they follow a very specific pattern for conjugating. I intend to use a switch to alter a string input by the user depending on the desired conjugation (eg, 1 for past, 2 for gerund, etc). Japanese verbs are conjugated based on the last two or three letters in their infinitive form, so my question is this: How can I make the program sort the verb based on the last three characters of the input string? Any help would be greatly appreciated.
Last edited on
What do you mean by "sort the verb"? Certainly we aren't talking about sorting a collection because you only have one verb. If you mean to request a way to check out the last 3 letters, you can do that simply using operator[] of the std::string or std::wstring class. Method std::string::size() or std::wstring::size() can tell you the size of the string.
That's exactly what I needed, thank you. The problem is, I'm really new to all of this, could you explain how that works? I haven't had much experience working with strings.
There's good documentation about std::string in this site. See the Reference section. FYI, std::string and std::wstring work exactly the same, except that the latter one uses wide chars (wchar_t) instead of the native char data type. This allows for Unicode UTF-16. I guess your particular project benefits from Unicode, so I suggest you read about std::string in this site but remember to use std::wstring and wide chars all the way.

In a nutshell, you can access individual characters using operator[]:

1
2
3
std::wstring myString = L"Hello STL String class!";
//Access the fourth character:
std::wcout << L"The fourth character is " << myString[3] << std::endl;


But yes, you will most likely be using std::getline() to read the verb from the keyboard, so you don't really know the length of the string, and there may very well be NO such thing as a fourth character! That would mean that the above code would crash and burn because it tried to access an illegal memory position (memory that hasn't been allocated). In reality the string class does a check and will throw an exception. But yes, in practice is the same: Your program will crash.

So there's this method called size() that tells you the length of the string. First read the user input (using std::getline() I would suggest), then check the string's length (via size()). Make sure it is a legal size. Are there verbs in Japanese that are less than 3 characters? If no, then that would be a first check to make sure the user wrote the thing correctly.

Once you have a valid string, you can access the last three characters one by one:

1
2
3
4
wchar_t c3 = myString[myString.size() - 3];
wchar_t c2 = myString[myString.size() - 2];
wchar_t c1 = myString[myString.size() - 1];
//Now you have the individual characters stored in variables c1, c2 and c3. 


Or you can use the string's method substr() to extract all three characters at once. You can then compare the extracted substring against predefined terminations (if that's what you need to do). I don't really know how things work so you decide which method is better for you.
Thank you sooo much. Substr() turned out to be exactly what I needed. Now it's just a matter of adding a switch for the two exceptions that exist in Japanese, but it's nothing I haven't done before. Thanks!
Topic archived. No new replies allowed.