elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Quitar acentos de un string
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: 1 [2] 3 Ir Abajo Respuesta Imprimir
Autor Tema: Quitar acentos de un string  (Leído 15,319 veces)
ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Quitar acentos de un string
« Respuesta #10 en: 10 Julio 2015, 00:48 am »

El error en la instrucción de antes. El error da más información.

A todo esto, pusiste

Código
  1. cadena == 'á'

¿cadena es un char? ¿No será un char*?


En línea

Stakewinner00


Desconectado Desconectado

Mensajes: 1.426



Ver Perfil WWW
Re: Quitar acentos de un string
« Respuesta #11 en: 10 Julio 2015, 00:48 am »

Eso es lo mismo que hacer
Código
  1. int n;
  2. if ( n == 5.55)
esta fuera de rango

Te refieres a este warning
Citar
warning: multi-character character constant [-Wmultichar]
? En ese caso no es un error, es un warning, que esta bien tratarlos como errores pero no lo son, aunque no se que mensaje te salio y como lo trato el compilador


En línea

ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Quitar acentos de un string
« Respuesta #12 en: 10 Julio 2015, 00:49 am »

¿Qué compilador utilizas? ¿Qué codificación tiene el fichero .cpp?
En línea

user-marcos

Desconectado Desconectado

Mensajes: 159


Ver Perfil
Re: Quitar acentos de un string
« Respuesta #13 en: 10 Julio 2015, 01:01 am »

Código
  1.  string s = "á";
  2.  if(s == 'á')
  3.    cout << "\nok";
  4.  
el error:
a.cc:14:11: warning: multi-character character constant [-Wmultichar]
   if(s == 'á')
           ^
a.cc: In function ‘int main()’:
a.cc:14:8: error: no match for ‘operator==’ (operand types are ‘std::string {aka std::basic_string<char>}’ and ‘int’)
   if(s == 'á')
        ^
a.cc:14:8: note: candidates are:
In file included from /usr/include/c++/4.8/iosfwd:40:0,
                 from /usr/include/c++/4.8/ios:38,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/postypes.h:216:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
     ^
/usr/include/c++/4.8/bits/postypes.h:216:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const std::fpos<_StateT>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/stl_pair.h:214:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
     operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^
/usr/include/c++/4.8/bits/stl_pair.h:214:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const std::pair<_T1, _T2>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator==(const reverse_iterator<_Iterator>& __x,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:291:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const std::reverse_iterator<_Iterator>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator==(const reverse_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:341:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const std::reverse_iterator<_Iterator>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/string:41:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/allocator.h:128:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_T2>&)
     operator==(const allocator<_T1>&, const allocator<_T2>&)
     ^
/usr/include/c++/4.8/bits/allocator.h:128:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const std::allocator<_CharT>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/string:41:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/allocator.h:133:5: note: template<class _Tp> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
     ^
/usr/include/c++/4.8/bits/allocator.h:133:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const std::allocator<_CharT>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/basic_string.h:2486:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2486:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘int’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/basic_string.h:2493:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
     operator==(const basic_string<_CharT>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2493:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   mismatched types ‘const std::basic_string<_CharT>’ and ‘int’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/basic_string.h:2507:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator==(const _CharT* __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2507:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   mismatched types ‘const _CharT*’ and ‘std::basic_string<char>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/basic_string.h:2519:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8/bits/basic_string.h:2519:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   mismatched types ‘const _CharT*’ and ‘int’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/bits/locale_facets.h:48:0,
                 from /usr/include/c++/4.8/bits/basic_ios.h:37,
                 from /usr/include/c++/4.8/ios:44,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/streambuf_iterator.h:204:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
     operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
     ^
/usr/include/c++/4.8/bits/streambuf_iterator.h:204:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const std::istreambuf_iterator<_CharT, _Traits>’
   if(s == 'á')
           ^
In file included from /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++allocator.h:33:0,
                 from /usr/include/c++/4.8/bits/allocator.h:46,
                 from /usr/include/c++/4.8/string:41,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/ext/new_allocator.h:139:5: note: template<class _Tp> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
     operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
     ^
/usr/include/c++/4.8/ext/new_allocator.h:139:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/stl_iterator.h:811:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
     operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:811:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’
   if(s == 'á')
           ^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8/bits/char_traits.h:39,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from a.cc:1:
/usr/include/c++/4.8/bits/stl_iterator.h:805:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
     ^
/usr/include/c++/4.8/bits/stl_iterator.h:805:5: note:   template argument deduction/substitution failed:
a.cc:14:11: note:   ‘std::string {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’
   if(s == 'á')
En línea

ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Quitar acentos de un string
« Respuesta #14 en: 10 Julio 2015, 01:04 am »

Código:
(operand types are ‘std::string {aka std::basic_string<char>}’ and ‘int’)

Estás comparando una string con un caracter. No puedes hacer eso. O haces s[0], o haces "á".
En línea

user-marcos

Desconectado Desconectado

Mensajes: 159


Ver Perfil
Re: Quitar acentos de un string
« Respuesta #15 en: 10 Julio 2015, 01:24 am »

Tengo una posible solución PROVISIONAL aunque puede desbordar:

Código
  1. #include<iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5.  
  6. bool cod_aux(string &s);
  7. void cod(string &s);
  8.  
  9.  
  10. int main()
  11. {
  12.  
  13.  string j = "áóÁsÚíu";
  14.  cod(j);
  15.  cout << endl << j;//-> aoAsUiu
  16. }
  17.  
  18.  
  19. void cod(string &s)
  20. {
  21.  string aux = "";
  22.  string aux2 = "";
  23.  bool add = false;
  24.  char auxc;
  25.  for(int i = 0; i < s.length(); i++)
  26.  {
  27.    aux +=s[i];
  28.    aux +=s[i+1];
  29.    if(cod_aux(aux) == false)
  30.    {
  31.     aux2 += aux;
  32.     i = i +1;
  33.    }
  34.    else aux2 +=s[i];
  35.  
  36.      aux = "";
  37.  }
  38.    s = aux2;
  39. }
  40.  
  41.  
  42. bool cod_aux(string &s)
  43. {
  44.  string aux = s;
  45.  if(s == "á")
  46.    s = "a";
  47.  if(s == "Á")
  48.     s = "A";
  49.  if(s == "é")
  50.    s = "e";
  51.  if(s == "É")
  52.     s = "E";
  53.  if(s == "í")
  54.    s = "i";
  55.  if(s == "Í")
  56.    s = "I";
  57.  if(s == "Ó")
  58.     s = "O";    
  59.  if(s == "ó")
  60.     s = "o";
  61.  if(s == "Ú")
  62.     s = "U";    
  63.  if(s == "ú")
  64.     s = "u";
  65.   return aux == s;
  66. }
  67.  
En línea

ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Quitar acentos de un string
« Respuesta #16 en: 10 Julio 2015, 01:33 am »

Código
  1. aux +=s[i+1];

Eso puede acceder a s[ s.length() ].

En cualquier caso, string::find() será más interesante que usar tal cadena de if().

Además, trata de hacer comparaciones y asignaciones con caracteres y no con strings. Tal vez simplemente tengas una extraña codificación.
En línea

Stakewinner00


Desconectado Desconectado

Mensajes: 1.426



Ver Perfil WWW
Re: Quitar acentos de un string
« Respuesta #17 en: 10 Julio 2015, 01:44 am »

Además, trata de hacer comparaciones y asignaciones con caracteres y no con strings. Tal vez simplemente tengas una extraña codificación.
Si no recuerdo mal á é y demás se almacenan internamente como const char* así que ya esta bien comparar con strings creo.
En línea

geeke

Desconectado Desconectado

Mensajes: 93


Ver Perfil
Re: Quitar acentos de un string
« Respuesta #18 en: 10 Julio 2015, 01:46 am »

Código
  1. if( cadena == 'á')
  2.  
no se puede, y ami me sale que las vocales con acento valen dos char

Los caracteres acentuados se componen de dos bytes, mientras los sin acento se representan con un solo byte, intenta probando esto

Código
  1. if( cadena == (unsigned char)('á'))

Saludos



En línea

ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Quitar acentos de un string
« Respuesta #19 en: 10 Julio 2015, 01:47 am »

Si no recuerdo mal á é y demás se almacenan internamente como const char* así que ya esta bien comparar con strings creo.

Si pero bueno, es preferible comparar con char que es más eficiente xD

De todas formas, user_marcos, prueba con wstring, que utiliza wide char.
En línea

Páginas: 1 [2] 3 Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines