Library functions from string manipulation
To use any of these functions in your code, the header files "strings.h" must be included. It is the programmer s responsibility to check that the destination array is large enough to hold either what is copied or appended to it. C performs error checking in this respect. See the array tutorial for more details. The data type size_t is equivalent to unsigned integer.
Strcpy
Strcpy copies a string, including the null. Character terminator from the source string to the destination. This function returns a pointer to the destination string or a NULL pointer on error. Its prototype is:
Char *strcpy (char *, const char *src);
#include <stdio.h>
#include <string.h>
Int main ( )
{
Char first [80], second [80];
Printf ("Enter a string :");
Gets (first):
Strcpy (second, first);
Printf ("first: %s, and second: %s\n, first, second);
Return O;
}
Strncpy
Strncpy is similar to strcpy, but it allows the number of characters to be copied to be specified. If the source is shorter than the destination, than the destination is padded with null characters up to the length specified. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is:
Char *strncpy (char *dst, const char *src, size_t len);
Strncat
This function appends a source string to the end of a destination string. This function returns a pointer to the destination string or a NULL pointer on error. Its prototype is:
Char *strcat (char *dst, const char * src):
Strncat
This function appends at most N characters from the source string to the end of the destination string. This function returns a pointer to the destination string, or a NULL pointer on error, its prototype is:
Char * strncat (char *dst, const char *src, size_t N);
The strcat function is used to join one string to another.
#include <stdio.h>
#include <string.h>
Int main ( )
{ Char first [80], second [80];
Printf ("Enter a string :");
Gets (first);
Printf ("Enter another string :");
Gets (second) ;
Strcat (first, second);
Printf ("The two strings joined together: %s\n", first):
}
Strcmp
This function compares two strings, if the first string is greater than the second it returns a number greater than zero. If returns a number less than zero. If the string are equal, it returns 0. Its prototype is:
Int strcmp (const char *fist const char *second);
Example. The following example uses the strcmp function to compare two strings.
#include <stdio.h>
#include <string.h>
Int main ( )
{ Char first [80], second [80];
Printf ("Enter a string :");
Gets (first) ;
Printf ("Enter another string :");
Gets (second) ;
If (strcmp (first, second) == 0)
Puts ("The two strings are equal");
Else
Puts ("The two string are not equal ")
Return 0:
}
Strncmp
This function compares the first N characters of each string. If the first string is greater than the second it returns a number greater than zero. If second string is greater, it returns a number less than zero. If string is equal it returns 0. Its prototype is:
Int strncmp (const char *first, const char * second, size_t N);
Strlen
This function returns the length of a string, not counting the null character at the end, That is, it returns the character count of the string without the terminators. Its prototype is;
Size _ t strlen (const char *str):
EXAMPLE. The strlen function is used to determine the length of a string.
#include <stdio.h>
#include<string.h>
Int main ( )
{
Char name [80]:
Int length:
Printf ("Enter your name :");
Gets (name):
Length = strlen (name)
Printf ("your name has %d characters\n", length):
Return 0;
}
Memset
Memset is useful to initialize a string to all nulls or to any character. It sets the first N positions of the string to the value passed. The destination may be specified by a pointer to nay type char int float etc. avoid pointer is used to allow different types to be specified. Its prototype is:
Void *memst (const void *dst, int c, stite_t N);
Trotk Function
The strtok function is used to find the next token in a sting. The token is specified by al list of possible delimiters. The following example reads a line text from a file and determines a word using the delimiters space tab and new line. Each word is then displayed on a separate line.
ECAMPLE
#include <stdio.h>
#include<string.h>
Int Mani ( )
{
FILE *in;
Char line [80];
Car *delimiters = "\t\n":
Char *token;
If ((in = fopen ("c:\\programming\\c\\text.txt", "r" == NULL)
{
Puts ("unable to open the input file");
Return 0;
}
/*Read each line one at a time */
While (! feof (in))
{
/* Get one line */
Fgets (line, 80, in);
If (! feof (in))
{
/*Break the line up into words */
Token = strtok (line, delimiters);
While (token! = NULL)
{
Puts (token);
/*Get the next word *\
Token = strtok (NULL, delimiters):
}
}
}
Fclose (in);
Return 0:
}
Data Structure & Algorithms Assignment Help, Live Experts
Struggling with data structure problems? Data structure subject is quite tough to learn? Need quick assistance in data structure questions? ExpertsMind.com is right place for you where your search ends, We at ExpertsMind offer online data structure assignment help, data structure homework help and data structure and algorithms question's answers by best online support by qualified tutors.
ExpertsMind.com - Library functions from string manipulation Assignment Help, Library functions from string manipulation Homework Help, Library functions from string manipulation Assignment Tutors, Library functions from string manipulation Solutions, Library functions from string manipulation Answers, An introduction to data structures Assignment Tutors