-3

I have just started to learn c++. I also want to clear that this is no homework question, its just something that I am stuck on.

I was going through assignment questions on the MIT website, and i have pasted the question for you here;

Write a function that returns the length of a string (char *), excluding the final NULL character. It should not use any standard-library functions. You may use arithmetic and dereference operators,but nottheindexing operator([]).

I don't know how to do this without an array.

Any help is appreciated!!

This is what i did:

#include<iostream>
#include<conio.h>
#include<string>


using namespace std;

int stringlength (char* numptr);

int main()
{
    char *mystring;


    cout<<"enter the string \n";
    cin>>mystring;

    cout<<"length is "<<stringlength(mystring);

    getch();
}

int stringlength (char* numptr)
{

    int count=0;

    for(;*numptr<'\0';*numptr++)
    {
                   count++;
    }
    return(count);
 }



This is what i had done previously before I asked u all about the problem.
But this got me an answer of zero.

But if in my function i change *numptr<'\0' to *numptr!= 0, i get the right answer.

Now what i am confused about is, isn't that the null character, so why cant i check for      that.
aghoribaba
  • 131
  • 1
  • 1
  • 11
  • 1
    The clue is in the question. Dereference the pointer to see what it's pointing at, and use arithmetic to move it to the next character. Count how many you find, and stop when you find a null character; or use more arithmetic to save having to count. – Mike Seymour May 02 '13 at 16:38
  • 1
    You may like to know that the subscripting operator is defined as `a[i] == *(a+i)`. So if you can do it with subscripting, it's very easy to do it with pointer addition and dereference instead. – Ben Voigt May 02 '13 at 16:40
  • Also possible dup: http://stackoverflow.com/questions/8831323/find-length-of-string-in-c-without-using-strlen – Shafik Yaghmour May 02 '13 at 16:41
  • I hope " the MIT website" isn't that terrible OpenCourseware C++ "course" which lists web tutorials as the only reference... – Cubbi May 02 '13 at 16:47
  • yeah, i was trying the opencourseware thing, is it that bad? – aghoribaba May 02 '13 at 16:49
  • Do u know a better place to learn c++ – aghoribaba May 02 '13 at 16:50
  • @DhruvKhatkar a better place is http://stackoverflow.com/questions/388242 – Cubbi May 06 '13 at 22:15

3 Answers3

4

Since you are doing this as an educational thing, I'm not gonna give you the answer. But I will help you a little on the way.

Use a char* and the ++ operator to check for terminating zero \0 this will be the last character in the string.

olevegard
  • 5,294
  • 1
  • 25
  • 29
3

First of all, this is not the way to learn C++ in 2013. The answer relies on low-level pointer manipulation. There are a lot more important things to learn about C++ before you get to this point. Right now, you should be learning about string, vector, functions, classes, and not about these low-level details.

To answer you question, you have to know how strings are represented. They are represented as an array of characters. In C and C++, arrays do not have a built in length. So you have to store it or use some other means of finding the length. The way the strings make is so you can find the length is that they store a 0, as the last position in the array. Thus "Hello" would be stored as

{'H','e','l','l','o',0}

To find the length you go through the array starting at index 0 and stop when you encounter a character value of 0;

The code would look something like this

int length(const char* str){
    int i = 0;
    for(; str[i] != 0; i++);
    return i;
} 

Now in C and C++ you can str[i] is the same as *(str + i); So to satisfy your question you can write it like this

int length(const char* str){
    int i = 0;
    for(; *(str + i) != 0; i++);
    return i;
} 

Now, instead of using + i, you can increment str directly;

int length(const char* str){
    int i = 0;
    for(; *str++ != 0; i++){;
    return i;
} 

Now in C, a value is false if it is 0, otherwise it is true, so we do not need the != 0, so we can write

int length(const char* str){
    int i = 0;
    for(; *str++; i++){;
    return i;
} 
John Bandela
  • 2,416
  • 12
  • 19
0
#include<iostream>
#include<conio.h>
#include<string>


using namespace std;

int stringlength (char* numptr);

int main()
{
    char *mystring;


    cout<<"enter the string \n";
    cin>>mystring;

    cout<<"length is "<<stringlength(mystring);

    getch();
}

int stringlength (char* numptr)
{

    int count=0;

    for(;*numptr<0;*numptr++)
    {
               count++;
    }
    return(count);
 }
aghoribaba
  • 131
  • 1
  • 1
  • 11