logo elektroda
logo elektroda
X
logo elektroda

c++ - A program that displays the number of letters in a word

Yerild 13185 6
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 13536154
    Yerild
    Level 9  
    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  
    And what can't you do, read the name on the keyboard or count the letters?
  • ADVERTISEMENT
  • #3 13536226
    Yerild
    Level 9  
    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
  • ADVERTISEMENT
  • #5 21313583
    pseudocfoch
    Level 2  
    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++.
  • #6 21315637
    JacekCz
    Level 42  
    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
  • #7 21689366
    Matx91
    Level 9  
    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

The discussion centers on creating a simple C++ program that accepts a first and last name input and outputs the total number of letters in both names. The original poster requested uncomplicated code to achieve this. Responses include a basic C++ code snippet utilizing the standard string class and its length() method to count characters. Additional suggestions highlight multiple methods to handle string length in C++, referencing official documentation for the string class. Some contributors question the choice of C++ for this task, suggesting Python as a simpler alternative unless performance at scale is required. The conversation also touches on the importance of reading documentation and clarifies that some shared code examples may not fully meet the requirement of displaying the total letter count.
Summary generated by the language model.
ADVERTISEMENT