Any other way to access strings without using an array?

int main() { clrscr(); char c[80],d[80]; cout<<"Enter a string = "; cin.get(a,80); strcpy(c,a); strrev(a); if(strcmp(c,a)==0) cout<<"String = "<<c<< "is palindrome."; else cout<<c<<" is not palindrome"; getch(); return 0; } 

i.e. is there any other way to easily accomplish this task without using an array or another way?

+6
source share
9 answers
 #include <string> #include <algorithm> #include <iostream> int main() { std::string s; getline(std::cin, s); if (std::equal(s.begin(), s.end(), s.rbegin())) std::cout << s << " is a palindrome\n"; else std::cout << s << " is not a palindrome\n"; } 

No arrays, no pointers.

+10
source
 bool is_palindrome(const char* s) { const char *p = s; const char *q = s + strlen(s) - 1; while (p < q) { if (*p != *q) return false; ++p; --q; } return true; } 
+8
source

My solution: (not that efficient, but just another “other” solution)

 bool is_palindrome(const std::string& s) { struct local { static bool work(const std::string& s, int l, int h) { return l>= h? true: (s[l] == s[h]? work(s, l + 1, h -1): false); } }; return local::work(s, 0, s.size() - 1); } //usage cout << is_palindrome("liril"); //just pass one argument. that it! 

Demo: http://www.ideone.com/WNuEC

+2
source

After C ++ 11, the variadic pattern brought a great new feature to solve such problems. With C ++ 17, the solution got even better.

Here is my template using a folding expression for a palindrome:

 template <typename ...ARG> bool isPalindrome(ARG ...args) { std::string temp1 = ""; ((temp1 += args), ...); std::string temp2 = ""; ((temp2 = args + temp2), ...); return temp1 == temp2; } int main(int argc, char *argv[]) { std::cout << isPalindrome('e','y', ' ', 'e', 'd','i','p',' ','a','d','a','n','a','d','a',' ','p','i','d','e',' ','y','e') << std::endl; return 0; } 
+1
source

You asked about changing the title bar, but it looks like you are checking palindromes?

There are several collection classes that will work.

You can implement reverse access using std: map. Store letters as pairs of numeric indices and single characters. When you create a map, give it a sort function to arrange it in reverse order, sorting it back by letter index.

If you really need to change the order of the letters in the list, you can put the same pairs in the std: vector and resort to them using the same trick, but with two different comparison functions.

The most effective way to do this is probably what you already have. The previous two methods have a lot of extra overhead for this task.

0
source

You can use iterators if you just want to test palindromes.

 > #include <string> using std::string; using std::getline; > > #include <iostream> using std::cin; using std::cout; > > int main() { > string s; > getline(cin, s); > > string::reverse_iterator rit; > string::iterator it=s.begin(); > for(rit=s.rbegin(); rit<s.rend(); rit++) > { > if(*rit==*it) > it++; > else > { > cout << s << " is not a palindrome\n"; > exit(0); > } > > } > > cout << s << " is a palindrome\n"; > exit(0); > > } 
0
source

You may try:

 int isPalin ( char *str ) { int i, len=strlen(str); for (i=0; i<len/2; ++i) if (str[i]!=str[len-i-1]) break; return i==len/2; } 

It is pure C and quite efficient. Requires time complexity of O (1) and O (n).

0
source
 public static void palindrome(string a) { bool y = true; for (int i = 0; i <= ((a.Length) / 2) && y; y = a[++i] == a[a.Length - (i + 1)]) ; Console.WriteLine(y ? "Palindrome" : "Not Palindrome"); } 
0
source

After C ++ 11, the variadic pattern brought a great new feature to solve such problems. With C ++ 17, the solution got even better.

Here is my template using a folding expression for a palindrome:

 template <typename ...ARG> bool isPalindrome(ARG ...args) { std::string temp1 = ""; ((temp1 += args), ...); std::string temp2 = ""; ((temp2 = args + temp2), ...); return temp1 == temp2; } int main(int argc, char *argv[]) { std::cout << isPalindrome('e','y', ' ', 'e', 'd','i','p',' ','a','d','a','n','a','d','a',' ','p','i','d','e',' ','y','e') << std::endl; return 0; } 
0
source

Source: https://habr.com/ru/post/886415/


All Articles