Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cctype>
- //Krampacki nacin, i nije bas efektan, posto ima dosta kopiranja, ali eto moze proci za manje stringove
- /*bool DaLiJePalindrom(std::string s)
- {
- std::string s1;
- for(int i(0); i < s.length(); i++)
- if(s[i] != ' ')
- s1.push_back(s[i]);
- std::string s2;
- for(int i(s1.length()-1); i >= 0; i--)
- s2.push_back(s1[i]);
- for(int i(0); i < s1.length(); i++)
- if(tolower(s1[i]) != tolower(s2[i]))
- return false;
- return true;
- }*/
- //Vrlo elegantna varijanta provjeravanja, samo preskace sve sto nije slovo
- bool DaLiJePalindrom(std::string s)
- {
- if(s.length() == 0)
- return false;
- if(s.length() == 1)
- return true;
- for(int i(0), j(s.length()-1); i < j; i++, j--)
- {
- if(!isalpha(s[i]))
- i++;
- if(!isalpha(s[j]))
- j--;
- if(tolower(s[i]) != tolower(s[j]))
- return false;
- }
- return true;
- }
- int main()
- {
- std::string s;
- std::cout << "Unesi neki string: " << std::endl;
- std::getline(std::cin, s);
- std::cout << "Uneseni string ";
- DaLiJePalindrom(s) ? (std::cout << "je palindrom.") : (std::cout << "nije palindrom.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement