Changement de casse en c++

- -  
 iMaTh -
Bonjour,
Je voulais changer la casse d'un string en c++ pour le mètre complètement en minuscule.

1. for (int i = 0; i < calcul.size(); i++)
2. {
3. const char* a = calcul.c_str();
4. if(*(a+i) > 64 && *(a+i) < 91)
5. *(a+i) += 33;
6. }

Le problème est que avec le const > "error C3892: 'i' : vous ne pouvez pas assigner une variable const" sur la ligne 5., sans le const > "error C2440: 'initialisation' : impossible de convertir de 'const char *' en 'char *'" sur la ligne 3..

Quelqu'un voit comment faire ?
Configuration: Windows Vista
Firefox 2.0.0.12

2 réponses

  1. Mahmah Messages postés 497 Statut Membre 125
     
    Bonjour,

    Pour un truc qui ait une gueule de C++ plus que du C il y a:

    #include <cstdio> // Pour le getchar()
    
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    void myToLower( char &rc )
    {
       rc = tolower( rc );
    }
    
    int main( int argc, char *argv[])
    {
       std::string sBlabla( "BlaBla" );
    
       std::cout << sBlabla << std:: endl;
    
    //   for ( std::string::iterator iter = sBlabla.begin() ; iter != sBlabla.end() ; iter++ )
    //      (*iter) = tolower( *iter );
    
    // Ou alors : (Le premier évite l'écriture de myToLower)
    
       std::for_each( sBlabla.begin(), sBlabla.end(), myToLower );
    
       std::cout << sBlabla << std:: endl;
    	
       getchar();
       return 0;
    }
    

    (Oui bon, okay, tolower j'ai pas trouvé l'équivalent en C++...)

    il y a certainement aussi moyen de faire une boucle sur la taille aussi...
       for ( unsigned int i = 0 ; i != sBlaBla.size() ; i ++ )
          sBlaBla[i] = tolower( sBlaBla[i] );
    


    M.
    0
  2. iMaTh
     
    Cadeau

    //mettre un string tout en minuscule
    void toLower(string *chaine) {
    
        for(unsigned int i=0; i<chaine->size(); i++) {
            if ( chaine->at(i) >= 65 && chaine->at(i) <= 90 ) {
                chaine->at(i) += 32;
            }
        }
    
    }
    
    //mettre un string tout en majuscule
    void toUpper(string *chaine) {
    
        for(unsigned int i=0; i<chaine->size(); i++) {
            if ( chaine->at(i) >= 97 && chaine->at(i) <= 122 ) {
                chaine->at(i) -= 32;
            }
        }
    
    }
    
    0