This forum is in archive mode. You will not be able to post new content.

Author Topic: Function to count the length of the string  (Read 3197 times)

0 Members and 3 Guests are viewing this topic.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Function to count the length of the string
« on: September 19, 2013, 12:01:31 PM »

Was sitting idle so wrote this. I used pointers instead of integers.;)

Code: (C++) [Select]
#include<iostream>

const int size = 5000;


int * strlen(char *, int );

int main(void){

char string[size];

cout << "Enter a string to find its length : ";

cin.getline(string, size);

int * len = strlen(string, size);

cout << "The string consists of " << (*len) << " characters." << endl;

delete len;

cin.get();

cin.get();

return 0;

}

int * strlen(char * array, int size){

int * len = new int;

*len = 0;

for (int i = 0; i < size; ++i){

if (array[i] == '\0')

break;

++(*len);

}

return len;

}

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Function to count the length of the string
« Reply #1 on: September 19, 2013, 12:40:10 PM »
A common(ish) way is to do it like this:
Code: (C) [Select]
int strlen(char *stuff) {
    int len = 0;
    while (stuff) {
        *stuff++;
        len++;
    }
    return len;
}
Obviously it will only work with strings shorter than 2^31 due to the limitations of int.

Also, why are you freeing len just before the program exits? At least on Windows, a process' memory is freed when it terminates.
« Last Edit: September 19, 2013, 12:40:49 PM by Fur »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Function to count the length of the string
« Reply #2 on: September 19, 2013, 04:26:28 PM »
Also, why are you freeing len just before the program exits? At least on Windows, a process' memory is freed when it terminates.

Because there is no guarantee it will be, plus it's also just good form.  Don't code for Windows specific, code to POSIX.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Function to count the length of the string
« Reply #3 on: September 19, 2013, 05:37:46 PM »
Because there is no guarantee it will be, plus it's also just good form.  Don't code for Windows specific, code to POSIX.


That's why I freed the memory allocated.;)


You should always have a backup plan.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Function to count the length of the string
« Reply #4 on: September 20, 2013, 09:02:43 AM »
Your code could use some indentation.  ;)

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Function to count the length of the string
« Reply #5 on: September 20, 2013, 09:08:21 AM »
Because there is no guarantee it will be, plus it's also just good form.  Don't code for Windows specific, code to POSIX.

But Windows isn't necessarily POSIX-compliant, so...?

@parad0x; Why is 'len' a pointer?  On 32-bit systems, 'int' is 4 bytes & a pointer is also 4 bytes.  On 64-bit systems, 'int' is 4 bytes whilst a pointer is 8 bytes.  In this sense, making 'len' a pointer is a waste of space.  Also, you can define a starting value when using 'new'.

Code: (c++) [Select]
int *foo = new int(42);

Your loop can do your 'len' increment as well, though it really isn't necessary.

Code: (c++) [Select]
for (int i = 0; i < size; ++i, ++(*len))

As Fur attempted to point out, you can simply assign len the value of 'i' after you break out of the loop.  Granted, to do this, you'll need to declare 'i' before the loop.  Another issue is that 'break' will break the loop and your "++(*len)" isn't going to run, thus making the return a 0-based count.  The problem with this is that 'strlen()' returns the number of characters in the string, not the last index.  Finally, using 'int' as the return type is going to limit your number to half of the range that could be used.  Because it is impossible to have a negative number of characters, 'unsigned int' would be a more appropriate return type.  Of course, that still deviates from the standard 'strlen()', but it's a desirable modification for most.

For an additional challenge, I'd suggest trying to find a way to do it recursively.

@Fur; "while (stuff)" may be undesirable as it could cause an infinite loop, or worse.  Perhaps you mean the following:


Code: (c) [Select]
while (*stuff++) ++len;

« Last Edit: September 20, 2013, 09:17:08 AM by Xires »
-Xires

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Function to count the length of the string
« Reply #6 on: September 20, 2013, 10:26:54 AM »
@Fur; "while (stuff)" may be undesirable as it could cause an infinite loop, or worse.  Perhaps you mean the following:

Code: (c) [Select]
while (*stuff++) ++len;

Ah, no idea how I missed that. Thanks for the correction.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Function to count the length of the string
« Reply #7 on: September 20, 2013, 01:35:42 PM »
  Another issue is that 'break' will break the loop and your "++(*len)" isn't going to run, thus making the return a 0-based count.  The problem with this is that 'strlen()' returns the number of characters in the string, not the last index.
For an additional challenge, I'd suggest trying to find a way to do it recursively.


As for your challenge, I accept it but I think you didn't get the point, the program gives the number of characters in a string, not the last index and I used that 'break' statement as if the char is a 'null', then we don't need to count it as null is automatically added to the end of the string( we all know that ).

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Function to count the length of the string
« Reply #8 on: November 01, 2013, 03:41:47 AM »
What's wrong with the original strlen() function? http://www.cplusplus.com/reference/cstring/strlen

Having the value allocated on the heap, vs, the stack isn't going to give you really any added benefit. For C++ though, the string datatype has a length() function.
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Function to count the length of the string
« Reply #9 on: November 01, 2013, 08:58:13 PM »
It's often useful to code your own version of a function that exists in the standard library.  If nothing else, it's most useful for understanding how it works, why it might work that way and there's always the programming exercise benefit.
-Xires

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Function to count the length of the string
« Reply #10 on: November 01, 2013, 09:21:53 PM »
It's often useful to code your own version of a function that exists in the standard library.  If nothing else, it's most useful for understanding how it works, why it might work that way and there's always the programming exercise benefit.
However once you reach that line where you can't be bothered to re-invent the wheel, you are better off using what is there already - libs are there to shorten the code and speed up the process.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Function to count the length of the string
« Reply #11 on: November 01, 2013, 11:58:49 PM »
It's often useful to code your own version of a function that exists in the standard library.  If nothing else, it's most useful for understanding how it works, why it might work that way and there's always the programming exercise benefit.

Why though? Unless you're aiming for performance gains, but in this case that function is worse than the original. If you absolutely needed a pointer to this value on the heap, it would be a different story, but without knowing the context in which this function *would* be used, this is not a case that demonstrates where it's useful to write your own implementation. Just my opinion though.

"If nothing else, it's most useful for understanding how it works, why it might work that way and there's always the programming exercise benefit." --- This is fun though, I agree with this.  :P I write stuff all the time just to see how I can get things to work.

I don't see the usefulness in this function, other than for learning purposes however.

However once you reach that line where you can't be bothered to re-invent the wheel, you are better off using what is there already - libs are there to shorten the code and speed up the process.

Exactly! A good programmer knows how the functions work and how to use them effectively. If it's a function that is lacking, THEN write your own. (+1)

Cheers
« Last Edit: November 01, 2013, 11:59:49 PM by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

Offline chapp

  • Peasant
  • *
  • Posts: 87
  • Cookies: 2
    • View Profile
Re: Function to count the length of the string
« Reply #12 on: November 02, 2013, 12:07:21 AM »
Also taking into consideration the overhead of allocating a new object every time you are calculating the length of a string, this function is a serious consumer.

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Function to count the length of the string
« Reply #13 on: November 02, 2013, 12:25:13 AM »
Also taking into consideration the overhead of allocating a new object every time you are calculating the length of a string, this function is a serious consumer.

This was one of the reasons I mentioned liking the original function instead too. Perhaps once or twice, not much to be noticed, but in the cases where it is used in repetition, if you're looking at the performance of those iterations which is perhaps wrapped up in a more general function, you would notice the difference here vs the original because this is stack vs. heap allocation.
« Last Edit: November 02, 2013, 12:25:35 AM by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.