Letter to lowercase conversion gives a numerical result

Asked

Viewed 57 times

1

Which part of this code C++ I’m missing where I need to convert a whole word to lowercase?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ctype.h>


using namespace std;

int main(){

   string nome = "Pedro";

   int tam = nome.size();

   int i;

        for(i = 0 ; i < tam ; i++){
            cout << tolower(nome[i]);
        }

    return 0;
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

Besides the precise disorganized code means you want a letter and not a number because according to the documentation that’s what the function tolower() results by default.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string nome = "Pedro";
    int tam = nome.size();
    for (int i = 0 ; i < tam ; i++) cout << tolower(nome[i], locale());
}

But if you want to use modern C++ :

#include <iostream>
#include <string>
using namespace std;

int main() {
    for (auto letra : "Pedro") cout << tolower(letra, locale());
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.