Need Help wit Programming Assignment strlen()

Discussion in 'C++' started by chessc4c6, Mar 6, 2005.

  1. chessc4c6

    chessc4c6 Guest

    I'm taking a C++ course right now and my homeowrk was to create a
    strlen function(even though it already existed) and have main call it
    and read a string to give its length.
    The first Version was supposed to have the prototype:
    long strlen(char s1[])
    and a for()loop and a array representing the string will be used. So I
    came up with:

    long strlen(char s1[]){
    long counter;
    for(counter=0;s1[counter] != '\0'; counter++);
    return counter;
    }
    void main(){
    long strlen(char s1[]);
    cout<<"The length of the string COMPUTER SCIENCE is : "<<
    strlen("COMPUTER SCIENCE")<<endl;
    }

    So the next step is making the prototype:
    long strlen(char *s1);
    using a while()loop and a pointer representing the string should be
    used. Now I was given the hint of changing the fucntion by doing
    something like "s1+1"...but am not really sure at all where to begin.
    Help would be appreciated.
     
    chessc4c6, Mar 6, 2005
    #1
    1. Advertising

  2. chessc4c6

    Guest

    chessc4c6 wrote:
    > I'm taking a C++ course right now and my homeowrk was to create a
    > strlen function(even though it already existed) and have main call it
    > and read a string to give its length.
    > The first Version was supposed to have the prototype:
    > long strlen(char s1[])
    > and a for()loop and a array representing the string will be used. So

    I
    > came up with:


    [snip]
    > So the next step is making the prototype:
    > long strlen(char *s1);
    > using a while()loop and a pointer representing the string should be
    > used. Now I was given the hint of changing the fucntion by doing
    > something like "s1+1"...but am not really sure at all where to begin.
    > Help would be appreciated.


    a := *(a + i)

    HTH, /david
     
    , Mar 6, 2005
    #2
    1. Advertising

  3. chessc4c6

    chessc4c6 Guest

    Ok, I'm still not understanding....but from what I can see, my function
    should be look something like this:
    long strlen(char *s1){
    long counter;
    while( ) // And I'm lost; How do I go about it.
     
    chessc4c6, Mar 6, 2005
    #3
  4. chessc4c6

    Phlip Guest

    chessc4c6 wrote:

    > So the next step is making the prototype:
    > long strlen(char *s1);
    > using a while()loop and a pointer representing the string should be
    > used. Now I was given the hint of changing the fucntion by doing
    > something like "s1+1"...but am not really sure at all where to begin.


    ++s1;

    Now curl up with your tutorial. Start a chapter before the part about
    "pointer arithmetic", and keep going until a chapter after. You cannot get
    over this intro stuff just by compiling and asking USENET. Your tutorial, by
    contrast, will lead through all these topics, and learning the tangential
    ones will help you get the "feel" for C++'s various aspects.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Mar 6, 2005
    #4
  5. chessc4c6 wrote:
    > I'm taking a C++ course right now and my homeowrk was to create a
    > strlen function(even though it already existed) and have main call it
    > and read a string to give its length.
    > The first Version was supposed to have the prototype:
    > long strlen(char s1[])


    Tell your teacher that he/she really really needs to consider writing it
    like:

    long strlen(const char s1[])

    > and a for()loop and a array representing the string will be used. So I
    > came up with:
    >
    > long strlen(char s1[]){
    > long counter;
    > for(counter=0;s1[counter] != '\0'; counter++);
    > return counter;
    > }
    > void main(){
    > long strlen(char s1[]);
    > cout<<"The length of the string COMPUTER SCIENCE is : "<<
    > strlen("COMPUTER SCIENCE")<<endl;
    > }
    >
    > So the next step is making the prototype:
    > long strlen(char *s1);
    > using a while()loop and a pointer representing the string should be
    > used. Now I was given the hint of changing the fucntion by doing
    > something like "s1+1"...but am not really sure at all where to begin.
    > Help would be appreciated.
    >


    Here is a start.

    long strlen(const char *s1)
    {
    const char *tmp = s1;

    while ( ??? )
    {
    ++ tmp;
    }

    return ????; // use pointer arithmetic
    }

    You're moving the pointer (tmp) until you find a nul. Given that the
    unary operator "*" dereferences the value, con you figure that out ?

    Think about pointer arithmetic for the return expression. If p1 and p2
    are pointers to the same array, (p1 - p2) evaluates to ?
     
    Gianni Mariani, Mar 6, 2005
    #5
  6. Consider for a minute that a for loop contains a while loop.
    // for
    x = 0;
    while( x < 10)
    { // do1
    cout<<x;
    // do2
    x++;
    }

    // and the equivelent for loop

    for( x=0; /*While*/ x < 10; /*do2*/ x++)
    { // do1
    cout <<x;
    }

    on another note:

    Function headers are only needed to describe the interface when the
    definition is not visable. If the function is defined before it is called,
    then you dont need a function header if it occurs after the call, then you
    need a header, and you dont put in main().

    and to finalize :

    char i[10];
    char * j = i;

    char i[0] = j[0]; // *j
    char i[1] = ++j[0];

    the pointer i plus offset of [] number of char's,
    vs the pointer j ;

    i is a const pointer and the offset must always be added at runtime; j is a
    variable, That is calculated at runtime.



    "chessc4c6" <> wrote in message
    news:...
    > I'm taking a C++ course right now and my homeowrk was to create a
    > strlen function(even though it already existed) and have main call it
    > and read a string to give its length.
    > The first Version was supposed to have the prototype:
    > long strlen(char s1[])
    > and a for()loop and a array representing the string will be used. So I
    > came up with:
    >
    > long strlen(char s1[]){
    > long counter;
    > for(counter=0;s1[counter] != '\0'; counter++);
    > return counter;
    > }
    > void main(){
    > long strlen(char s1[]);
    > cout<<"The length of the string COMPUTER SCIENCE is : "<<
    > strlen("COMPUTER SCIENCE")<<endl;
    > }
    >
    > So the next step is making the prototype:
    > long strlen(char *s1);
    > using a while()loop and a pointer representing the string should be
    > used. Now I was given the hint of changing the fucntion by doing
    > something like "s1+1"...but am not really sure at all where to begin.
    > Help would be appreciated.
    >
     
    DHOLLINGSWORTH2, Mar 6, 2005
    #6
  7. chessc4c6

    chessc4c6 Guest

    ok, since I've posted this question and doing some research, I've come
    up with this code:
    #include <iostream>
    using namespace std;
    long strlen(char *s1){
    long count;
    s1=new char[10];
    count=0;
    while (s1 != '/0'){
    count+=1;
    s1++;
    }
    return count;
    }
    void main(){
    long strlen(char *s1);
    cout <<"The length of the string JAIME is" << strlen("JAIME") <<endl;
    }

    The whole purpose is to use long strlen(char *s1) using a while loop
    and a pointer
    representing the string will be used.Now I get errors doing this and
    I'm not sure if its correct.
    I used s1=new char[10] to dynamically allocate memory. Can someone
    help me out or tell me if
    there is another easier way to do it.
     
    chessc4c6, Mar 7, 2005
    #7
  8. chessc4c6 wrote:
    >
    > ok, since I've posted this question and doing some research, I've come
    > up with this code:
    > #include <iostream>
    > using namespace std;
    > long strlen(char *s1){
    > long count;
    > s1=new char[10];


    What is the memory allocation doing in here?
    You already have a pointer to the beginning
    of a valid string: it was passed as argument
    to the function.

    > count=0;
    > while (s1 != '/0'){


    You don't want to check if the pointer equals '\0'
    (whatever that may mean). You want to check if the thing
    the pointer *points to* equals '\0'

    while( *s1 != '\0' ) {

    Note: the '*' which makes all the difference.

    > count+=1;


    or more idiomatic
    cout++;

    > s1++;
    > }
    > return count;
    > }



    > void main(){

    int main() {

    > long strlen(char *s1);
    > cout <<"The length of the string JAIME is" << strlen("JAIME") <<endl;
    > }
    >
    > The whole purpose is to use long strlen(char *s1) using a while loop
    > and a pointer
    > representing the string will be used.Now I get errors doing this and
    > I'm not sure if its correct.
    > I used s1=new char[10] to dynamically allocate memory.


    What for?
    I mean, what do you need this dynamic allocated memory for?

    In:

    I pass the beginning of a character sequence to a function (as a pointer)
    I want the function to count how often the passed pointer can be incremented
    before it points to a '\0' character and return that number

    I really see no need in allocating some memory dynamically in this.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Mar 7, 2005
    #8
  9. chessc4c6

    Taras Guest

    On Mon, 07 Mar 2005 13:52:10 +0100, Karl Heinz Buchegger
    <> wrote:

    > chessc4c6 wrote:
    >>
    >> ok, since I've posted this question and doing some research, I've come
    >> up with this code:
    >> #include <iostream>
    >> using namespace std;
    >> long strlen(char *s1){
    >> long count;
    >> s1=new char[10];

    >
    > What is the memory allocation doing in here?
    > You already have a pointer to the beginning
    > of a valid string: it was passed as argument
    > to the function.
    >
    >> count=0;
    >> while (s1 != '/0'){

    >
    > You don't want to check if the pointer equals '\0'
    > (whatever that may mean). You want to check if the thing
    > the pointer *points to* equals '\0'
    >
    > while( *s1 != '\0' ) {
    >
    > Note: the '*' which makes all the difference.
    >
    >> count+=1;

    >
    > or more idiomatic
    > cout++;
    >
    >> s1++;
    >> }
    >> return count;
    >> }

    >
    >
    >> void main(){

    > int main() {
    >
    >> long strlen(char *s1);
    >> cout <<"The length of the string JAIME is" << strlen("JAIME") <<endl;
    >> }
    >>
    >> The whole purpose is to use long strlen(char *s1) using a while loop
    >> and a pointer
    >> representing the string will be used.Now I get errors doing this and
    >> I'm not sure if its correct.
    >> I used s1=new char[10] to dynamically allocate memory.

    >
    > What for?
    > I mean, what do you need this dynamic allocated memory for?
    >
    > In:
    >
    > I pass the beginning of a character sequence to a function (as a
    > pointer)
    > I want the function to count how often the passed pointer can be
    > incremented
    > before it points to a '\0' character and return that number
    >
    > I really see no need in allocating some memory dynamically in this.
    >



    I wish to add, that it is not just useless, but with that code you
    wrote, no one can tell where your loop will end, even if you'll write
    "while( *s1 != '\0' )" with that *. Because dynamically allocated
    memory will have any values and you may get '\0' on the very first
    character, or do not get it at all (your program will crash).
     
    Taras, Mar 7, 2005
    #9
  10. chessc4c6

    Old Wolf Guest

    chessc4c6 wrote:

    > I'm taking a C++ course right now and my homeowrk was to create a
    > strlen function(even though it already existed) and have main call it
    > and read a string to give its length.
    > The first Version was supposed to have the prototype:
    > long strlen(char s1[])
    > and a for()loop and a array representing the string will be used. So

    I
    > came up with:
    >
    > long strlen(char s1[]){


    Firstly, strlen() is a poor name for a function, because if you
    ever go to include <string.h> or <cstring> to try and use some
    other string functions, then it will conflict with the strlen()
    that's already there.

    And there are even some compilers that will confuse your one with
    the standard one anyway.

    > long counter;
    > for(counter=0;s1[counter] != '\0'; counter++);
    > return counter;
    > }
    > void main(){


    A common error (your instructor isn't up with it, obviously).
    main() must have 'int' for its return type, even though some
    compilers accept 'void'.

    > long strlen(char s1[]);
    > cout<<"The length of the string COMPUTER SCIENCE is : "<<
    > strlen("COMPUTER SCIENCE")<<endl;
    > }
    >
    > So the next step is making the prototype:
    > long strlen(char *s1);


    In a function prototype, "char *s1" is identical to "char s1[]".
    Try it with your original example, you will see that there is no
    difference. When you try to pass an array to a function, what
    actually happens is that you have passed a pointer to the array's
    first element to the function.

    > using a while()loop


    Well, a while loop is a subset of a for loop, isn't it:
    for (a; b; c) { ... }
    is just the same as:
    a;
    while (b) {
    ...
    c
    }
    (if you don't do a 'continue' in the ... section).

    > and a pointer representing the string should be used.


    You already have a pointer to the string: s1.

    > Now I was given the hint of changing the fucntion by doing
    > something like "s1+1"...but am not really sure at all where to begin.


    Well if you are pointing to one character and you want to go
    onto the next character, then obviously incrementing the pointer
    is how to do it.

    > Help would be appreciated.


    Here's your original code, with the 'modification' I already
    mentioned (which has no effect):

    long strlen(char *s1)
    {
    long counter;
    for(counter=0;s1[counter] != '\0'; counter++);
    return counter;
    }

    I think what your instructor wants is for you to make
    another pointer, let's call it s2, which you will point
    at the end of the string. Then if you do ( s2 - s1 ),
    you will have the length of the string, which you can
    return.

    So we now have:
    long strlen(char *s1)
    {
    char *s2;
    ...........?
    return s2 - s1;
    }

    I'll leave you to fill in the blank. You have to:
    - start s2 off pointing to s1
    - move it until it points to the end of s1's string
    (this is where the while loop will come in: while
    it is not at the end of the string, increment it)
     
    Old Wolf, Mar 8, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kris Rockwell

    Problems wit Databinding to a DropDown box

    Kris Rockwell, Apr 4, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    419
    Kris Rockwell
    Apr 6, 2004
  2. =?Utf-8?B?SGVuZHJp?=

    How to combine Asp.net wit hms.exel ?

    =?Utf-8?B?SGVuZHJp?=, Aug 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    378
    =?Utf-8?B?SGVuZHJp?=
    Aug 18, 2004
  3. J de Boyne Pollard

    need help wit sbrk

    J de Boyne Pollard, Sep 12, 2007, in forum: C++
    Replies:
    0
    Views:
    559
    J de Boyne Pollard
    Sep 12, 2007
  4. Replies:
    8
    Views:
    332
    JosephKK
    May 6, 2009
  5. sterta bole
    Replies:
    0
    Views:
    810
    sterta bole
    Aug 27, 2012
Loading...

Share This Page