Science Fair Projects Ideas - Strcat

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Strcat


In computing, the C programming language offers a library function called strcat that allows one memory block to be appended to another memory block. Both memory blocks are required to be null-terminated. Since, in C, strings are not first-class datatypes, and are implemented as blocks of ASCII bytes in memory, strcat will effectively append one string to another given two pointers to blocks of allocated memory.

For example:

 char *str1 = malloc(LARGE_NUMBER_1);
 char *str2 = malloc(LARGE_NUMBER_1+LARGE_NUMBER_2-1);
 
 fgets(str1, LARGE_NUMBER_1, stdin);
 fgets(str2, LARGE_NUMBER_2, stdin);
 strcat(str2, str1); /* the argument order makes it like an assignment - str2 "+=" str1 */

Here is one possible implementation of strcat:

  char *
  strcat(char *dest, const char *src)
  {
    char c;
    const char *p;
    char *q;

    for (q = dest; *q != '\0'; q++)
       ;
    
    for(p = src; *p != '\0'; p++, q++)
       *q = *p;
    
    *q = '\0';

    return dest;
  }

Else it is possible to do it with other string library functions:

  char *
  strcat(char *dest, const char *src)
  {
    char *s = dest + strlen(dest);
    strcpy(s, src);
    return dest;
  }

strcat can be dangerous because if the string to be appended is too long to fit in the destination buffer, it will overwrite adjacent memory, causing unpredictable behavior. Usually the program will simply cause a segmentation fault when this occurs, but a skilled attacker can use such a buffer overflow to crack into a system (see computer security).

The bounded variant strncat does the same thing as strcat but as it only appends a specified number of bytes, it is susceptible to buffer overflow only if the number of bytes specified is too large to fit in the destination string. OpenBSD strlcat is regarded as a safer version of these variants.

09-23-2007 01:00:40
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice