Thread: how to count number of strings being used in an array of strings

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    how to count number of strings being used in an array of strings

    hi everyone, my title seems confusing but i didnt know how to word it any other way so ill explain it a bit better here.

    i have declared an array of strings to be,dir[30][13]. Now my program is supposed load file names into those strings and i want to know how i can count the number of strings being used in that array of strings.

    ive tried doing while(dir[i]!=NULL) and various other combinations of functions but i cant think of how to solve this problem.

    In my program if i want to add a new file to the dir with user input and the dir is full (all string arrays are being used) then an error message of "directory is full" will show up.

    your help is appreciated

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Before operating on the array initialize every string in it with value of 0 using memset() function or for loop.
    To count how many strings are entered use strlen() function or check if dir[i][0] != 0

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Or count how many file names you process as you load them into the array and keep that number handy for later use. Start the count with 0 and each time you insert a file name into the array bump the count up by one.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to count number of spaces, certain letters, etc. in strings?
    By blindchicken11 in forum C Programming
    Replies: 2
    Last Post: 11-29-2011, 04:31 PM
  2. count strings in an array?
    By sangamesh in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 08:23 AM
  3. Counting number of strings in an array
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 06-21-2010, 11:25 AM
  4. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  5. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM