#include <iostream>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void TachLayTrai(char *strGoc, char *&strDau, char *&strSau);
void TachLayPhai(char *strGoc, char *&strCuoi, char *&strTRuoc);
int main(int argc, char** argv) {
char *strGoc=new char[225];
strcpy(strGoc,"Tran Chien Cong");
char *strDau=new char[225];
char *strSau=new char[225];
TachLayTrai(strGoc,strDau,strSau);
cout<<strDau<<endl;
cout<<strSau<<endl;
cout<<"----------------\n";
char *strCuoi=new char[225];
char *strTruoc=new char[225];
strcpy(strCuoi,"");
strcpy(strTruoc,"");
TachLayPhai(strGoc,strCuoi,strTruoc);
cout<<strCuoi<<endl;
cout<<strTruoc<<endl;
return 0;
}
void TachLayTrai(char *strGoc, char *&strDau, char *&strSau){
//Tran Chien Cong
char *p=strchr(strGoc,' ');
int space=p-strGoc;
strncpy(strDau,strGoc,space);
p+=1;
strSau=p;
}
void TachLayPhai(char *strGoc, char *&strCuoi, char *&strTruoc){
// Tran Chien Cong
int j=0;
for (int i=strlen(strGoc);i>=0;i--){
if (strGoc[i]==' '){
j=i+1;
break;
}
}
int i2=0;
for (int i=j;i<strlen(strGoc);i++){
strCuoi[i2]=strGoc[i];
i2++;
}
strncpy(strTruoc,strGoc,j-1 ); // HEREEEEEEEEEEEE
}
Cho em hỏi tại sao j-1 j-2 j-3 nó lại điền ký tự random vào ạ ??? . Em cảm ơn ạ.