]>
Softwares of Agnibho - anagram.git/blob - anagram.cpp
1 /**********************************************************************
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
9 **********************************************************************
10 This file is part of Anagram.
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.
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.
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 **********************************************************************/
26 *The Command Line Interface for Anagram
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
42 int main(int argc
, char** argv
){
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
50 //Check command line options
52 while((c
=getopt(argc
, argv
, "dhmpP"))!=-1){
54 case 'd': //Check dictionary
60 case 'm': //Allow multi-line
63 case 'p': //Check palindrome
66 case 'P': //Check palindrome only
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
77 //Optional second argument
78 if(argv
[optind
+1]!=NULL
){ //Set the second non-option argument as against
79 against
=argv
[optind
+1];
83 else{ //If no non-option argument present read from stdin
84 cerr
<<"Enter word:"<<endl
;
88 //Check validity of word
89 if(is_valid(word
, multi
)){
91 //Convert to lower case
92 transform(word
.begin(), word
.end(), word
.begin(), ::tolower
);
94 if(palindrome
==2){ //Don't check anagram if only palindrome is requested.
96 if(is_palindrome(word
)){
97 cout
<<word
<<": The word is a Palindrome"<<endl
;
100 cout
<<word
<<": The word is NOT a Palindrome"<<endl
;
104 if(palindrome
==1){ //Check palindrome if requested.
106 if(is_palindrome(word
)){
107 cout
<<word
<<": The word is a Palindrome"<<endl
;
110 cout
<<word
<<": The word is NOT a Palindrome"<<endl
;
113 if(compare
){ //Don't find all anagrams if only comparison is requested.
115 if(is_anagram(word
, against
)){
116 cout
<<word
<<": "<<against
<<": The words are anagrams of each other"<<endl
;
119 cout
<<word
<<": "<<against
<<": The words are NOT anagrams of each other"<<endl
;
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
127 else{ //Single-word anagram
128 anagram_list
=find_anagram(word
); //Finds anagrams
130 //Check the word itself from the anagram list
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
135 cout
<<word
<<": The word is NOT present in the dictionary"<<endl
;
138 else{ //If the word itself is present in the anagram list
140 cout
<<word
<<": The word is present in the dictionary"<<endl
;
142 anagram_list
.erase(i
); //Remove the word itself from the anagram list
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
;
150 else{ //If only one anagram
151 cerr
<<word
<<": The following anagram was found-"<<endl
;
153 for(string i
:anagram_list
){
158 cerr
<<word
<<": No anagrams were found"<<endl
;
164 cerr
<<"Invalid word: "<<word
<<endl
; //Exit with error if the word is not valid.