In C++, there are two types of Strings that you should be aware of:
The first is a C-Style String which is actually a array of characters which is terminated by a null operator. The null operator is this character:
'\0'
A common method of declaring C-Style Strings is like so:
char message[] = {'I','a','m',' ', 'a', ' ', 'C', '-','s','t','y','l','e',' ', 's', 't','r','i','n','g'};
Here is an example whereby we print out a C-Style String/Character Array:
#include <iostream>
int main()
{
using std::cout;
//This is an example of how to instantiate and print out a C-Style String/Character Array:
char message[] = {'I',' ','a','m',' ', 'a', ' ', 'C', '-','s','t','y','l','e',' ', 's', 't','r','i','n','g'};
cout << message;
return 0;
}
;Now whilst that does the job, I am sure you have noticed that it is a real pain to declare a C-Style String this way, as such you can instantiate the String in a more C++ fashion like so:
#include <iostream>
int main()
{
using std::cout;
//Simpler way of initializing a C-Style String/Character Array
char message[] = "I am a C-Style String";
cout << message;
return 0;
}
Now that you know how to declare a C-Style String, Lets look at some common methods that are needed for String handling such as:
- Copying a C-Style String's contents:
There are two methods for this, mainly strcpy() and strncpy().
strcpy() copies the entire contents from one String to another, the method has a format like so:
strcpy(DESTINATION_STRING, SOURCE_STRING);
Hence it takes the contents from the SOURCE_STRING to the DESTINATION_STRING, a simple example of this is below:
#include <iostream> #include <string.h> //necessary to use required C-Style String functions! int main() { using std::cout; //This is an example of how to use [b]strcpy[/b](): const int messageLength = 13; //length of message1 minus terminating character. char message[] = "I am String 1"; char message2[messageLength + 1] = {'\0'}; /*creates a C-Style String with 25 characters each instantiated to the null character hence when you fill the String with String 1's data, it will replace the null characters up to its max length but still will have a null character available to terminate the C-Style String.*/ strcpy (message2, message); cout << message << std::endl; cout << message2; return 0; }
What however if you wished to not copy the entire C-Style String? Well, this is where strncpy() comes in:
It will copy N elements of the character array specified. The max elements to copy is defined by you in the form of a third parameter, so the format for the method is:
strncpy(DESTINATION_STRING, SOURCE_STRING, MAX_ELEMENTS_TO_BE_COPIED);
Obviously this is not the documentation MSDN or another source will give you but it illustrates to you much like the other method's format did, how the method is used!
So a simple example of strncpy():
#include <iostream> #include <string.h> //necessary to use required C-Style String functions! int main() { using std::cout; //This is an example of how to use strncpy(): const int messageLength = 11; //length of message1 minus terminating character. char message[] = "I am String 1"; char message2[messageLength + 1] = {'\0'}; /*creates a C-Style String with message1's length + space for a null terminating character. */ strncpy (message2, message, messageLength); cout << message << std::endl; cout << message2; //note how it only copies up to the 11th element as defined by our integral constant. return 0; }
- Receiving input and storing it in the C-Style String:
One of the simplest methods in which to do this is to use the cin.get() method:
The format for this method is as follows:
cin.get(CHARACHTER_ARRAY, MAX_ELEMENTS_TO_GET);
So an example of utilizing this method (with comments to make it easy to follow) is:
#include <iostream> #include <string.h> //necessary to use required C-Style String functions! int main() { //example of how to recieve input into a C-Style String: using std::cout; using std::cin; char message[30] = {'\0'}; /*creates a C-Style String with 30 characters each instantiated to the null character hence when you fill the String with String 1's data, it will replace the null characters up to its max length but still will have a null character available to terminate the C-Style String.*/ cout << "Enter a String to Store in C-Style String message:" << std::endl; cin.get (message, 29); //use 29 so that we do not get a bufferOverflow error whilst adding null terminating character. cout << message; return 0; }
Note at line 12, when we only fetch 29 characters at maximum, this is essentially our character array's length - 1 as we need space for the null terminating character!
Now that you hopefully have a grasp on what a C-Style String is (simply a character array), you are probably thinking:
"Surely there is easier and more C++ ways to do this?!" Well you are in luck, although C++ inherited the C-Style String and the library of functions that includes strcpy() and strncpy() from C, these functions are not done in a Object-Orientated manner in the way that the character arrays are static. You define how large they are and they always take up that much room in memory (Note: on most systems, 1 character == 1 byte), even if you do not need it all and obviously writing past the end of the array can result in unexpected and often unwanted results (This is known as writing past the array's boundaries and most compilers do not warn your about this and the code results in a StackCorruption or BufferOverflow error). The main problem with the C-Style String is that it requires the programmer to handle memory management and this can often be frightening for beginners. In order to address this there is string methods in the Standard library (std) which handles all the C++ Style Strings methods etc...
In order to initialize a C++ Style String, you would do the following:
std::string myString ("This is my C++ String");
As I am sure you can see this is a lot easier than utilizing a C-Style String and leaves all the worrisome tasks such as Memory Management to the Compiler. How about copying Strings? This is even simpler and you simply do the following much like you would do with a Integer Data type:
std::string myString ("This is my C++ String");
std::string mySecondString;
mySecondString= myString;
Now I am sure you can see (or are starting to see) the advantages of the C++ Style String but how about receiving input into the String?
The most common method I have come across for this is to use the getline(INPUT_METHOD, STRING) method. Now lets do a code listing that will feature all of the above!
#include <iostream>
#include <string> //needed for C++ Style Strings!
int main()
{
using std::cout;
//declare a C++ Style String
std::string myString ("This is a C++ String! ");
//display the string
std::cout << "myString = " << myString << std::endl;
std::string mySecondString;
//assign to make a copy of the first string in the second
mySecondString = myString;
//display the 2nd String
std::cout << "mySecondString = " << mySecondString << std::endl;
//receive input to mySecondString;
getline(std::cin,mySecondString);
cout << "New data stored in mySecondString is: " << mySecondString << std::endl;
return 0;
}
Another thing that is super easy with a C++ Style String is to concatenate two Strings, you simply add them like you would a integer:
#include <iostream>
#include <string> //needed for C++ Style Strings!
int main()
{
using std::cout;
//declare a C++ Style String
std::string myString ("This is a C++ String! ");
//display the string
std::cout << "myString = " << myString << std::endl;
std::string mySecondString;
//assign to make a copy of the first string in the second
mySecondString = myString;
//display the 2nd String
std::cout << "mySecondString = " << mySecondString << std::endl;
//concatenate two Strings:
std::string result;
//overwrite mySecondStrings data:
mySecondString = "This is a second C++ String!";
result = myString + mySecondString;
cout << result << std::endl;
return 0;
}
I hope that the following tutorial has made you aware of C and C++ Style Strings, it is up to you to use either one however I strongly suggest to use C++ Style Strings as:
1) They make your life a lot easier.
2) They are a part of the new accepted ANSI standard for C++.
3) You are using C++ not C
I hope this tutorial has helped you understand the difference between C and C++ Style Strings, there are many more methods for both types of Strings and I am sure a simple Google search will get you started on learning even more!
Thanks,
v0rtex
_______
EDIT: Please note that C Style Strings and C++ Style Strings (using std::string) do not support Unicode, should your application for some reason need to work with a non ASCII charachter set (Chinese, Japanese etc...) A seemingly good library to utilize is The ICU Unicode Libraries However I am sure there are other ways to accomplish this.
This post has been edited by v0rtex: 09 June 2011 - 11:54 AM







MultiQuote








|