C++ concatenates two strings to implement code similar to strcat

  • 2020-04-01 21:26:43
  • OfStack

 
#include "stdafx.h" 
#include<iostream> 
using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
char s1[60]="kingbaby"; 
char *s2="hello"; 
int i=0;int j=0; 
while(s1[i]!='0')i++; 
while((s1[i]=s2[j])!='0'){ 
j++;i++; 
} 
cout<<s1<<endl; 
return 0; 
} 

Method 2
 
#include "stdafx.h" 
#include<iostream> 
using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
char a[20] ="aaaa"; 
char b[10]="bbb"; 
char *stra=a; 
char *strb=b; 

while(*stra!='0')stra++; 
while(*strb!='0') 
{ 
*stra=*strb;//Simultaneous movement of pointer
strb++; 
stra++; 
} 
return 0; 
} 

Related articles: