edited
edited
Last edited by hinman; 04-16-2007 at 09:06 PM. Reason: edited
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.Code:int size( char*s) { return theactualsizeofthestring; }
Quzah.
Hope is the first step on the road to disappointment.
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.*
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?
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 ?
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:
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.Code:'H', 'e', 'l', 'l', 'o', '!', '\0'
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.*
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!
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.
or like hinman did and free()'d his post.