What is a string?
Please can you explain me what a string is and give me a pretty plain example?
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn''t remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
Strings are used to tie up stuff.....no, really they are usually an array of characters that say something. For example "string" is a string containing 7 characters (an extra character with a value of 0 is put on the end to declare the end of the string) if an array of chars were used to hold the string used above char[0] = ''s'' char[1] = ''t'' and so on.
Right from MSDN:
string
Data composed of a sequence of characters, usually representing human-readable text.
_____________________________________________________
ICQ #: 149510932
Google - OpenGL - DirectX - Windows Guide Network - MSDN - Symantec Virus Info
"Imagination is more important than knowledge." - Albert Einstein
string
Data composed of a sequence of characters, usually representing human-readable text.
_____________________________________________________
ICQ #: 149510932
Google - OpenGL - DirectX - Windows Guide Network - MSDN - Symantec Virus Info
"Imagination is more important than knowledge." - Albert Einstein
Strings are how programmers call text variables. (From "character string", because you have a bunch of characters strung together, obviously).
Assuming you are programming in C++, you have two type of strings available to you. Bear with me as I have a lot to say about them. Feel free to interrupt me () if you can't follow.
First, C++ strings are a bit easier to use than C strings, so I'll start with them.
string foo = "This is a string.";
You can do whatever you want with foo. Change a letter (foo[6]='z'), the whole string (foo="Another string"), concatenate two strings (foo += "more text") without having to pay attention to the length.
That's it.
Note: Strings and threads have nothing to do with each other, even if there is a 'standard' class for very big strings called 'rope'.
Now, as CoolTomK pointed out, the C-style string, in the shape of a NULL-terminated character array. Here is a summary of how you can get such a string. Feel free to skip it as is it *ahem* a very technical discussion.
Initialize a char array to an explicit character string constant :
char foo[] = "This is a string";
Here you can modify the contents of the foo variable, either by explicitely changing each character (foo[6]='z'; would change the text to "This iz a string"), or using C string functions (strcpy, strcat...). Beware though that since you didn't specify the size of the array (foo[])the compiler only allocated enough memory to hold the original string, plus the NULL character ('\0').
Create a fixed-size char array, and either initialize it or use C string functions :
char foo[32] = "This is a string"; or
char foo[32]; strcpy(foo, "This is a string" );
In this case, you have allocated enough room for 32 char variable. Since a string ends with a NULL character (implicitly added by the compiler when you use a quoted "" string), you can store up to 31 letters. The reason for the NULL termination is that C doesn't keep track of the size (or, for that matter, nature) of arrays, so string functions have to figure out where is the end of the string all by themselves. Thus it has been established that all strings be ended with the NULL character, whose ASCII code is 0 (as opposed to the '0' character, whose ASCII code is 48). This makes it easy to test too (while( !foo[k] ) { do stuff }). In the present case again, since foo was declared as an array, the variable 'own' its contents, which can be modified, but the variable itself cannot. You cannot write foo="Another string", you must use strcpy(foo, "Another string") and be sure that the new text can fit in the space allocated (31+1 characters). Otherwise, you will get what is known as a buffer overflow (yes, the very same thing you hear crackers using to break into systems).
Create a pointer, make it point to a pre-existing string (e.g. an explicit constant).
char* foo = "This is a string";
Now, you see, the text "This is a string" exists somewhere in the program you created. Therefore it has an address and it is possible to create a pointer pointing to that address. The difference with the previous case is that now, the pointer doesn't 'own' the memory reserved by the compiler to hold the string : the string is a static constant. You cannot write foo[6]='z' (though you can still use foo[6] to refer to the 7th letter in the string; you just cannot write to it). However, since foo is a pointer and not an array, you can make it point somewhere else. Thus you can write foo = "Another string";. You are still pointing to a constant string that exist somewhere in the code of your program so you can't modify its contents, not even with strcpy (which would try to write to the address pointed to by the pointer variable).
Finally, you can create a pointer, and make it point to a non-constant string (e.g. a char array). Since dynamically-allocated variables (malloc/free, new/delete) is a sensitive subject, I will not deal with it here.
char foo[] = "This is a string";
char *bar = foo;
Now, the string is owned by foo, just as it was in the first case. Which means that you can modify it at will. But bar is a pointer, so you can make it point to another string if you wish (constant or not, that is your choice). However, when bar points to foo, you are also free to modify the contents, just as if bar really was foo (bar[5]='z' will modify the string stored in foo). Just keep in mind that if you modify the string stored in foo, the text that bar and any other pointer pointing to foo will change too, as they do not hold a string of their own, but are just saying "I really hold the same thing as foo".
That's it for C strings. Now, that wasn't so difficult, was it ?
[edited by - Fruny on March 31, 2002 1:03:47 PM]
Assuming you are programming in C++, you have two type of strings available to you. Bear with me as I have a lot to say about them. Feel free to interrupt me () if you can't follow.
First, C++ strings are a bit easier to use than C strings, so I'll start with them.
string foo = "This is a string.";
You can do whatever you want with foo. Change a letter (foo[6]='z'), the whole string (foo="Another string"), concatenate two strings (foo += "more text") without having to pay attention to the length.
That's it.
Note: Strings and threads have nothing to do with each other, even if there is a 'standard' class for very big strings called 'rope'.
Now, as CoolTomK pointed out, the C-style string, in the shape of a NULL-terminated character array. Here is a summary of how you can get such a string. Feel free to skip it as is it *ahem* a very technical discussion.
Initialize a char array to an explicit character string constant :
char foo[] = "This is a string";
Here you can modify the contents of the foo variable, either by explicitely changing each character (foo[6]='z'; would change the text to "This iz a string"), or using C string functions (strcpy, strcat...). Beware though that since you didn't specify the size of the array (foo[])the compiler only allocated enough memory to hold the original string, plus the NULL character ('\0').
Create a fixed-size char array, and either initialize it or use C string functions :
char foo[32] = "This is a string"; or
char foo[32]; strcpy(foo, "This is a string" );
In this case, you have allocated enough room for 32 char variable. Since a string ends with a NULL character (implicitly added by the compiler when you use a quoted "" string), you can store up to 31 letters. The reason for the NULL termination is that C doesn't keep track of the size (or, for that matter, nature) of arrays, so string functions have to figure out where is the end of the string all by themselves. Thus it has been established that all strings be ended with the NULL character, whose ASCII code is 0 (as opposed to the '0' character, whose ASCII code is 48). This makes it easy to test too (while( !foo[k] ) { do stuff }). In the present case again, since foo was declared as an array, the variable 'own' its contents, which can be modified, but the variable itself cannot. You cannot write foo="Another string", you must use strcpy(foo, "Another string") and be sure that the new text can fit in the space allocated (31+1 characters). Otherwise, you will get what is known as a buffer overflow (yes, the very same thing you hear crackers using to break into systems).
Create a pointer, make it point to a pre-existing string (e.g. an explicit constant).
char* foo = "This is a string";
Now, you see, the text "This is a string" exists somewhere in the program you created. Therefore it has an address and it is possible to create a pointer pointing to that address. The difference with the previous case is that now, the pointer doesn't 'own' the memory reserved by the compiler to hold the string : the string is a static constant. You cannot write foo[6]='z' (though you can still use foo[6] to refer to the 7th letter in the string; you just cannot write to it). However, since foo is a pointer and not an array, you can make it point somewhere else. Thus you can write foo = "Another string";. You are still pointing to a constant string that exist somewhere in the code of your program so you can't modify its contents, not even with strcpy (which would try to write to the address pointed to by the pointer variable).
Finally, you can create a pointer, and make it point to a non-constant string (e.g. a char array). Since dynamically-allocated variables (malloc/free, new/delete) is a sensitive subject, I will not deal with it here.
char foo[] = "This is a string";
char *bar = foo;
Now, the string is owned by foo, just as it was in the first case. Which means that you can modify it at will. But bar is a pointer, so you can make it point to another string if you wish (constant or not, that is your choice). However, when bar points to foo, you are also free to modify the contents, just as if bar really was foo (bar[5]='z' will modify the string stored in foo). Just keep in mind that if you modify the string stored in foo, the text that bar and any other pointer pointing to foo will change too, as they do not hold a string of their own, but are just saying "I really hold the same thing as foo".
That's it for C strings. Now, that wasn't so difficult, was it ?
[edited by - Fruny on March 31, 2002 1:03:47 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
March 31, 2002 12:07 PM
I''m proably just repeating the last two posts, but I like seeing things K.I.S.S. style.
A string is an array of characters that make up words, phrases, gibberish, or pretty much whatever.
In Visual Basic strings are handled as their own data type:
dim stringExample as String
stringExample = "Hello World"
In C++ things are handled a little differently. Strings are, as mentioned above, an array of chars:
char stringExample[] = "Hello World";
A string is an array of characters that make up words, phrases, gibberish, or pretty much whatever.
In Visual Basic strings are handled as their own data type:
dim stringExample as String
stringExample = "Hello World"
In C++ things are handled a little differently. Strings are, as mentioned above, an array of chars:
char stringExample[] = "Hello World";
VB strings are very similar to STL strings in C++. It is generally easier to work with STL strings, and you are more likely to write reliable code.
The standard C representation of a string is much more basic; it is a simple array of characters. In this case, things are actually relatively simple, provided you understand arrays and are comfortable with pointers. One topic here that hasn't been discussed is dynamically allocating C-style strings.
If you're using C-style strings in C++, use new and delete, as they are the preferred method of allocating memory:
The standard C representation of a string is much more basic; it is a simple array of characters. In this case, things are actually relatively simple, provided you understand arrays and are comfortable with pointers. One topic here that hasn't been discussed is dynamically allocating C-style strings.
If you're using C-style strings in C++, use new and delete, as they are the preferred method of allocating memory:
char * strPtr;unsigned int len;cout << "How long should the string be? ";cin >> len;strPtr = new char[len + 1]; //The +1 is for the terminating NULL charactercout << "Nice! Now enter a string: ";cin >> strPtr;cout << "Ah ha! You just entered \"" << strPtr << "\"!\n";//Now reverse the string!unsigned int a = 0, b = len;while(b > a){ //Swap strPtr[a] and strPtr<br></font><br> <font color="blue">char</font> temp = strPtr[<font color="purple">a</font>];<br> strPtr[<font color="purple">a</font>] = strPtr[<font color="purple">b</font>];<br> strPtr[<font color="purple">b</font>] = temp;<br><br> ++a;<br> –b;<br>}<br><br>cout << <font color="darkred">"Now, the real trick: Backwards, what you entered was:\n"</font> << strPtr;<br><br><font color="blue">delete</font> [] strPtr; <font color="gray">//Remember to do this! If you forget, you<br></font><br> <font color="gray">//create what is called a "memory leak."<br></font><br><br>cout << <font color="darkred">"\nDone!\n"</font>;<br> </pre></DIV><!–ENDSCRIPT–><br><br><SPAN CLASS=editedby>[edited by - TerranFury on March 31, 2002 2:20:32 PM]</SPAN>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement