Thread: Urgent help in finding the length

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Urgent help in finding the length

    Hello all,

    I need help with finding the length of the string input by the user.

    It prints out 100 each time.

    Also i cannot use the strlen function

    Also is there a way that each of those strings can be converted to single characters? like each of s can be assigned to a char?

    Thank You for ur help...

    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    int main( int argc, char *argv[]) {
        char s[MAX];
        int i, j=0;
    //    char c;
    
        printf("Enter a string\n");
        fgets(s, MAX, stdin);
    
        for(i =0; i<MAX; i++ ) {
           s[MAX] = j;
           j++;
        }
    
        printf("The repetition of this line is:");
        puts(s);
    
        printf("The length is %d.\n", j);
        return 0;
    
    }
    Last edited by s.sidak; 10-31-2010 at 08:55 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    look up strlen()....

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Sorry, I cannot use strlen

    Is there any other way to find the length without the strlen function?


    Thanks

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You can't use strlen() ... why not? It's a standard C library function...

    I suppose you could write your own version. All strlen() does is loop through a string looking for the null character at the end. Should be easy enough to duplicate...
    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    int main( int argc, char *argv[]) {
        char s[MAX];
        int i, j=0;
    //    char c;
    
        printf("Enter a string\n");
        fgets(s, MAX, stdin);
    
        for(i =0; i<MAX; i++ ) {
          if (s[i] == null)     // exit when null found
            { j =  i;
               break; }
        }
    
        j = (j == 0) ? Max : j;  // return MAX if no null
    
        printf("The repetition of this line is:");
        puts(s);
    
        printf("The length is %d.\n", j);
        return 0; }
    Last edited by CommonTater; 10-31-2010 at 09:18 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there any other way to find the length without the strlen function?
    The entirety of the standard library can be written in C, so it stands to reason that if you can't use strlen, you can write your own function that does exactly the same thing:
    Code:
    size_t my_strlen(const char *s)
    {
        size_t n = 0;
    
        while (*s != '\0') {
            ++n;
            ++s;
        }
    
        return n;
    }
    >You can't use strlen() ... why not? It's a standard C library function...
    Obviously because the OP's teacher is one who favors stupid restrictions as a teaching style. It's much easier than putting forth the effort of coming up with practical problems.

    >if (s[i] == null) // exit when null found
    What's null? I can only assume you meant NULL, but keep in mind that NULL shouldn't be used in place of '\0'. It's strictly meant for use in the context of a pointer.
    Code:
    for(i =0; i<MAX; i++ ) {
          if (s[i] == null)     // exit when null found
            { j =  i;
               break; }
        }
    
        j = (j == 0) ? Max : j;  // return MAX if no null
    This is a very awkward algorithm. j isn't needed at all because the loop will stop when i reaches MAX. You could simply assign i to j after the loop, or use i directly:
    Code:
    for (i = 0; i < MAX; i++) {
        if (s[i] == '\0')
            break;
    }
    
    /* i == MAX || s[i] == '\0' */
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. Finding length of char **
    By Zarniwoop in forum C Programming
    Replies: 12
    Last Post: 04-24-2008, 06:48 AM
  4. Need help with a snake game (ncurses)
    By Adam4444 in forum C Programming
    Replies: 11
    Last Post: 01-17-2007, 03:41 PM
  5. Finding maximum queue length
    By crepincdotcom in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-05-2004, 09:31 AM