Thread: another strlen

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    6

    another strlen

    Hi people! I'm learning C now and hope someone can help me with the task. I thought I would be able to write strlen function but it doesn't work. Here is the code:

    Code:
    #include "mystrlen.h"
    
    int mystrlen(char *s){
    	
    	int i,k;
    	for(i = 0; s[i] != '\0'; i++){
    		k++;
    	}
    	
    	return k;
    }

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "mystrlen.h"
    
    int main () {
       
        int i;
    	char *p1 = "Use malloc()";
    	char *p2;
    	
    	if (NULL == (p2 = malloc(mystrlen(p1) + 1))){
    		printf("Not enough memory!\n");
    		return 1;
    	}
    	
    	for (i = 0; i <= mystrlen(p1); i++)
    		p2[i] = p1[i];
    	
    	printf("%s\n",p2);
    	free(p2);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You need to initialize k, although you could just use i instead and get rid of k completely.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    Code:
    #include "mystrlen.h"
    
    int mystrlen(char *s){
    
    	int i;
    	
    	for(i = 0; s[i] != '\0'; i++){
    	}
    	
    	return (i);
    
    }
    Running…
    0 0 0 0 0 0 0 0 0 0 //normally it should print Use malloc() here
    Debugger stopped.
    Program exited with status value:0.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    That is very weird...you sure you didn't forget to build the project or anything like that? Because with your original source and with your modified strlen everything was working fine.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    can it be because of mac ? compiler maybe

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Mac should not matter, and I doubt you have found a bug in the compiler. Either you are running the wrong executable, you are compiling the wrong code (or not compiling at all) or the source you posted here is not in line with the source you currently have.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You do need to manually add the null terminator to the end of the string in p2 after your loop, but that's not why you are getting 0s printed.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Actually he doesnt , notice how he loops to i <= strlen, which means he will get the trailing \0 when he does the string copy.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yep. Thanks for the correction.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    big thanx all for help! I woke up today with the idea to reorganize things in the code and it's strange but now it's printing Use malloc() like it should )

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int mystrlen(char *s){
    	
    	int i;
    	for(i = 0; s[i] != '\0'; i++){
    	}
    	
    	return i;
    }
    
    
    int main () {
       
        int i;
    	char *p1 = "Use malloc()";
    	char *p2;
    	
    	if (NULL == (p2 = malloc(mystrlen(p1) + 1))){
    		printf("Not enough memory!\n");
    		return 1;
    	}
    	
    	for (i = 0; i <= mystrlen(p1); i++)
    		p2[i] = p1[i];
    	
    	printf("%s\n",p2);
    	free(p2);
    	
        return 0;
    }
    Code:
    Loading program into debugger…
    Program loaded.
    run
    [Switching to process 1516]
    Running…
    Use malloc()
    
    Debugger stopped.
    Program exited with status value:0.

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    I added mystrcpy function which is replacing strcpy and it returns null, somebody knows why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int mystrlen(char *s){
    	
    	int i;
    	for(i = 0; s[i] != '\0'; i++){
    	}
    	
    	return i;
    }
    
    void mystrcpy(char *s1, char *s2){
    	
    	int i;
    	
    	if (NULL == (s2 = malloc(mystrlen(s1) +1))){
    	   printf("Not enough memory!\n");
    	}
    	
    	for (i=0; s2[i] != '\0' ; i++)
    		s2[i] = s1[i];
    }
    
    int main () {
       
    	char *p1 = "Use malloc()";
    	char *p2;
    	
    	mystrcpy(p1, p2);
    	
    	printf("%s\n",p2);
    	free(p2);
    	
    	return 0;
    }
    This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
    Loading program into debugger…
    Program loaded.
    run
    [Switching to process 2082]
    Running…
    (null)

    Debugger stopped.
    Program exited with status value:0.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that mystrcpy changes the parameter named s2. It does not change the local variable in main named p2. One option is to use a pointer to a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    no idea how to do this

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Aside from the s2 pointer getting lost as soon as the function returns that others have mentioned, there's also:
    Code:
    	for (i=0; s2[i] != '\0' ; i++)
    		s2[i] = s1[i];
    I think you mean:
    Code:
    	for (i=0; s1[i] != '\0' ; i++)
    		s2[i] = s1[i];
    What you're really trying to recreate is strdup(). You can see, the copied-to memory is simply returned by strdup(), because it's easier than having you pass in a pointer to a pointer to the allocated memory.
    Last edited by itsme86; 01-09-2011 at 10:47 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  2. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  3. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  4. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM
  5. strlen
    By dirgni in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2002, 11:57 PM