• (2 Pages)
  • +
  • 1
  • 2

C-Style and C++ Style Strings Rate Topic: -----

#1 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Posted 09 June 2011 - 12:09 AM

*
POPULAR

C-Style Strings and C++ Style Strings:

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


Is This A Good Question/Topic? 9
  • +

Replies To: C-Style and C++ Style Strings

#2 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

Posted 09 June 2011 - 04:59 AM

Thanks, I actually always used C style strings and was unaware of C++ Style strings altogether.

I moved from C to C++ and that is most likely why. Its good for people to know the difference, even though C Style strings are not commonly used I am sure there are situations when they are needed and thus it is important for people to know the difference between them and thus which one to use in which situation :) thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#3 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Posted 09 June 2011 - 07:20 AM

True however generally in C++, it is recommended that you use a C++ Style String. I have rarely come across a situation where I needed to use a C Style String but having the knowledge is always good I believe :) Thanks.
Was This Post Helpful? 1
  • +
  • -

#4 kingfeanor  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 60
  • Joined: 18-April 09

Posted 09 June 2011 - 09:09 AM

One thing to point out is that neither the default C string mechanism or the std::string library support Unicode. Both only support ASCII characters. For simple stuff that is fine, but as soon as you are trying to write code which might run for someone who doesn't read a language represented by ASCII (character sets like Chinese, Japanese, Cyrillic, etc), you are going to have problems. Any commercial solution and any good open source project should support localization/globalization and to do so, all strings should support Unicode. The easiest way to do so is to grab the ICU library from here. There are other Unicode string libraries also (Xerces has a solution to this as does the QT library among others). My two cents is that it is best to use a library like ICU all the time instead of the built in ASCII only string mechanisms. Then you are familiar with the library and won't get stuck going through and replacing an ASCII based string system with a Unicode one later on.
Was This Post Helpful? 3
  • +
  • -

#5 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Posted 09 June 2011 - 09:11 AM

View Postalpha_x, on 09 June 2011 - 05:59 AM, said:

Thanks, I actually always used C style strings and was unaware of C++ Style strings altogether.

I moved from C to C++ and that is most likely why. Its good for people to know the difference, even though C Style strings are not commonly used I am sure there are situations when they are needed and thus it is important for people to know the difference between them and thus which one to use in which situation :) thanks a lot!


I honestly can't think in of a situation where C strings would be significantly better. I guess one case is when you don't need to perform any operations on strings, just read them; using C strings would then save you the trouble of having to include one more header for the C++ strings... Then again, it's probably better to be consistent and use C++ string whenever you need to pass text around.
Was This Post Helpful? 0
  • +
  • -

#6 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Posted 09 June 2011 - 09:15 AM

Thanks kingfeanor however tbh from my experience, most people I have seen use the ASCII set and tbh like your post shows if they have a problem with the default ASCII String handling then they should look for a solution, My tutorial never aimed to focus on non ASCII standard String handling merely to make the reader aware of the difference between C and C++ Style Strings.
Was This Post Helpful? 1
  • +
  • -

#7 kingfeanor  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 60
  • Joined: 18-April 09

Posted 09 June 2011 - 09:24 AM

View Postv0rtex, on 09 June 2011 - 10:15 AM, said:

Thanks kingfeanor however tbh from my experience, most people I have seen use the ASCII set and tbh like your post shows if they have a problem with the default ASCII String handling then they should look for a solution, My tutorial never aimed to focus on non ASCII standard String handling merely to make the reader aware of the difference between C and C++ Style Strings.


v0rtex, you wrote a great tutorial and you weren't aiming at Unicode. My comment was not intended to diminish what you put together. I just wanted anyone who reads this to know the limitation of C and C++ default strings and to know there are string solutions out there better than std::string. I shot myself in the foot just out of college at my first job by using std::string and then had to back and redo it with a Unicode string library. I'd like so save others that mistake.
Was This Post Helpful? 0
  • +
  • -

#8 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Posted 09 June 2011 - 09:31 AM

@ kingfeanor:

Quote

my first job by using std::string and then had to back and redo it with a Unicode string library. I'd like so save others that mistake.
Wow shame, thats quite annoying. I will edit your link in so that people visit it if they dont bother to read the comments :)
Was This Post Helpful? 0
  • +
  • -

#9 Astraeus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 205
  • Joined: 17-November 09

Posted 09 June 2011 - 09:35 AM

View Postalpha_x, on 09 June 2011 - 05:59 AM, said:

Thanks, I actually always used C style strings and was unaware of C++ Style strings altogether.

I moved from C to C++ and that is most likely why. Its good for people to know the difference, even though C Style strings are not commonly used I am sure there are situations when they are needed and thus it is important for people to know the difference between them and thus which one to use in which situation :) thanks a lot!


We used C-Style strings instead of c++ strings during FIRST robotics last year for some stuff. :3

Nice tutorial! It's good to know both C-Strings and C++ style strings because both have their uses. ^^
Was This Post Helpful? 0
  • +
  • -

#10 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 194
  • Joined: 21-April 09

Posted 09 June 2011 - 11:26 AM

View Postkingfeanor, on 09 June 2011 - 12:09 PM, said:

One thing to point out is that neither the default C string mechanism or the std::string library support Unicode. Both only support ASCII characters. For simple stuff that is fine, but as soon as you are trying to write code which might run for someone who doesn't read a language represented by ASCII (character sets like Chinese, Japanese, Cyrillic, etc), you are going to have problems. Any commercial solution and any good open source project should support localization/globalization and to do so, all strings should support Unicode. The easiest way to do so is to grab the ICU library from here. There are other Unicode string libraries also (Xerces has a solution to this as does the QT library among others). My two cents is that it is best to use a library like ICU all the time instead of the built in ASCII only string mechanisms. Then you are familiar with the library and won't get stuck going through and replacing an ASCII based string system with a Unicode one later on.


Isn't that what wchar_t is for? I mean I use c-style string for this reason exactly.

wchar_t* unicode_c_str = "This is a unicode c-style string";

This post has been edited by code_m: 09 June 2011 - 11:36 AM

Was This Post Helpful? 1
  • +
  • -

#11 kingfeanor  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 60
  • Joined: 18-April 09

Posted 09 June 2011 - 01:24 PM

w_char doesn't, by itself, solve the Unicode problem. Take a look at this article. A lot of Windows API calls use w_char* and the API method do the right things, so for Windows coding, that may be enough. Standard methods in C++ don't support Unicode though (like cin, cout, character based file reading, etc). For these things you need special methods provided by a library or API (like the windows API).
Was This Post Helpful? 0
  • +
  • -

#12 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Posted 09 June 2011 - 10:25 PM

As a note, std::string is just a typedef of basic_string<char>, so you could get C++ string behavior for wide characters by doing typedef basic_string<wchar_t> UnicodeString;. But, as kingfeanor said, this will only provide a way for you to store the Unicode characters -- to actually print them, you'll need something more.

This post has been edited by diego_pmc: 09 June 2011 - 10:27 PM

Was This Post Helpful? 1
  • +
  • -

#13 ryan20fun  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 109
  • Joined: 20-December 10

Posted 09 June 2011 - 10:34 PM

but whats what std::wstring is for, no ?
unless im mistaken and that does not have multibyte support.

This post has been edited by ryan20fun: 09 June 2011 - 10:36 PM

Was This Post Helpful? 0
  • +
  • -

#14 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Posted 12 June 2011 - 12:55 AM

View Postryan20fun, on 09 June 2011 - 11:34 PM, said:

but whats what std::wstring is for, no ?
unless im mistaken and that does not have multibyte support.


That seems to be Microsoft specific. (I might be mistaking.)
Was This Post Helpful? 0
  • +
  • -

#15 ryan20fun  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 109
  • Joined: 20-December 10

Posted 12 June 2011 - 03:11 AM

View Postdiego_pmc, on 12 June 2011 - 12:55 AM, said:

View Postryan20fun, on 09 June 2011 - 11:34 PM, said:

but whats what std::wstring is for, no ?
unless im mistaken and that does not have multibyte support.


That seems to be Microsoft specific. (I might be mistaking.)


oh ok, i thought it was part of the STL since it is declared in the STL string header.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2