Thread: couting the number of characters

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    16

    edited

    edited
    Last edited by hinman; 04-16-2007 at 09:06 PM. Reason: edited

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int size( char*s)
    {
        return theactualsizeofthestring;
    }
    Just to let you know, I'm pretty sure you're going to fail your homework assignment if you use strlen to do the work for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by hinman View Post
    edited
    Don't do that.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Quote Originally Posted by Dave_Sinkula View Post
    Don't do that.
    check the discouraging quzah's answer

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by hinman View Post
    check the discouraging quzah's answer
    The purpose in making you write a function to do something already written in the C library, at least for something like strlen(), is probably more for you to learn about the language and understand the reasoning behind what it does. It's probably not meant for you to figure out how to wrap the built-in function with a custom one, which by the way, you don't even know how to do if what you posted was any indication of your knowledge.

    Also, if you get discouraged that easily, you are probably not cut out for programming. What you wrote shows that you have no understanding of what you are doing and probably have done no other real assignments to date, unless they were somehow easier than this one.

    If you're going to stick with it, don't get hurt so easily. Take the initiative and actually learn the language. Programming involves being able to think and learn on your own. Do you still need help from others? Absolutely. Is it right to post questions on the forums here? Absolutely. The key is that you have to put forth effort before anyone is willing to help you, because if you won't help yourself, why should anyone else help you?

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    tx macgyver for the answer, well 1st, i did not like quzah's answer because he's talking about failing a homework since the 1st post, what the heck ? it s not a homework, it is something that I want to learn, by my self. I am doing research online since i am having troubles with prototypes and function calling, may be I did not ask the qustion right, but it's not a reason to make an answer discouraging...
    if he says that he is "pretty sure" that strlen is not the one I need, why there is no explanations about that?...
    If sizeof() can't work and strelen can not either, whats the solution ?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    strlen() is the proper way to get the length of a string, however, for learning purposes, if you wish to write your own, it's quite possible.

    The idea is that strings are simply char arrays that contain a '\0' char at the end, which has a numerical number of 0. So if I give you a C string "Hello!", obviously you know the length of it is 6 intuitively. In memory, it'll look like this:

    Code:
    'H', 'e', 'l', 'l', 'o', '!', '\0'
    Try to understand how you know the length of a string. What do you naturally do? Count the characters. When do you stop? When you reach the end. How do you know you've reached the end? You should know that now since the answer is in this post.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by hinman View Post
    check the discouraging quzah's answer
    Harsh criticism will get you further along than babying. If you don't want to be embarrassed ever so slightly anonymously, don't ever apply for a job.

    If however you want to learn and can risk asking a question after trying to find an answer yourself and being able to demonstrate your efforts, you will most often be well rewarded.

    I still have questions that folks here could kick me around with quite easily; why should you be any different? Do you want to learn, or do you want to feel good? No pain, no gain.

    --

    The real issue for forums such as these is that when you after-the-fact modify real questions, those that will follow are deprived of an answer. Edit typos, edit additions before subsequent replies; otherwise, however embarrassing at the moment, leave post in tact. Others will follow, search for terms, and learn from similar mistakes. Depriving folks of that leaves folks like you in a worse predicament instead of a better one.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by hinman View Post
    check the discouraging quzah's answer
    Awwww didn't mummy tell you she loved you enough when you was a widduw iddy baby? Ya ........ing whiner!


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't ask a question, resize the answer then delete your question cause you feel like a moron. leave it here so other people can learn from it!

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by zacs7 View Post
    Don't ask a question, resize the answer then delete your question cause you feel like a moron. leave it here so other people can learn from it!
    We got it like 10 posts ago, thanks.

    Too bad we can't pick what gets bumped on the page.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by citizen View Post
    We got it like 10 posts ago, thanks.

    Too bad we can't pick what gets bumped on the page.
    Don't you know you're not supposed to use gets? Sheesh man, they even wrote a FAQ on it!


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I dunno, incorporating fgets into my speech has been rather difficult. It adds a whole nother two variables, you know, and I don't really spend that much time talking.

    What really gets me going though is when people resize their answers with realloc and neglect to use temporary answers.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    or like hinman did and free()'d his post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read the number of characters
    By rehan in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2007, 10:57 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  5. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM