C ++ function subject --strcat

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/chen1083376511/article/details/91640465

Title: Make the output of the following code? 

char d[20]="123";
char s[20]="abc";
strcat(d,s);
int i=0;
while(d[i++]!='\0') 
i++;
printf("%d",i);

[Refer to the answer]

Results: 7.

Topic: Why strcat (string, '!') ; Does not work?

[Refer to the answer]

The difference between character strings and obvious, while strcat () for string concatenation.

use correctly:

strcat (string, "!"); // it is a string, and a '\ 0' composition '!' '!'.

Title: Please realize strcat function.

[Refer to the answer]

    char *mystrcat(char *Dest,const char* Source)
{
        if(Dest==NULL || Source==NULL)
            throw "非法操作";
    char *temp=Dest;
    while(*Dest!='\0')
         Dest++;
    while(*Source!='\0')
         *Dest++=*Source++;
    *Dest='\0';
return temp;
}
    int main()
{
    char a[20]="world",b[]="hello";
    mystrcat(a,b);
    cout<<a<<endl;
         return 0;
}

 

Guess you like

Origin blog.csdn.net/chen1083376511/article/details/91640465