C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 306,749 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,900 people online right now. Registration is fast and FREE... Join Now!





String to Char

Converts a string to a char array

Submitted By: ImaSexy
Actions:
Rating:
Views: 121

Language: C++

Last Modified: November 2, 2009

Snippet


  1. string str = "ABCDEFG"; // input string
  2. char *buf = new char[strlen(str.c_str())]; // char
  3. strcpy(buf,str.c_str());
  4. for(int i=0; i<7; i++)
  5. {
  6.      cout<<buf[i]<<endl;
  7. }

Copy & Paste


Comments


jwwicks 2009-11-05 20:03:13

Why not use string::copy() and string::length()? string str = "ABCDEFG"; size_t len = str.length(); char* buf = new char[len] str.copy(buf, len); buf[len]='\0'; Jw


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month