C - STRING HANDLING FUNCTION

 STRING HANDLING FUNCTION –

Header File -   <string.h>

Some string handling functions are :


1.   strlen()

2.   strcpy()

3.   Strcat()

4.   strcmp()

5.   Strrev()


 1. strlen() – It is use to count the number of characters in a string.

SYNTAX-

int variable = strlen( string / character array);

EXAMPLE-

    1. int i=strlen (“hello BCA”);
    2. char x[20]=“Hello”;

int i= strlen (x);

 2. strcpy() –

It is use to assign a string into an array.

SYNTAX-

strcpy( character array, string / character string );

EXAMPLE-

strcpy ( x , “Bhopal”);

‘B’

‘h’

‘o’

‘p’

‘a’

‘l’

‘\0’

 

0          1          2          3          4          5          6          7

 

3. strcat() –

It is use to join two strings.

SYNTAX-

strcat( array, string );

EXAMPLE-

strcat ( x , “M.P.”);

‘B’

‘h’

‘o’

‘p’

‘a’

‘l’

‘\0’

‘M’

‘.’

‘P’

‘.’

 

0          1          2          3          4          5          6          7          8          9          10        11

 

4. strcmp() –

The function strcmp() is used to compare two strings. This function is useful while writing program for ordering or searching strings.

The function accepts two strings as parameters and returns an integer value, depending upon the relative order of the two strings.

Return value

Description

Less than 0

If string1 is less than string2

Equal to 0

If string1 and string2 are identical

Greater than 0

If string1 is greater than string2

SYNTAX-

strcmp( string1 , string2 );

EXAMPLE-

int i= strcmp( ‘BCA’ , ‘bca’);

 

5. strrev() –

It is use to reverse a string.

SYNTAX-

strrev( character array );

EXAMPLE-

#include<stdio.h>

#include<string.h>

main()

{

char x[20];

printf (“Enter name “);

gets(x);

strrev(x);

puts(x);

getch();

}

Output-

Enter name vikash

hsakiv

Comments

Popular posts from this blog

Courses After 12th Science

MS Office Versions List

Computer - Network Topologies