Jumat, 14 Desember 2012

ANSI C++ string class and the C standard library:

he full use of the C standard library is available for use by utilizing the ".c_str" function return of the string class.

01#include <strings.h>
02#include <string>
03#include <stdio.h>
04using namespace std;
05    
06int main()
07{
08   char *phrase1="phrase";
09   string phrase2("Second phrase");
10   char  phraseA[128];
11   char  *phraseB;
12    
13   strcpy(phraseA,phrase2.c_str());
14   phraseB = strstr(phrase2.c_str(),phrase1);
15 
16   printf("phraseA: %s\n",phraseA);
17   printf("phraseB: %s\n",phraseB);
18   printf("phrase2: %s\n",phrase2.c_str());
19}  
Compile and run:
[prompt]$ g++ test.cpp
[prompt]$ ./a.out
phraseA: Second phrase
phraseB: phrase
phrase2: Second phrase

Tidak ada komentar:

Posting Komentar