Thread: strlen intermittent problem

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Unhappy strlen intermittent problem

    Hi all!

    I'm going through tutorial exercises in a textbook and I'm stuck with something I'm sure should be simple!

    I need to show the percentage of capacity used by the string in the array but it just won't work!

    If anybody can offer any adviced I'd be very grateful!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define ARRAYSIZE 20
    
    int main(void)
    {
        char a[ARRAYSIZE];
    
        printf("\nEnter some text: ");
        gets(a);
    
        printf("\nYour string is %d characters long", strlen(a));
    
        printf("\nThis string occupies %d percent of it's array's capacity", strlen(a) / ARRAYSIZE * 100);
    
        return 0;
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1)Never use gets() for any reason. Use fgets() instead.
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

    2) You got your equation all mixed up and you are doing INTEGER arithmetic!
    Think about it this way:
    I am using strlen(a) + 1 .............. out of ..................... ARRAYSIZE
    which is X................... out of....................... 100

    What is X?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The problem is that math on integers only yields integers (whole numbers), and C rounds down. Since strlen(a) will always be less than ARRAYSIZE, dividing it by ARRAYSIZE will always yield zero.

    You need to use floating point numbers, which can have decimal values:
    Code:
    int len = strlen(a);
    printf("\nThis string occupies %d percent of it's array's capacity", 
            (int)((float)len/ARRAYSIZE * 100));
    The red bits are casts; they tell the compiler to treat the variable as if it were a different type. By casting "len" to a float, we get a float back from the division. Multiplying a float will yield a float, so we then need to cast the result back to int.
    Last edited by MK27; 05-10-2011 at 07:51 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Try
    Code:
    printf("\nThis string occupies %d percent of it's array's capacity", strlen(a) * 100 / ARRAYSIZE);

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You guys are still missing the extra 1 used by \0, but if you dont want to be purists, I don't blame you
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Wink Thanks!

    Thanks guys! All great ideas that I can take on board and hopefully remember, good job!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using strcmp and strlen in a Linked List
    By matrixx333 in forum C Programming
    Replies: 4
    Last Post: 11-23-2009, 04:13 AM
  2. Problem: Bubblesort - "strlen" in the function...
    By Petike in forum C Programming
    Replies: 15
    Last Post: 01-24-2008, 09:20 PM
  3. MSVC 6 Intermittent Linker Crashing
    By SMurf in forum Tech Board
    Replies: 1
    Last Post: 11-02-2006, 10:53 AM
  4. Problem with strlen()
    By ilmarculin in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 09:10 AM
  5. strlen problem
    By niroopan in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2002, 05:46 PM

Tags for this Thread