GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Sep-2009, 12:37
bubaduff bubaduff is offline
New Member
 
Join Date: Sep 2009
Posts: 3
bubaduff is on a distinguished road

Write two strlen (string length) functions


I am writing a program where i write two strlen(string length) functions. The first version of the program should use array subscripting, and the second version should use pointers and pointer arithmetic. This is what i have so far;

CPP / C++ / C Code:
#include <cstring>
using std::strlen;

int main()
{
    char *string1 = " ";
    char *string2 = " ";
    char *string3 = " ";

    cout << "The length of \"" <<string1 <<"\" is " <<strlen(string 1)
            << "The length of \"" <<string2 <<"\" is " <<strlen(string 2)
            << "The length of \"" <<string3 <<"\" is " <<strlen(string 3)
            <<endl;
     return 0;
}

Need help!!!

thanks.
Last edited by admin : 09-Sep-2009 at 14:48. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 08-Sep-2009, 13:21
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help with this program


Here is a hint:

The end of a string is the '\0' character.

So, you could look at the first character and, if not equal to '\0', increment the count and go repeat on the next character.
  #3  
Old 08-Sep-2009, 18:31
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Need help with this program


Quote:
Originally Posted by bubaduff
I am writing a program where i write two strlen(string length) functions. The first version of the program should use array subscripting, and the second version should use pointers and pointer arithmetic. This is what i have so far;

#include <cstring>
using std::strlen;

int main()
{
char *string1 = " ";
char *string2 = " ";
char *string3 = " ";

cout << "The length of \"" <<string1 <<"\" is " <<strlen(string 1)
<< "The length of \"" <<string2 <<"\" is " <<strlen(string 2)
<< "The length of \"" <<string3 <<"\" is " <<strlen(string 3)
<<endl;
return 0;
}

Need help!!!

thanks.


I don't know what the "code" you've submitted is supposed to be, but it isn't anything useful. Perhaps...?:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

int strlen_pointer(const char* str)
{
    int len = 0;
    const char* p = str;
    while(*p)
    {
        ++len;
        ++p;
    }
    return len;
}

int strlen_array(const char* str)
{
    int len;
    for(len = 0; str[len] != 0; len++)
    {
        ;
    }
    return len;
}

int main()
{
    char* p = "Hello, World!";
    cout << strlen_pointer(p) << endl;
    cout << strlen_array(p) << endl;
    return 0;
}


...would it KILL YOU to read the guidelines?!


MxB
  #4  
Old 09-Sep-2009, 07:15
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help with this program


Mexican Bob - Where were you at when I was in school?
  #5  
Old 09-Sep-2009, 09:18
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Need help with this program


Quote:
Originally Posted by fakepoo
Mexican Bob - Where were you at when I was in school?

It depends...when were you in school?


MxB
  #6  
Old 23-Sep-2009, 04:02
april198474 april198474 is offline
New Member
 
Join Date: Sep 2009
Posts: 2
april198474 is an unknown quantity at this point

Re: Write two strlen (string length) functions


I use C#:

C-SHARP / C# Code:
public long Length(string strLen)
{
bool winnt_chinese=true;
winnt_chinese=("中国".Length ==2);
if(winnt_chinese)
{
long l,t,c;
int i;
l=strLen.Length ;
t=l;
for( i=0;i<l;i )
{
c=(int)strLen[i];
if( c<0)
{
c=c 65536;
}
if (c>255)
{
t=t 1;
}
}
return t;
}
else 
{
return strLen.Length ;
}
}
  #7  
Old 23-Sep-2009, 04:59
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Write two strlen (string length) functions


Quote:
Originally Posted by april198474
I use C#:

That is incredibly unfortunate for you, particularly since this is a C++ forum, but apparently you are incapable of reading the requirements OR the guidelines.

The requirements state TWO functions. One that uses an array and the other that uses pointers. Are you still working on the pointer version?

bool winnt_chinese=true;
winnt_chinese=("中国".Length ==2);
if(winnt_chinese)

...so when is winnt_chinese not true?

Your coding style needs attention. It is inconsistent and sloppy. Was it your intention to write a recursive function?

Is the intention to support double byte character sets?

Quote:
Originally Posted by april198474
t=t 1;

...is that even a valid statement? If that is valid code in C#, you can HAVE it...but PLEASE take it far, far away! How about back to "中国?"


MxB
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 09:24
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 03:48
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 06:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:03.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.