18

Spending my time on high level languages it suddenly occurred to me that I did not know the difference between a Character Array and a String. I think they are the same thing but not sure. Is there a difference? Is it just a Character Array with some abstraction?

2
  • you need to specify the language before this can be answered. Commented May 12, 2011 at 8:09
  • 2
    Based on Jus12 - That could be a good answer: a string is a language specific implementation of a char based data structure. Commented Oct 11, 2013 at 19:52

10 Answers 10

21

A String is an abstraction, but of a sequence of characters. It says nothing of implementation. If you wanted to create a String implementation based on a linked list of characters there's nothing stopping you.

In a language such as C, there is very little difference - just that a c string is a null-terminated series of characters at sequential addresses, that is generally accessed through a pointer.

In an OOP language, a String will be an object of some String class. This will probably hold the data in a character array internally, but you don't need to know that. A character array can only be a simple array, but a String class can provide many operations (substrings, regex, etc) on strings if the implementer decides to.

Sign up to request clarification or add additional context in comments.

3 Comments

Haskell is an example of a language where the native string implementation is based on a linked list of characters.
My main problem with this answer, is that there are languages where that isn't true. For instance, in Fortran "string" is a proper type, totally separate from array types. It is no more of an "abstraction" there than "array" is.
"a c string is a pointer to a null-terminated series of characters" no. It is the "null-terminated series of characters" itself, not the pointer to it.
15

a character array is simply an array of characters

a string is data structure that uses an array of characters

some string representations use a null-terminator (like C), others use a length prefix

6 Comments

I think "data structure" is misleading. A classic C string has characters placed one after another in memory with the last byte containing a binary zero. Exactly the same as an array with the exception of that last byte.
Kind of a C-centric answer. There are languages out there where strings and arrays are very different things.
@jay: is that not a structure? Do bad things happen when part of the structure is removed? A String in most languages is a structure even if that's nothing more than an array of chars and either a terminator or other mechanism for specifying the length.
A string is an abstraction, not a data structure. It is implemented in terms of data structures such as arrays or lists, but that is a detail that differs from langauge to language, whereas the abstraction -- a data type that represents a chunk of text -- is largely the same everywhere.
I have to say that I prefer Draemon's emphasis on a string as an abstraction. There have been many, many different implementation used over the years.
|
9

I used to teach programming, and this is how I used to explain this particular issue.

First, focus on what both things have in common: both a char array and a string consist of a sequence of characters. Being a sequence implies that the characters are ordered and that they can be enumerated, for example.

Now focus on what each of the two things add, in their particular different ways, to this common ground.

A char array adds what any array is known to add: indexing and random access to individual items.

A string, on the other hand, adds the fact that the sequence of chars is seen as a whole thing with its own properties. In some implementations, achieving this means altering the way the chars are stored (adding a terminating null in C strings, for example).

This approach (look at the commonalities, then at how things diverge from them) has proven useful in a variety of situations.

Hope this helps.

1 Comment

Exactly: properties of the abstraction versus properties of the implementation.
4

In C these are almost the same, though a string will have an additional null character at the end.

In other languages (Java, C# etc), a string is an object, whereas a character array is an array of ... chars (which are primitive data types).

Normally, strings are implemented with character arrays.

Comments

4

The answer to some extent depends on what language your talking about. In the .Net/C# world strings are immutable objects, whereas a char array you can add/change values easily in the array. Strings can be treated as char arrays in a read-only fashion, as you can iterate over the characters in a string.

In the abstract I think the biggest difference is in how you want to work with them. Are you wanting to work with a chunk of text, say to show a message to an end user, or are you looking at a sequence of characters, doing some processing on the list? It's all rather subjective at a certain level.

Comments

1

It depends on the language. In C-ish languages, they are pretty much synonomous. You could claim the difference is that "strings" have an implicit terminating nul, but that would be splitting hairs.

Fortran is the other extreme. There character arrays and character strings are entirely different types, with different operations available for them.

1 Comment

Without '0'-terminator it's not a C-string. Mostly all str*() clib functions fail, if the 0-terminator is missing.
0

String is a class in java. So it has attributes e.g. length. So when you ask for the size of string it simply returns that instead of computing the value each time. It also other methods e.g. indexOf, substring, etc to make life easy so you don't have to do that yourself.

2 Comments

length() is a method not an attribute.
@Draemon - +1 for mentioning it is a method, but internally it just returns an attribute.
0

A C-style string is internally represented by array of character with the '\0' at end, which indiciates the end of string.

In C++, there's a string container class defined in string.h which provides some typical string operations to manipulate the string.

1 Comment

The C++ class is in string , not string.h , and while it is a container, it is not a container for a C-style string (C++ strings are not null-terminated).
0

A string is the character array terminated by null character ‘\0’

1 Comment

... at least in C.
0

In C, a string is an array of characters terminated by a null character(\0) but

In C++, a string is a class and we use its object and there is no null character at the end but an array of characters contains null character at the end.

Also, we can use operators with the string object in C++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.