Thread: strlen function help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    strlen function help

    hello
    im just confused even after some reading of how to write the strlen function
    i dont want to call it from the string library

    here's how far ive got so far
    Code:
    #include <stdio.h>
    #include "simpleio.h"
    
    int strlen(char *myString)
    {
    	
    	return 0;
    }
    
    int main()
    {
    	char myNum[256];
    	int x;
    	int i;
    
    	printf("enter the numbers: \n");
    	getString(myNum,255);
    
    	x = strlen(myNum);
    	for(i=x-1;i>=0;i--)
    	{
    		printf("%c \n", myNum[i]);
    	}
    	return 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Run through 'myString' until you encounter a zero. The two most common approaches are to either increment a counter inside the loop or else subtract the start address with the address of the last character (the null terminator, that is) to obtain the length.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    i just dont know how i should program this
    the logic is simple

    a basic structure on how it should be done just to give me a step would be appreciated

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    int strlen(char *myString)
    {
            int len = 0;
    	char *p = myString;
            while(*p)
                 len++, p++;
    	return len;
    }

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    thanks loads that did the job

    i think i just need to learn how to break functions down on a further level
    thanks again

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Tool View Post
    Code:
    int strlen(char *myString)
    {
            int len = 0;
        char *p = myString;
            while(*p)
                 len++, p++;
        return len;
    }
    That's very generous of you, but then how is anyone to learn anything if we do all of their fishing for them? Give advice, pseudocode, and tips...not the entire solution!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Tip #1: recursion is fun.
    Code:
    int mystrlen( char *s )
    {
        return s && *s ? 1 + mystrlen( s + 1 ) : 0;
    }

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

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    aadil7,

    How about since tool just did the task for you, why don't you breakdown the (short) function Tool wrote for you and explain it to us?

    If you really want to learn it'll do you good.

    -Feen

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    1
    Code:
    int strlen(char *myString)
    {
    	int len=0;
    	while(myString[len]!='\0')
    		len++;
    	return len;
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int mystrlen( char *s )
    {
        char *p = s;
        while( p && *p )
            p++;
        return p - s;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As someone once said give people fishing rods not fish itself!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If someone has already given them a fish, why does it hurt to give them another one? Especially if it's a fish they don't know how to clean when asked. Or something.


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

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by quzah View Post
    Tip #1: recursion is fun.
    Code:
    int mystrlen( char *s )
    {
        return s && *s ? 1 + mystrlen( s + 1 ) : 0;
    }

    Quzah.
    It is much slower too!
    Devoted my life to programming...

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Sipher View Post
    It is much slower too!
    I didn't write it because it was the optimal solution. I wrote it because... wait a minute, didn't I already explain this? Go read Tip #1 again.


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

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Quote Originally Posted by Feengur View Post
    aadil7,

    How about since tool just did the task for you, why don't you breakdown the (short) function Tool wrote for you and explain it to us?

    If you really want to learn it'll do you good.

    -Feen
    i do understand the coding
    its just that im still new to the whole programming side of things, so i didn't know how to actually phrase things together. but im working on C alot more now so hopefully it will click sooner rather than later.

    and heres what it means:
    Code:
    int strlen(char *myString)
    {
    int len = 0; /*make an integer called len and set it to 0*/
    char *p = myString; /*p is a pointer to myString*/
    while(*p) /*while loop stating while p is still pointing to a character increment len and p so it moves across to the next character*/
    len++, p++;
    return len; /*len has been counting how many characters have been noted*/
    }
    Last edited by aadil7; 03-24-2010 at 03:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM