Thread: Calculating the length of a string

  1. #16
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    My book has an array getting passed to a function in a function call of calling the

    function

    functionname ( Arrayname, size ) . I understand now its a string and of course a sting end in null character. That's all you had to say girl. That's what I get for turning the section on passing array to function and its not talking about strings.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    You are not making any changes so the condition can come true, this is definitely an infinite loop.
    What I suggest you to do is the following:
    As you know, any string has a null-terminator (\0) which represents the end of the string.
    So you could pass a string to the function, and every loop, check if str[counter] equals to the \0, if so, exit the loop.
    While the loop is still on track, increment counter by one because this is not the end of the string and str[counter] is a character.
    Code:
    int strlen(char *str)
    {
     int counter = 0;
     while(str[counter] != '\0')
              counter++;
     return counter;
    }
    
    int main(void)
    {
        int length = strlen("Hello World!");
        printf("%d", length);
        return 0;
    }
    Last edited by eXeCuTeR; 07-01-2008 at 03:23 PM.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by BSmith4740 View Post
    Sorry if I offended anyone. I am really trying to learn this and someone states I have an infinite loop. I state what "I think it is doing" and ask a question and Elysia gets ........ed off. What the heck?
    You seem to continuously ignore the errors in your code.
    First you pass an integer where a string is expected.
    Then you destroy your function prototype.
    And then you seem to lack the understanding of pointers.

    You have not made one single fix. All you've done is made things worse.
    Why? Because you do no understand what you do.
    Therefore, the best course of action, is to read over your books or resources again, because you lack fundamental knowledge of the things you are using.

    Most of the people here help out others, but we do not teach. We do not tell you the basics of C. That's what books and tutorials are for.

    Several people continuously told you that your function call was WRONG, and yet you seemed to either ignore or fail to understand. I even told you that the type you passed is NOT what the function expects. How much clearer than that can it get?

    Do us a favor. Consult your books. Learn how to program C. Then come back to us and you'll get a warmer reception.
    C is not for the faint of heart.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    So what's the best way to learn C? You can only read the book so many times. You can't talk to the book. You can't ask questions to the book. However, I do appreciate the help.

    If my function prototype is

    int stringlength ( char *s);

    I am returning an intenger and taking in an array or pointer.

    If my function call is

    length = stringlength ( count );

    count is what I am returning from the function correct.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by BSmith4740 View Post
    So what's the best way to learn C? You can only read the book so many times. You can't talk to the book. You can't ask questions to the book. However, I do appreciate the help.
    Then it's a bad book, because a book is supposed to teach.

    If my function call is
    length = stringlength ( count );
    count is what I am returning from the function correct.
    No.
    return = function(arguments);
    You are passing count as an argument, something IN to the function and what is coming OUT from the function is assigned to length.

    And as for the infinite loop... You keep checking the very same position all the time.
    You never increment or tell it go to the next position and check. Hence, infinite.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Arguments is not something I get with this concept.

    I understand statements like
    Code:
    if ( x == 2 && y == 3 ) {
    statements
    }
    Those are arguments that I understand x == 2 && y == 3.

    I don't get the function call term argument.

    If I want to return something from the function to the function call. I am not arguing with it or checking if a condition is true.

    if I hit the return statement I want it to return whatever x is.


    length = stringlength ();

    makes a lot more sense to me than arguments.

  7. #22
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    An amateur like myself would also think that

    length = stringlength (count);

    should also work if the above worked.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by BSmith4740 View Post
    Those are arguments that I understand x == 2 && y == 3.
    They are not arguments.

    if I hit the return statement I want it to return whatever x is.
    length = stringlength ();
    makes a lot more sense to me than arguments.
    Again, this is because you lack basic C understand, and, heck, basic programming understanding.

    Quote Originally Posted by BSmith4740 View Post
    An amateur like myself would also think that
    length = stringlength (count);
    should also work if the above worked.
    No! How many times do we have to tell you?
    Count is an integer and the function expects a string (const char*)!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Obviously it does not

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Obviously it DOES.
    Because you are using C, the compiler only warns you.
    Try using C++ and you the compiler will spit out an error in your face because you cannot understand a basic concept.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    but if you look in the function prototype I am returning an intenger. Whatever the function takes in the function prototype it has to be there in the function call. If that is true. I don't see how the count would ever get passed by value to length.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you not listening? Everyone has told you that it's WRONG.
    Look:
    int stringlength(const char* s);
    What do I see? I see const char*, NOT int!
    You are passing an INT, but it expects CONST CHAR*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    If the above is true. One could think of the function call has x

    length = x (what i take in);

    x() is just substituting whatever is in the blocks?

    Code:
    return value x (  what I take in )
    {
    
    
    
    }

  14. #29
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int how_long;
    char* what_string;
    how_long = stringlength(what_string)
    I don't think this can get any more basic. You tell the function which string's length to find and it returns the length that you can assign to a numeric variable.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #30
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Okay...

    Functions need arguments.

    Arguments are values.

    Values have types.

    You specify those types when you define the function.

    You pass a value of that type to the function when you call the function.

    If you pass a value of a different type to the function, the code in the function might not work right.

    Understand?
    Last edited by robwhit; 07-01-2008 at 04:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM