Char and String

Dec 2, 2010 at 4:33pm
Sorry to be clogging up the forums, but I have another question. As I make my way through the tutorial, I came across the data types char and string. I know that how char works and its array-like properties, but don't understand the difference between it and string. If there is a difference, could someone explain to me what makes string and char different, and possibly how, when, and why to use string and char in programs.
Thanks,
-Ameobea
Dec 2, 2010 at 4:44pm
char by itself doesn't have "array-like properties". string does.
char represents a single 8 bit character.

a string is basically an array of characters.

Also, keep in mind string isn't standard and needs to be #included from an outside library. (unless you create your own "string" type)
Last edited on Dec 2, 2010 at 4:48pm
Dec 2, 2010 at 4:49pm
std::string is part of the C++ standard. You certainly don't need an "outside" library.
Dec 2, 2010 at 5:27pm
If you can use strings with std, why have the include statement #include <string> in the same file? I understand a program will compile fine if std::string is present and the #include <string> statement is not present. Does the std library have the string class in it?
Dec 2, 2010 at 5:31pm
In the tutorial, it included the following code:

1
2
3
string mystring;
char myntcs[]="some text";
mystring = myntcs;


As I see it, a char is a specialized way of manipulating the characters of a string, and a string is just a string of characters. Can you directly read values into a string and a char using cin, or is there some special way of doing this?
Dec 2, 2010 at 5:56pm
Ameobea wrote:
As I see it, a char is a specialized way of manipulating the characters of a string, and a string is just a string of characters. Can you directly read values into a string and a char using cin, or is there some special way of doing this?


char is not a specialized way of manipulating the characters of a string, however there is a relationship in the sense that strings are essentially arrays of chars, so you can pluck out elements of a string and treat them as chars. You can use cin for both char and strings, however typically you will read strings with the getline function.
Dec 2, 2010 at 6:00pm
tmoney91 wrote:
I understand a program will compile fine if std::string is present and the #include <string> statement is not present. Does the std library have the string class in it?

That's not correct. You do need to #include <string> to use a std::string. It might be that another standard header you're using already includes <string>, but you shouldn't count on that. Incidentally, the standard library is spread across the many standard headers (such as <string>).

Ameobea wrote:
As I see it, a char is a specialized way of manipulating the characters of a string, and a string is just a string of characters.

Not quite. A std::string is simply a container of chars. A char is a basic built-in type, just like int, double and so on. In C, there's no std::string, so a raw array of chars is used, with a null character marking where it ends, since arrays don't "know" their own size. Sometimes, when using a C library, you'll have to convert a std::string into a C string. C++ code shouldn't use C strings unless there's a good reason for that.
Dec 2, 2010 at 6:28pm
citation:
String objects are a special type of container, specifically designed to operate with sequences of characters.

Unlike traditional c-strings, which are mere sequences of characters in a memory array, C++ string objects belong to a class with many built-in features to operate with strings in a more intuitive way and with some additional useful features common to C++ containers.

The string class is an instantiation of the basic_string class template, defined in <string> as:;
typedef basic_string<char> string


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

char
http://www.cplusplus.com/doc/tutorial/ntcs/




Last edited on Dec 2, 2010 at 6:30pm
Dec 2, 2010 at 6:43pm
Thanks for the clarification.
Dec 2, 2010 at 7:18pm
So, if I'm understanding this, you can do the following without error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

char myword[];
string mystring;

cout<<"Enter a string of characters:  /n";
getline (cin, mystring);
cout<<"You entered "<<mystring<</n/n;

cout<<"Enter a string of characters:  /n";
cin>>myword[];
cout<<"You entered "<<myword[];


You can/should use string to store input, but can/should use char when you need to manipulate the characters in a string of input?
Last edited on Dec 2, 2010 at 7:22pm
Dec 2, 2010 at 7:33pm
Ameobea wrote:
You can/should use string to store input, but can/should use char when you need to manipulate the characters in a string of input?


As mentioned before, consider a string as an array of characters, so the same rules essentially apply. Let's say you have the following code:

1
2
string sentence = "This is a sentence";
cout << sentence[3];


You can access any element in the string by using the subscript operator just as you would in a char array. In the example I am outputting the character the is stored in element 3 of the string. Since arrays start indexing at 0, position 3 is the character 's'.

Also, your code has some problems. Try to compile it and see if you can find the errors first.
Last edited on Dec 2, 2010 at 7:35pm
Dec 2, 2010 at 8:46pm
So, what would char be used for if string can do it all?

By the way, I found some of the problems with my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

main()
{
char myword[];
string mystring;

cout<<"Enter a string of characters:  /n";
getline (cin, mystring);
cout<<"You entered "<<mystring<<"/n/n";

cout<<"Enter a string of characters:  /n";
cin>>myword[];
cout<<"You entered "<<myword[];
return 0;
}
Dec 2, 2010 at 10:23pm
1) main must return an int
2) lines 15/16 don't work. You can't just cin a char* and have it work.
Dec 2, 2010 at 11:24pm
Besides that, can you show me how to specifically use both char and string to retrieve and store data? It would really clarify it.
-Ameobea
Dec 9, 2010 at 2:59pm
That's enough for now; thanks, all!
Topic archived. No new replies allowed.