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.
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 !
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>.
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
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
usingnamespace std;
int main()
{
// C-strings
cout << " C-strings\n\n";
{
char buffer[50] = "";
constchar * 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 (unsignedint 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 (unsignedint 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";
constchar * 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.
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.
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:
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