]> Softwares of Agnibho - anagram.git/blob - anagram.cpp
Removed binaries
[anagram.git] / anagram.cpp
1 /**********************************************************************
2 * Title: Anagram
3 * Description: Application for finding anagrams of a word
4 * Author: Agnibho Mondal
5 * Website: http://code.agnibho.com/anagram
6 **********************************************************************
7 Copyright (c) 2013-2015 Agnibho Mondal
8 All rights reserved
9 **********************************************************************
10 This file is part of Anagram.
11
12 Anagram is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 Anagram is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with Anagram. If not, see <http://www.gnu.org/licenses/>.
24 **********************************************************************/
25 /*
26 *The Command Line Interface for Anagram
27 */
28
29 #include <iostream> //For console output
30 #include <string> //For string variables
31 #include <unistd.h> //For getopt
32 #include <algorithm> //For transform, sort
33 #include <vector> //For vector data
34 #include "validity.hpp" //For is_valid
35 #include "palindrome.hpp" //For is_palindrome
36 #include "check.hpp" //For is_anagram
37 #include "find.hpp" //For find_anagram
38 #include "help.hpp" //For show_help
39
40 using namespace std;
41
42 int main(int argc, char** argv){
43
44 //State variables
45 bool multi=false; //For multi-word
46 bool compare=false; //Check if two words are anagrams of each other
47 bool dict=false; //Check if the word is in the dictionary
48 int palindrome=0; //Values: 0=>don't check palindrome, 1=> check palindrome along with anagram, 2=> check palindrome only
49
50 //Check command line options
51 int c=0;
52 while((c=getopt(argc, argv, "dhmpP"))!=-1){
53 switch(c){
54 case 'd': //Check dictionary
55 dict=true;
56 break;
57 case 'h': //Show help
58 show_help();
59 return 0;
60 case 'm': //Allow multi-line
61 multi=true;
62 break;
63 case 'p': //Check palindrome
64 palindrome=1;
65 break;
66 case 'P': //Check palindrome only
67 palindrome=2;
68 break;
69 }
70 }
71
72 string word; //The string to find anagrams for
73 string against; //The string to check if the word is a anagram of it
74 //First non-option argument
75 if(argv[optind]!=NULL){ //Set the first non-option argument as word
76 word=argv[optind];
77 //Optional second argument
78 if(argv[optind+1]!=NULL){ //Set the second non-option argument as against
79 against=argv[optind+1];
80 compare=true;
81 }
82 }
83 else{ //If no non-option argument present read from stdin
84 cerr<<"Enter word:"<<endl;
85 getline(cin, word);
86 }
87
88 //Check validity of word
89 if(is_valid(word, multi)){
90
91 //Convert to lower case
92 transform(word.begin(), word.end(), word.begin(), ::tolower);
93
94 if(palindrome==2){ //Don't check anagram if only palindrome is requested.
95 //Check palindrome
96 if(is_palindrome(word)){
97 cout<<word<<": The word is a Palindrome"<<endl;
98 }
99 else{
100 cout<<word<<": The word is NOT a Palindrome"<<endl;
101 }
102 }
103 else{
104 if(palindrome==1){ //Check palindrome if requested.
105 //Check palindrome
106 if(is_palindrome(word)){
107 cout<<word<<": The word is a Palindrome"<<endl;
108 }
109 else{
110 cout<<word<<": The word is NOT a Palindrome"<<endl;
111 }
112 }
113 if(compare){ //Don't find all anagrams if only comparison is requested.
114 //Check anagram
115 if(is_anagram(word, against)){
116 cout<<word<<": "<<against<<": The words are anagrams of each other"<<endl;
117 }
118 else{
119 cout<<word<<": "<<against<<": The words are NOT anagrams of each other"<<endl;
120 }
121 }
122 else{ //Look for anagrams in the dictionary
123 vector<string> anagram_list; //Container for anagrams
124 if(multi){ //Multi-word anagram
125 anagram_list=find_anagram_multiword(word); //Finds anagrams
126 }
127 else{ //Single-word anagram
128 anagram_list=find_anagram(word); //Finds anagrams
129 }
130 //Check the word itself from the anagram list
131 {
132 vector<string>::iterator i=find(anagram_list.begin(), anagram_list.end(), word);
133 if(i==anagram_list.end()){ //If the word itself is not in the anagram list
134 if(dict){
135 cout<<word<<": The word is NOT present in the dictionary"<<endl;
136 }
137 }
138 else{ //If the word itself is present in the anagram list
139 if(dict){
140 cout<<word<<": The word is present in the dictionary"<<endl;
141 }
142 anagram_list.erase(i); //Remove the word itself from the anagram list
143 }
144 }
145 //Print out the angrams if present
146 if(anagram_list.size()>0){
147 if(anagram_list.size()>1){ //If more than one anagrams
148 cerr<<word<<": The following "<<anagram_list.size()<<" anagrams were found-"<<endl;
149 }
150 else{ //If only one anagram
151 cerr<<word<<": The following anagram was found-"<<endl;
152 }
153 for(string i:anagram_list){
154 cout<<i<<endl;
155 }
156 }
157 else{
158 cerr<<word<<": No anagrams were found"<<endl;
159 }
160 }
161 }
162 }
163 else{
164 cerr<<"Invalid word: "<<word<<endl; //Exit with error if the word is not valid.
165 return 1;
166 }
167 return 0;
168 }