logo elektroda
logo elektroda
X
logo elektroda

[C++] How to count letters in a first and last name?

Yerild 13746 6
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 13536154
    Yerild
    Level 9  
    Posts: 32
    Rate: 13
    Hello everyone. I need help in writing a program where I could enter a first and last name, and then the program would print out the number of letters of that first and last name. Unfortunately, I can't write it, I would like some not very complicated code.
  • ADVERTISEMENT
  • #2 13536219
    D12
    Level 14  
    Posts: 142
    Help: 8
    Rate: 5
    And what can't you do, read the name on the keyboard or count the letters?
  • ADVERTISEMENT
  • #3 13536226
    Yerild
    Level 9  
    Posts: 32
    Rate: 13
    Okay, I've managed it :)

    Here is the code (may be useful to someone)

    [code:1:621e19b84e]#include
    #include
    using namespace std;

    int main()
    {
    string st;
    int dl;
    coutst;
    dl=st.length();
    cout
  • ADVERTISEMENT
  • #5 21313583
    pseudocfoch
    Level 2  
    Posts: 112
    Rate: 1
    Why in C++? You can do it in Python easier and fast. Unless you need to repeat this process millions of times, then you may consider C++.
  • ADVERTISEMENT
  • #6 21315637
    JacekCz
    Level 42  
    Posts: 8670
    Help: 760
    Rate: 1464
    the_fifth_horseman wrote:
    Read the documentation:


    Seriously, you perceive the questioner @Yerild as someone who reads the documentation ?

    Added after 6 [hours] 3 [minutes]:

    Yerild wrote:
    cin>>st;
    dl=st.length();
    cout<< "The number of letters of your name'"<<st<<"' is"<<dlt<<endl;
    .

    Jean-Marie ... does not display number of letters
  • Correcting the flawed char division approach

    #7 21689366
    Matx91
    Level 9  
    Posts: 30
    In C on numbers I would do it like this:
    Depends on the number of letters. If there are, say, 5, then the first character is a variable divided by 1000. That's on numbers. And when it comes to the alphabet, it has 32 letters - you have to divide the number of digits by 32 and multiply by the letters it puts out. E.g. Matx, I want M then I make the variable digit=char/(32) and I have the first one and if I want the last one I guess digit=char/(32*4) because x is the fourth character,because there are four letters. And this is how I would solve it. Greetings. I think I am thinking correctly?

    Added after 5 [hours] 31 [minutes]: .

    In the C language, char is a basic data type used to store a single character that is represented by its numeric value, usually an ASCII code. Although char is single-byte, its default state can be signed (from -128 to 127) or unsigned (from 0 to 255), depending on compiler options or the char type. In the C language, char is also keyed to create strings, which are actually arrays terminated by a special NULL character (¯0), rather than string objects. So you divide by the maximum you can use in an ANCI character

    Added after 3 [minutes]:

    Divide by the maximum number that fits in one character and it should work


    Moderated By gulson:

    The advice is wrong. 3.1.11. Publish posts that do not contain substantive content, contain harmful advice, advice that does not attempt to solve a problem, and publish identical posts that follow or repeat information included in the course of the discussion.

    .

Topic summary

LABEL_AI_GENERATED
The discussion is about a simple C++ program that reads a first and last name and prints the number of letters in the entered name. The suggested solution uses the C++ string type, stores the input in a string variable, and obtains the character count with the length() method. One response points to the C++ string documentation and mentions that there are multiple ways to solve the task. Another comment notes that the problem could be done more easily in Python, while a later reply incorrectly discusses counting letters using numeric division and char values.
AI summary based on the discussion. May contain errors.
ADVERTISEMENT