0

I'm a new Computer Science student, and I have a homework question that is as follows:

Write a Function that passes in a C-String and using a pointer determine the number of chars in the string.

Here is my code:

#include <iostream>
#include <string.h>
using namespace std;
const int SIZE = 40;

int function(const char* , int, int);

int main()
{
     char thing[SIZE];
     int chars = 0;

     cout << "enter string. max " << SIZE - 1 << " characters" << endl;
     cin.getline(thing, SIZE);
     int y = function(thing, chars, SIZE);
     cout << y;
}


int function(const char *ptr, int a, int b){
    a = 0;
    for (int i = 0; i < b; i++){
        while (*ptr != '\0'){
            a++;
        }
    }
    return a;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ye0123
  • 3
  • 1
  • 1
    why 2 loops , just one is all you need, and you dont need the argument a, just have `int count = 0` in your function at the start – pm100 Feb 28 '22 at 01:40
  • 2
    `a` and `b` are pointless in that function as arguments. The standard library function `strlen` doesn't need them, and neither should you. Think about how. – WhozCraig Feb 28 '22 at 01:42
  • As already established - you don't need `a` and `b` parameters. But a word of advice: If you do need them one day, don't call them `a` and `b`. `num_chars` and `max_len` may be better names in this case. – John3136 Feb 28 '22 at 01:44
  • int function(const char *ptr){ return std::string{ptr}.size(); } – QuentinUK Feb 28 '22 at 02:24
  • There's a huge bug in that code related to `ptr`. – Mark Ransom Feb 28 '22 at 02:27
  • @QuentinUK `int function(const char *ptr) { return std::strlen(ptr); }` would avoid a memory allocation. But either solution is cheating for this assignment. – Remy Lebeau Feb 28 '22 at 03:09

1 Answers1

1

First of all welcome to stackoverflow ye0123! I think you are trying to rewrite the strlen() function here. Try giving the following link a look Find the size of a string pointed by a pointer. The short answer is that you can use the strlen() function to find the length of your string. The code for your function will look something like this:

int function(const char *ptr) 
{
    size_t length = strlen(ptr);
    return length;
}

You should also only need this function and main.

Edit: Maybe I misunderstood your question and you are supposed to reinvent strlen() after all. In that case, you can do it like so:

unsigned int my_strlen(const char *p)
{
    unsigned int count = 0;

    while(*p != '\0') 
    {
        count++;
        p++;
    }
    return count;
}

Here I am comparing *p from '\0' as '\0' is the null termination character.

This was taken from https://overiq.com/c-programming-101/the-strlen-function-in-c/

gabri
  • 62
  • 8
  • 1
    BTW, besides `strlen()`, there is also [`std::char_traits::length()`](https://en.cppreference.com/w/cpp/string/char_traits/length), which works over `wchar_t`, `char16_t`, and `char32_t` as well as `char` (and, presumably, `char8_t` as of C++20). – Dúthomhas Feb 28 '22 at 02:35
  • `char *p` should be `const char *p`. And it is possible to implement this without the `count` variable, only pointer arithmetic. – Remy Lebeau Feb 28 '22 at 03:12
  • Thanks @RemyLebeau I edited const char *p – gabri Feb 28 '22 at 03:20