Like Tree3Likes
  • 2 Post By rags_to_riches
  • 1 Post By laserlight

C-Style String and its Length

This is a discussion on C-Style String and its Length within the C++ Programming forums, part of the General Programming Boards category; Hello folks, I need to write a function that takes a C-style string as an input and returns its length. ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    C-Style String and its Length

    Hello folks,

    I need to write a function that takes a C-style string as an input and returns its length. It must be done in a modular way by creating three separate files, stringLen.h, stringLen.C and useStringLen.C. useStringLen.C takes a command line argument and display its length by calling the function stringLen.

    This is my header file.

    Code:
    //// stringLen.h ////
    
    
    size_t strlen(const char * str);
    My loop function to count the length of the string.

    Code:
    //// stringLen.C ////
    #include "stringLen.h"
    
    
    int stringLen(int a, int n)
    {
        int i, ans=1
        for (i=1; i<=n; i++) { ans *=a; }
        return ans;
    }
    And my main function.

    Code:
    
    //// useStringLen.C ////
    #include <iostream>
    #include "stringLen.h"
    
    
    int main(int argc, char *argv[])
    {
        
    return0;
    }

    I know there are errors but I am confused how to integrate the three. Can anyone help me out? I would appreciate it.

    Thank you.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    This is the signature (more or less) for a builtin function called strlen that does what you're supposed to do.
    Code:
    size_t strlen(const char * str);
    You probably want instead to prototype your own function, stringLen() or whatever it's called. But that function also has a number of errors; missing semicolon, multiplication for some strange reason, no string being passed in.

    I would try to write this program in one file at first until you at least have some code that works. Figure out how to pass in a string to your function. Figure out how to call that function with a hard-coded string, and later progress to using a command-line argument. Maybe read some tutorials or pick up a book on C or C++; calculating the length of a string is often covered pretty near the beginning.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Why are you declaring your function as

    Code:
    size_t strlen(const char * str)
    then implementing it as

    Code:
    int stringLen(int a, int n)
    And does it really make sense to pass two integers to a function which is measuring the length of a string?
    Elysia and thedm007 like this.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Well to incorporate the function you need to call it, maybe something like this:
    Code:
    int main()  {
      char  s1[] = "hello";
    
      int len =  stringLen(s1);   // wouldn't this make sense?
     
      return 0;
    }
    And yes, the function you have above would not fit this so you need to work on that.
    Last edited by HowardL; 11-01-2011 at 01:01 PM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Ok, so I have made progress. I've built my header file, implementation file and the file that calls my implementation.

    I've run into a single problem. I keep getting, Program received signal: "EXC_BAD_ACCESS" and then it highlights the while statement in my implementation file. Can anyone help me out?

    Here is my code.

    The header file.

    Code:
    int stringLen(const char str[]);
    Implementation File.

    Code:
    #include "stringLen.h"
    
    
    int stringLen(const char str[])
    {
        int index = 0;
        while (str[index] != '\0')
            index++;
        return index;
    }
    The main function.

    Code:
    #include <iostream>
    #include "stringLen.h"
    
    
    usingnamespacestd;
    
    
    int main(int argc, char *argv[])
    {
        cout << "The string length is: " << stringLen(argv[1]) << endl;
    return0;
    }
    Thank you!

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Weird but it still worked. I'm still curious, why does that error come up? Thank you for your response.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    You did not check that argv[1] exists as a null terminated string (it might not exist, or it might be a null pointer), so perhaps that's where the problem lies.
    thedm007 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Don't know if it's just a miscopy to the post but using g++:
    Code:
    // In main.cpp
    usingnamespacestd; //gets error - in that cout and endl are not found.
    return0;           // gets error - undifined variable.
    // not checking argc just gets "unused parameter" warning.
    
    // In stringLen.h
    #include stringLen.h // will include itself ad infinitum at compile time
                         //  because of "stupid" compiler:)
    After fixing them it runs ok.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > [COLOR=#bf2e9d]usingnamespace[COLOR=#4b8186]std[/COLOR][COLOR=#000000];[/COLOR][/COLOR]
    This is an example of what is being pasted into the edit window.

    Either
    - stop using whatever HTML colouring tool you're using, and let the board do it for you.
    - If pasting from your IDE, then check to see what "paste as text" options you have. If there is none, then first paste into notepad, then recopy and paste to the forum.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 03-08-2011, 05:16 PM
  2. string length
    By larrydeloafer in forum C Programming
    Replies: 11
    Last Post: 03-02-2011, 05:23 PM
  3. How to set a string to length of 0
    By youjieus in forum C Programming
    Replies: 1
    Last Post: 10-25-2007, 04:32 AM
  4. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM
  5. String length
    By Kokila in forum C++ Programming
    Replies: 4
    Last Post: 12-19-2001, 12:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21