Hi, I am new to C++ programming and I have come across my first dilemma. I am doing a project and I need to add up the letters of a word i.e text string so that it gives me a mathematical answer. For example the word cake will give me four letters so I need to return an answer of four. Can you help me, please. Thank you.
if you're using a char array, you can use strlen to find the number of chars; if you're using the string class, you can use the member function length to find the number of chars. Code: ... // bits of code #include <string> // string class #include <cstring> // c string functions ... // bits of code char c_word[] = "test"; std::string s_word = "test"; strlen(c_word); // used on terminated char arrays or strlen(s_word.c_str()); // can be used on string class objects or s_word.length(); // applies to string class objects only