Thread: Building an own strlen_function as an exercise

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    36

    Building an own strlen_function as an exercise

    Hi!

    Code:
    #include <stdio.h>
    
    int stringlen(char* field, int length) {
        int i = 0;
    
    
        while(field[i] != '\0') {
            i++;
        }
    
    
        return i;
    }
    
    
    int main() {
        char text[100] = "Hello";
    
    
        /* #define LAENGE 100
    
    
        char array[LAENGE];
    
    
        fgets(array, LAENGE, stdin);
        */
    
    
        int laenge;
        laenge = stringlen(text, 100);
    
    
        printf("%d\n", laenge);
    
    
    	return 0;
    }
    If I would "uncomment" my commented out code and out-comment the used code in the main(), basically if I would use fgets for a string_userinput, then regards to my function the length (would not be "i", but "i - 1". Why is this?
    Has it something with "stdin" to do?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    498
    fgets() always put a NUL char at the end of the buffer.

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by flp1969 View Post
    fgets() always put a NUL char at the end of the buffer.
    ah ok,
    but with NUL char yoou do not mean '\0', since '\0' is also existing within every char-array at the end and hence also within
    every initialized string?

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    may i ask why you have a length parameter for your function

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    don't quote me on this but i think what's happening if you use the commented out code is you get a char array of 99 characters and the hundredth is the \0 character i only say this as i wanted to read in a single number using fgets and needed a buffer size of 2. this could be why i is 99 not 100.
    Last edited by cooper1200; 05-13-2019 at 02:14 AM.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    653
    Your function seems to work correctly (though as cooper1200 asked, why does it take a length parameter?):

    Code:
    c@c:~$ cat 177566.c
    #include <stdio.h>
    
    int stringlen(char* field, int length) {
        int i = 0;
    
    
        while(field[i] != '\0') {
            i++;
        }
    
    
        return i;
    }
    
    
    int main() {
        char text[100] = "Hello";
    
    
        #define LAENGE 100
    
    
        char array[LAENGE];
    
    
        fgets(array, LAENGE, stdin);
    
    
        int laenge;
        laenge = stringlen(array, 100);
    
    
        printf("%d\n", laenge);
    
    
            return 0;
    }
    c@c:~$ ./177566
    Hello
    6
    c@c:~$
    Remember that fgets() leaves the newline ('\n') character at the end of the string. That is counted in the string's length.

  7. #7
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by cooper1200 View Post
    may i ask why you have a length parameter for your function
    Just practice - has no other purpose.

  8. #8
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by christop View Post
    Your function seems to work correctly (though as cooper1200 asked, why does it take a length parameter?):

    Code:
    c@c:~$ cat 177566.c
    #include <stdio.h>
    
    int stringlen(char* field, int length) {
        int i = 0;
    
    
        while(field[i] != '\0') {
            i++;
        }
    
    
        return i;
    }
    
    
    int main() {
        char text[100] = "Hello";
    
    
        #define LAENGE 100
    
    
        char array[LAENGE];
    
    
        fgets(array, LAENGE, stdin);
    
    
        int laenge;
        laenge = stringlen(array, 100);
    
    
        printf("%d\n", laenge);
    
    
            return 0;
    }
    c@c:~$ ./177566
    Hello
    6
    c@c:~$
    Remember that fgets() leaves the newline ('\n') character at the end of the string. That is counted in the string's length.
    ah ok, thanks^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Building a NFA help
    By crashband in forum C Programming
    Replies: 6
    Last Post: 10-25-2013, 03:14 PM
  2. building a DLL
    By sass in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2006, 05:25 AM
  3. need help with building DLL
    By Frantic- in forum C++ Programming
    Replies: 6
    Last Post: 06-25-2005, 11:10 PM
  4. building/using dll's
    By bluehead in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2005, 07:03 AM
  5. building pc..
    By Shadow in forum Tech Board
    Replies: 13
    Last Post: 01-08-2003, 07:53 PM

Tags for this Thread