Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Need Help wit Programming Assignment strlen() (http://www.velocityreviews.com/forums/t289311-need-help-wit-programming-assignment-strlen.html)

chessc4c6 03-06-2005 06:20 AM

Need Help wit Programming Assignment strlen()
 
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.


davidrubin@warpmail.net 03-06-2005 06:29 AM

Re: Need Help wit Programming Assignment strlen()
 

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[i] := *(a + i)

HTH, /david


chessc4c6 03-06-2005 06:35 AM

Re: Need Help wit Programming Assignment strlen()
 
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.


Phlip 03-06-2005 06:36 AM

Re: Need Help wit Programming Assignment strlen()
 
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/bi...UserInterfaces





Gianni Mariani 03-06-2005 06:39 AM

Re: Need Help wit Programming Assignment strlen()
 
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 ?

DHOLLINGSWORTH2 03-06-2005 07:54 PM

Re: Need Help wit Programming Assignment strlen()
 
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" <prodigyofqnz1@aol.com> wrote in message
news:1110090035.105681.295320@f14g2000cwb.googlegr oups.com...
> 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 03-07-2005 12:35 AM

Re: Need Help wit Programming Assignment strlen()
 
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.


Karl Heinz Buchegger 03-07-2005 12:52 PM

Re: Need Help wit Programming Assignment strlen()
 
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
kbuchegg@gascad.at

Taras 03-07-2005 04:45 PM

Re: Need Help wit Programming Assignment strlen()
 
On Mon, 07 Mar 2005 13:52:10 +0100, Karl Heinz Buchegger
<kbuchegg@gascad.at> 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).

Old Wolf 03-08-2005 04:17 AM

Re: Need Help wit Programming Assignment strlen()
 
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)



All times are GMT. The time now is 05:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.