hi, help me write a c++ program. We assume the existence of some "Alien Language". In…
- User
hi, help me write a c++ program.
We assume the existence of some "Alien Language". In this language, the words are created in specific way, following some rules. You do not need to understand what these words mean. You are just required to write a program which takes as input a string, parses it (i.e., analyzes it), and outputs whether this string is a correct "Alien" word or an incorrect one, obeying the rules we outline below.
Rules Every word in the "Alien Language" obeys the rules below:
The only characters used in the alien language are lowercase alphabets 'a' to 'z', and three more symbols '@', '#', '|' A string that contains only one of the lowercase alphabets ('a' to 'z') is a correct word: 'a' to 'z' If string s is a correct word, then so are s@ and s# If s and t are correct words, then so are st| If s and t are correct words, then so are #st Rules 1 to 5 are the only rules that define a correct word. If a word cannot be verified using these rules, then the word is incorrect.
Here is the skeleton, help me to finish it based on the requirement. /*
- File: lab6_skeleton.cpp
- Lab 6: Recursion - Alien Word Parsing */
#include #include
using namespace std;
const int MAX_LEN = 256;
bool correct_word(char word[], int s, int l) {
}
int main() { char word[MAX_LEN]; char c;
do{ cout << "Enter a word: "; cin >> word; if (correct_word(word, 0, strlen(word) - 1)) cout << "The word is CORRECT!\n"; else cout << "The word is INCORRECT!\n"; cout << "Do you want to enter a new word ('Y'/'y' for Yes, 'N'/'n' for No)? "; cin >> c; } while (c == 'Y' || c=='y');
return 0; }
- Assistant
Here is the complete implementation of the program:
#include <iostream> #include <cstring> using namespace std; const int MAX_LEN = 256; bool is_lowercase_alphabet(char c) { return (c >= 'a' && c <= 'z'); } bool correct_word(char word[], int start, int end) { // Base case: single lowercase alphabet character is a correct word if (start == end && is_lowercase_alphabet(word[start])) { return true; } // Recursive cases else if (end - start >= 1) { // Rule 2: s@ is a correct word if s is a correct word if (word[end] == '@' && correct_word(word, start, end - 1)) { return true; } // Rule 3: s# is a correct word if s is a correct word else if (word[end] == '#' && correct_word(word, start, end - 1)) { return true; } // Rule 4: st| is a correct word if both s and t are correct words else if (word[end] == '|' && correct_word(word, start, end - 1)) { for(int i = start; i < end; i++) { if (correct_word(word, start, i) && correct_word(word, i + 1, end - 1)) { return true; } } } // Rule 5: #st is a correct word if both s and t are correct words else if (word[start] == '#' && correct_word(word, start + 1, end)) { for(int i = start + 1; i <= end; i++) { if (correct_word(word, start + 1, i) && correct_word(word, i + 1, end)) { return true; } } } } // Rule 1: single lowercase alphabet character is a correct word else if (start == end && is_lowercase_alphabet(word[start])) { return true; } // If none of the rules match, the word is incorrect return false; } int main() { char word[MAX_LEN]; char c; do{ cout << "Enter a word: "; cin >> word; if (correct_word(word, 0, strlen(word) - 1)) { cout << "The word is CORRECT!\n"; } else { cout << "The word is INCORRECT!\n"; } cout << "Do you want to enter a new word ('Y'/'y' for Yes, 'N'/'n' for No)? "; cin >> c; } while (c == 'Y' || c=='y'); return 0; }