-1

Let's suppose I write the code..

char arr[20];
gets(arr);

Now, the case is when I give input of less than 20 characters. (Eg.- "hello there" is of 11 characters). How to determine the exact length that we have typed? I know we can do it using loops but I'm asking if we have any other method.

  • 5
    "I know we can do it using loops" - how would you do this with loops? Also, the (total) size of an array is different from the length of a string you wrote into that array – ForceBru Feb 04 '21 at 10:36
  • 3
    Did you search before posting? [How do I find the length of an array?](https://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array) – underscore_d Feb 04 '21 at 10:37
  • Do you really mean an array generally, or do you actually mean a null-terminated string specifically? If the latter, then `std::strlen()`, also very easy to search for. Either way, there are `std::array`, `std::vector`, and `std::string`, which can/should replace any uses of C arrays. – underscore_d Feb 04 '21 at 10:39
  • @ForceBru int c=0; for(int i=0;arr[i]!='\0';i++) { c+=1; } cout< – Saurabh Bora Feb 04 '21 at 10:53
  • 1
    @SaurabhBora, the `strlen` function from the C standard library does almost the same thing, so this is the way. Or, since your question is tagged [tag:c++], use `std::string` that can calculate its length itself. – ForceBru Feb 04 '21 at 10:59
  • Despite the current title speaking of "size of an array", the question is. How to determine the size of a string literal? -> string_view::size/strlen. – Adrian Maire Feb 04 '21 at 13:10

3 Answers3

1

Never use gets. It has been deprecated since C++11, and has been removed from the language since C++14. It is dangerous since it relies on the user input to not overflow the array... which is a huge security vulnerability in the worst case, and an annoyingly unnecessary limitation at best.

Here is a better alternative:

std::string str;
std::cin >> str;
std::size_t length = str.size();

Now, the case is when I give input of less than 20 characters.

Note that if user input isn't less than 20 characters, then the behaviour of your program is undefined.


How to find the size of an array in c++?

char arr[20];

The size of this array is 20. You can use std::size to get it programmatically in order to not rely on magic numbers.

std::size_t size = std::size(arr); // 20

How to determine the exact length that we have typed?

gets null terminates the input string. You can get the length of a null terminated string by linearly searching for the null character and counting the number of non-null characters on the way. There is a standard function for this:

std::size_t length = std::strlen(arr);
eerorika
  • 223,800
  • 12
  • 181
  • 301
0

First, lets initialize the char array to have all of its elements to be 0.

for(int i = 0; i < 20; i++) arr[i] = 0;

Now we can iterate through the array to find where we find the value 0 in. If we find it, it means that were in the end of the string. We can create a simple function for this, it'll look like this.

int FindStrLen(const char* length, int maxLen)
{
    for(int i = 0; i < maxLen; i++)
        if(len[i] == 0)
            return i;

    // If not found, return the maxLen.
    return maxLen;
}

The C++ STL provides a function to do this for you. Its called std::strlen. Or else you can use std::string and it has done all that for you.

D-RAJ
  • 3,078
  • 2
  • 5
  • 22
0

Don't use raw arrays!

Use std::array instead:

#include <array>
#include <iostream>

int main()
{
    std::array<char,20> arr;
    std::cout << "Length: " << arr.size() << std::endl;
}

There is literally no benefit in using raw arrays now a day in C++ (except maybe compatibility with C++03)


EDITED:

Now, the case is when I give input of less than 20 characters. (Eg.- "hello there" is of 11 characters). How to determine the exact length that we have typed?

Here we have several questions hidden:

  • How to make a variable-length (runtime) char array to get input from the user?
  • How to get the length of a string?

How to make a variable-length (runtime) array?

For non-char, containers like std::vector would help. In the case of a string, an ever better option is std::string.

Also, using streams to get the input from the user, most of those problems are solved.

How to get the length of a raw string?

Understanding raw-string as a sequence of char on a buffer, ended with an \0 nil character.

With std::string_view, we can use most of the std::string features on a raw char array:

#include <array>
#include <iostream>
#include <string_view>

int main()
{
    std::array<char,20> arr{"This is a string"};
    
    std::string_view strView(arr.data());
    
    std::cout << "'" << strView << "' of length " << strView.size() << std::endl;

    return 0;
}

Which output: 'This is a string' of length 16

Adrian Maire
  • 13,006
  • 9
  • 37
  • 80
  • 1
    That would answer the title of the question. But not the actual question which is `Now, the case is when I give input of less than 20 characters. (Eg.- "hello there" is of 11 characters). How to determine the exact length that we have typed?` – t.niese Feb 04 '21 at 11:03