Crear un programa que dada una secuencia diga si es palíndromo,
palíndromo es secuencia simétrica, ejemplos: 1122, 12321, 1551, ...
Mi duda está en que si el vector no es palíndromo me imprime que no lo es , pero si SÍ lo es no imprime que lo sea.
#include<iostream>
#include<vector>
using namespace std;
bool palindromo(vector<int>v){
int i=0, j=v.size()-1;
while(i<j){
if(v[i]!=v[j]){return false;}}
return true;}
int main(){
int n,e,i;
cout<<"mida";
cin>>n;
vector<int>v(n);
for (i=0;i<n;i++){
cout<<" l'element "<<i<<" ... ";
cin>>e;
v[i]=e;}
if (palindromo(v)){cout<<"el vector es simetrico"<<endl;}
else {cout<<"el vector no es simetrico"<<endl;}
system("pause");
return 0;}
¡Gracias por la ayuda!
nunca alteras la variable de control del while en tu funcion palindromo. prueba con:
if (v[i++] != v[j--]) return false;
gracias xiruko :)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
string str, str2;
cin >> str;
size_t pos = str.find_first_of(" ");
while(pos != string::npos){
str.erase(pos);
pos = str.find_first_of(" ");
}
str2 = str.substr(0, str.length());
reverse(str.begin(), str.end());
if(str2 == str)
cout << "La frase '"<<str<<"' es palindroma." <<endl;
else
cout << "La frase '"<<str<<"' no es palindroma." <<endl;
system("pause");
return EXIT_SUCCESS;
}