Counting using Lib Cstring

This is a discussion on Counting using Lib Cstring within the C++ Programming forums, part of the General Programming Boards category; need the code for cstring counting: this is string counting: Code: #include <string.h> ... char name[25]; int length; length = ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    72

    Counting using Lib Cstring

    need the code for cstring counting:

    this is string counting:

    Code:
    #include <string.h>
    
    ...
    
    char name[25];
    int length;
    
    length = strlen(name);
    could someone provide the code to count using the library, cstring. The declaration of character variables is using the class, string.

    ex.
    Code:
    string name;
    Thanks very much, wanna keep working on my program.

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    could you please clarify what you want? your post is very cryptic.
    PHP and XML
    Let's talk about SAX

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    i need the code that is similar to strlen(), but since i'm using cstring it will not work. I need the code which will count the number of characters in a variable, using a command which is not in string.h/

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To use the string class in STL you have to have an include like this:

    #include <string>

    If your compiler is STL and namespace compliant then it probably also drops the h extension and uses the c prefix for the old standard files. Therefore to work with stuff in string.h in the new fangled compilers you need an include that looks like this:

    #include <cstring>

    Now how to communicate between the two. Well, there is little reason to do so, except for educational purposes because the vast majority of functions in cstring are methods in string as well. If you really want to use cstring functions on a string then you need to extract the underlying char[] in the string class using the c_str() method.

    //copy a string into a char[] and determine it's length;
    #include <iostream>
    #include <string>
    #include <cstring>


    string word = "gopher";
    char embedded[80];
    strcpy(embedded, word.c_str());//stcpy and strlen are in cstring

    cout << word.length() << ", " << strlen(embedded);
    Last edited by elad; 12-13-2002 at 05:53 PM.

  5. #5
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    try
    sizeof()

    if cstring (not familiar with it) has no terminating /0 like character arrays do, then do
    length=sizeof(name)-1;
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 12:29 PM
  2. Writing CObjects with Serialization to CArchive
    By ruben_gerad in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2006, 08:25 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Help with CString class and operator+ overloading
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 11:23 AM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM

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