Implementation Of String Functions In C

30
IMPLEMENTATION OF STRING FUNCTIONS

Transcript of Implementation Of String Functions In C

IMPLEMENTATION OF STRING FUNCTIONS

Hello!

I Am Fazila SadiaRoll No. : BM -014

What Are Strings

“Strings are actually array of

characters terminated by a null

character.Each character occupies one

byte of memory.

String Functions

The prototypes of string functions

can be found in string.h file.The

following is a list of functions found

within the <string.h> header file:

strlen( )

name_variable=strlen(string_name);

Function strlen( ) returns value of type integer

Synatax Of strlen( ) :

Example

Without Using Built In Function

String Concatenation

String concatenation is the operation of

joining character strings end-to-end. For

example, the concatenation of "snow" and

"ball" is "snowball. For two sets of strings S1

and S2, the concatenation S1S2 consists of

all strings of the form vw where v is a string

from S1 and w is a string from S2 .

strcpy( )

Syntax Of strcpy

strcpy(dest,source);

Here, source and destination are

both the name of the string. This

statement, copies the content of

string source to the content of

string destination.

Example

If destination string length is less than source string, entire

source string value won’t be copied into destination string.

For example, consider destination string length is 20 and

source string length is 30.

If you want to copy 25 characters from source string using

strncpy( ) function, only 20 characters from source string

will be copied into destination string and remaining 5

characters won’t be copied and will be truncated.

strncpy( )

strncpy(dest,source,size of)

Syntax Of strcpy( ):

Syntax Of strcmp( )

int strcmp (str_1,str_2);

strcmp is case sensitive, i.e "a" and "A" are

treated as different characters

strncmp( )

Syntax Of strncmp( )

int strncmp(dest,source,size_t);

The strncmp function returns a negative, zero, or

positive integer depending on whether the first n

characters of the object pointed to by s1 are less

than, equal to, or greater than the first n characters of

the object pointed to by s2.

The strncmp function will stop comparing if a null

character is encountered in either s1 or s2

strcmpi( )Syntax Of strcmpi

This function is not case i.e "a" and "A"

are considered same characters.The

strncmpi function returns a negative value

if string1<string2, zero if both are equal, or

positive integer if string1 > string2 .

int strcmpi(dest,source,size_t);

strlwr( )Syntax : strlwr(string_name);

strupr( )

strrev( )

Syntax : strrev(string_name);

Thanks!

Any questions?