C-string vs string objects

Hey guys. Although I use this website frequently I've never actually needed to make a post. Anyways, I have a huge programming exam coming up and I've found myself pretty confused on a certain topic. I really don't know how to distinguish between c-strings and string objects. I had to do an assignment where I make a password verification program and in the description she wrote that we should try doing it with both c-strings and string objects. Can anyone explain what the difference would be?

Are there different functions that can be used with the two? Is the declaration different? As of right now I think string objects are just strings while a c-string is a character array. Can only one of these use the strlen function for example? Can both use the isupper function? I just really am not sure how to distinguish the two. I want to go about completing the assignment with both types of strings like she suggested but since I am not sure on the difference i'm kind of stuck. Long first post I know but I really feel like I should understand this topic.

Thanks!
Oh and also im a bit confused about cin.get and cin.getline. Are these only for string objects that need multiple words read in? How do I know which one to use and what are the key differences? Sorry for all the questions !
Hello edifarnecio!

I am very new to all of this, and this is my first day on the forum, so if this doesn't help, please don't mind me. :)

After reading through your question I did some reading here: https://www.cs.fsu.edu/~myers/cop3330/notes/strings.html and felt it did a good job explaining some key differences between c-string and string objects.

Hope it helps!
Lol funny you should link me that. My programming professor actually wrote that and I found it just didn't help as much as I needed.
Wow, small world!
Sorry I couldn't help more. I hope that someone can go into greater depth for you.
This isn't intended to be thorough, just a few key points.
The C-string is a plain array of characters, with a null terminating byte.
To use it, include the header <cstring>.

The std::string is a C++ class. It also makes use of an array of characters, but neatly wrapped in a container together with other information such as the current size and capacity of the string. To use it, include the header <string>.

See reference page:
http://www.cplusplus.com/reference/cstring/
http://www.cplusplus.com/reference/string/string/
tutorial:
http://www.cplusplus.com/doc/tutorial/ntcs/

Can only one of these use the strlen function for example? Can both use the isupper function? I just really am not sure how to distinguish the two.


To tell them apart, look at where it is declared. A C-string will be either an array of char or a pointer to char. A C++ string will be a string or std::string

Examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    
    // C-strings
    cout << " C-strings\n\n";
    {
        char buffer[50] = ""; 
        const char * a = "Apple";
        char b[]  = "Pear";
        
        cout << "length of a is " << strlen(a) << '\n';
        cout << "length of b is " << strlen(b) << '\n';
        cout << "length of buffer is " << strlen(buffer) << '\n';
                
        strcpy(buffer, a);    // copy a to buffer
        strcat(buffer, " ");  // concatenate a space
        strcat(buffer, b);    // concatenate b

        cout << "length of buffer is " << strlen(buffer) << '\n';
        cout << "contents of buffer is " << buffer << '\n';
        
        for (unsigned int i=0; i<strlen(buffer); ++i)
            buffer[i] = toupper(buffer[i]);

        cout << "contents of buffer is " << buffer << '\n';
                
    }
    
    // C++ std::strings
    cout << "\n\n C++ std::strings\n\n";
    {
        string buffer; 
        const string a = "Apple";
        string b       = "Pear";
        
        cout << "length of a is " << a.size() << '\n';
        cout << "length of b is " << b.length() << '\n';
        cout << "length of buffer is " << buffer.size() << '\n';
        
        buffer = a;             // copy a to buffer
        buffer = buffer + " ";  // concatenate a space
        buffer += b;            // concatenate b

        cout << "length of buffer is " << buffer.size() << '\n';
        cout << "contents of buffer is " << buffer << '\n';

        // or do it all in one statement        
        buffer = a + " " + b;

        cout << "length of buffer is " << buffer.size() << '\n';
        cout << "contents of buffer is " << buffer << '\n';
        
        for (unsigned int i=0; i<buffer.size(); ++i)
            buffer[i] = toupper(buffer[i]);

        cout << "contents of buffer is " << buffer << '\n';

        // use C++11 range-based loop                
        for ( char & c : buffer )
            c = tolower(c);

        cout << "contents of buffer is " << buffer << '\n';
                
    }
    
    // conversion between the two
    cout << "\n\n Conversion between the two\n";
    const char * a = "Something";
    string b = a;   // from C-string to std::string
    
    string c("Another");
    char d[10] = "";         // make sure destination is large enough
    strcpy(d, c.c_str() );   // from std::string to C=string
    
    cout << "a = " << a << "   b = " << b 
         << "   c = " << c << "   d = " << d << '\n';

}

Output:
 C-strings

length of a is 5
length of b is 4
length of buffer is 0
length of buffer is 10
contents of buffer is Apple Pear
contents of buffer is APPLE PEAR


 C++ std::strings

length of a is 5
length of b is 4
length of buffer is 0
length of buffer is 10
contents of buffer is Apple Pear
length of buffer is 10
contents of buffer is Apple Pear
contents of buffer is APPLE PEAR
contents of buffer is apple pear


 Conversion between the two
a = Something   b = Something   c = Another   d = Another

Oh and also im a bit confused about cin.get and cin.getline. Are these only for string objects that need multiple words read in? How do I know which one to use and what are the key differences? Sorry for all the questions !


getline() comes in two different syntax versions. One is for C-strings. The other for std::strings.

http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/string/string/getline/

The main difference, other than the syntax, is that the C-string version needs you to tell it the size of the character buffer you have, to avoid buffer overflows. The C++version automatically handles that and adjusts the string size if necessary, so is easier and safer to use.
Thanks a lot! That really helped. Although I still am not sure when I should cin.getline vs just cin you really clarified a lot for me. I appreciate it.
cin.getline does what it says: it reads one line of input. Using just cin will read until the next space or newline character. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string input;
    cout << "Cin: ";
    cin >> input;
    cout << "You entered: " << input << "\n";
    cout << "Getline: ";
    getline(cin, input);
    cout << "You entered: " << input << "\n";
}


The program will only store the first word of the input when using cin, leaving the leftover input in the buffer. getline reads until the next newline, so you can enter an entire sentence like "This is std::getline!".
If you are using Visual C++ then use CString; the CString Class is actually using the TCHAR character type which is substituted by the precompiler with the type of character underneath char or wchar_t where char is ASCII & wchar_t is unicode & this is identified automatically when compiled, please see the following Microsoft article:

https://msdn.microsoft.com/en-us/library/aa300688(v=vs.60).aspx

std::string is a more commonly used in other versions of C++ and its used for ASCII characters only but you should use std::wstring for the Unicode wchar_t characters

I found it easier to use CString & its functions rather than dealing with the complications of CHAR & TCHAR
Topic archived. No new replies allowed.