How to make the first letters that the user type uppercase?

Asked

Viewed 89 times

1

An example,

joão August

How to make the first letter of the name and surname uppercase:

João Augusto

  • 1

    Suppose there are no unnecessary spaces in your string (at the beginning, at the end or duplicates) change the first character to uppercase and all the others that are preceded by space.

3 answers

1

Thus, you are forcing the user to put the first uppercase letter

char nome[50];

do{
printf("Qual o nome?\n");
scanf("%c", nome);
}while (nome[0]>='a' && nome[0]<='z');

  • 1

    I want it in C++, baby.

0

Some of this

string name;
do{
cout << "Qual o nome?";
getline (cin,name);
}while (nome[0]>='A' && nome[0]<='Z');

0

You can use the function toupper() which takes an integer value corresponding to the ASCII value of a character and returns the ASCII integer value of the uppercase version of that character, if any. This function is set in the header <cctype>.

int toupper (int ch);

Because the function works with integer values, you can use type casting to use character values. You can pass a character as a parameter with the casting (int) and return the value to the same character using casting (char).

For example:

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

int main() {
    string nome;

    cout << "Digite seu nome: ";
    cin >> nome;

    // o primeiro char do nome recebe a versão maiúscula dele mesmo
    nome[0] = (char) toupper( (int) nome[0] );

    cout << nome << endl;
}

I recommend that you prioritize storing the names in strings rather than vectors of char.

To apply to multiple names, you can read using the command getline(cin, nome) and use in any repeat structure, strongly recommend the for.

I hope I’ve helped.

Browser other questions tagged

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