Some usage _c language based on C-language string function

Source: Internet
Author: User

For example, the concatenation function of a string is strcat.
prototype:extern char *strcat (char *dest,char *src);
usage:#include <string.h>
function: to add the string src refers to the end of the dest (covering the end of the ' dest ') and add ' ".
Description:the memory area of SRC and dest can not overlap and dest must have enough space to hold the SRC string.
Returns a pointer to Dest.
Example:

Copy Code code as follows:

Strcat.c
#include <syslib.h>
#include <string.h>
Main ()
{
Char d[20]= "Golden Global";
Char *s= "View";
CLRSCR ();
strcat (d,s);
printf ("%s", d);
GetChar ();
return 0;
}

The above code can have no problem output Golden Global View.
But here's what if you change it like this:
Copy Code code as follows:

Strcat.c
#include <syslib.h>
#include <string.h>
Main ()
{
Char *p= "Golden Global";
Char *s= "View";
CLRSCR ();
strcat (P,s);
printf ("%s", p);
GetChar ();
return 0;
}

The parameter is in accordance with its requirements 2 pointer parameters, but this program can not run. Start baffled, why the parameter type is correct but not to return the desired results. This can only look at the function prototype.

strcat function prototype

Copy Code code as follows:

Char *strcat (char *strdest, const char *STRSCR)//Add a const to the source string, indicating that it is an input parameter
{
char * address = strdest; If the statement is placed on assert, the compilation fails

Assert ((strdest!= null) && (STRSCR!= null)); Add a non 0 assertion to the source address and destination address

while (*strdest)//is a simplified form of while (*strdest!= ' ")
{//If you use while (*strdest++), there will be an error, because + + is not subject to cyclic
strdest++; Constraint. So be in the circulation body + +; Because if *strdest the last finger
}//To the end of the string "".
while (*strdest++ = *strscr++)//is a simplified form of the while (*strdest++ = *strscr++)!= ' "
{
NULL; The cyclic condition can be used with + +,
//Here you can add a statement *strdest= ' "; Is there any need?

return address; In order to achieve the chain operation, the destination address is returned
}

From that, we know why.
Copy Code code as follows:

while (*strdest++ = *strscr++)
{
NULL;
}

If strdest is a pointer, the *strdest here is the value of an unknown address, which is not tolerated by the compiler. But why is it possible when strdest is an array, because the array is equal to assigning it a contiguous address. The security address that you are applying for can certainly be used. Of course we can also write a string concatenation function that actually takes pointers as arguments, and here's a function prototype I wrote myself:
Copy Code code as follows:

Char *strcatdemo2 (char *str1, const char *STR2)//Add a const to the source string, indicating that it is an input parameter
{
Assert ((str1!= null) && (str2!= null));

Char *address = (char *) malloc ((strlen (STR1) + strlen (str2) + 1) *sizeof (char));

char *des = address;

ASSERT (address!= NULL);

while (*STR1)
{
*address = *STR1;
str1++;
address++;
}

while (*STR2)
{
*address = *STR2;
str2++;
address++;
}

*address = ' n ';

Return des;
}

In this, there is a space for the pointer address to store 2 strings, note that there is a need to apply one more, because the string requires a ' to ' end. Use it in this way:
Copy Code code as follows:

int main (int argc, char *argv[])
{
char *p = "Hello,", *s = "world!";

Char *t = StrcatDemo2 (P, s);
Puts (t);

System ("PAUSE");
return 0;
}

The one written above is similar to the function of adding strings in C #.
In fact, most of the C language string is a character array parameter, a character pointer parameter to use. Here is the prototype of these things, you can take a good look at, to avoid future mistakes.

strcat function Prototype:

Copy Code code as follows:

Char *strcat (char *strdest, const char *STRSCR)//Add a const to the source string, indicating that it is an input parameter
{
char * address = strdest; If the statement is placed on assert, the compilation fails
Assert ((strdest!= null) && (STRSCR!= null)); Add a non 0 assertion to the source address and destination address
while (*strdest)//is a simplified form of while (*strdest!= ' ")
{//If you use while (*strdest++), there will be an error, because + + is not subject to cyclic
strdest++; Constraint. So be in the circulation body + +; Because if *strdest the last finger
}//To the end of the string "".
while (*strdest++ = *strscr++)//is a simplified form of the while (*strdest++ = *strscr++)!= ' "
{
NULL; The cyclic condition can be used with + +,
//Here you can add a statement *strdest= ' "; Is there any need?
return address; In order to achieve the chain operation, the destination address is returned
}

strcpy function Prototype:
Copy Code code as follows:

Char *strcpy (char *strdest, const char *STRSCR)
{
Char *address=strdest;
Assert ((strdest!= null) && (STRSCR!= null));
while (*STRSCR)//is a simplified form of while (*strscr!= ' ");
{
*strdest++ = *strscr++;
}
*strdest = ' n '; When the STRSCR string length is less than the original strdest string length
return address; , if you do not change the statement, you will be wrong.
}

strcmp function Prototype:
Copy Code code as follows:

int strcmp (const char *str1,const char *STR2)
{
int len = 0;
Assert ((str1!= ' ") && (str2!= ')");
while (*str1 && *str2 && (*str1 = = *STR2))
{
str1++;
str2++;
}
return *STR1-*STR2;
}

strlen function Prototype:
Copy Code code as follows:

int strlen (const char *STR)
{
int len = 0;
ASSERT (str!= NULL);
while (*str++)
{
len++;
}
return Len;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.