Thread: problem with getting correct string length

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    problem with getting correct string length

    I am having some trouble getting my program to make arrays of the right length. Our assignment is to make functions called "category1", "category2" and so on, all of which analyzes a char array and determines if the passed char array is in the specific category.
    The "category" that I am having trouble with determines if the first third of the char array appears elsewhere in the whole array. Here's my code so far.

    Code:
    int Category4 (char s1[])
    {
       int ndx, ndx2;
       char comp[strlen(s1)/3];
    
       for (ndx = 0; ndx < strlen(comp); ndx++) {
          comp[ndx] = s1[ndx];
       }
    
       for (ndx = strlen(comp); ndx < strlen(s1); ndx++) {
          if (s1[ndx] == comp[0]) {
             for (ndx2 = 0; ndx2 < strlen(comp); ndx2++) {
                if (comp[ndx2] != s1[ndx+ndx2]) {
                   break;
                }
                if (ndx2 == strlen(comp)) {
                   printf("%s is in category 4.\n", s1);
                   return 1;
                }
             }
          }
       }
    
       return 0;
    }
    I pass a 6 character string each time, and the length that I get for the array I created in the function (comp) comes out to 5. Does anyone know why this is happening?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't use string functions if you aren't actually dealing with strings. You have to have a null character on the end of each for it to be considered a string. Say you pass an array of six characters, and it's really a string, so it's actually got seven characters total, just for the sake of argument:
    Code:
    void foo( char bar[] )
    {
        char baz[ strlen(  bar ) = 6, 6 / 3 = 2, baz = 2 ];
        ... now assume you actually make 'baz' a string ...
        ... that leaves you 1 usable character ...
    
        for( x = 0; x < strlen baz = 1; x++ )
            ... whatever you're doing here is only going to happen to 1 character, so you don't need a loop ...
    
    }
    Start looking at that, and see if that's really what you had in mind.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Find String length and Arraysearch
    By s.rajaram in forum C Programming
    Replies: 5
    Last Post: 10-03-2007, 02:28 AM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread