Saturday 31 August 2013

String Handling Function in C | C Programming Language Material pdf



STRING HANDLING FUNCTIONS IN C:

There are four important string Handling functions in C language.
(i)   strlen( ) function
(ii)  strcpy( ) function
(iii) strcat( ) function
(iv)  strcmp( ) function

(i)   strlen( ) Function:
strlen( ) function is used to find the length of a character string.
Ex:
int n;
char st[20] = “Bangalore”;
n = strlen(st);
=> This will return the length of the string 9 which is assigned to an integer variable n.
=> Note that the null charcter „\0? available at the end of a string is not counted.

(ii) strcpy( ) Function:
strcpy( ) function is used to copy from one string to another string.
Ex :
char city[15];
strcpy(city, “BANGALORE”) ;
This will assign the string “BANGALORE” to the character variable city.
Note that character value like
city = “BANGALORE”; cannot be assigned in C language.

(iii) strcat( ) Function:
strcat( ) function is used to join character, Strings. When two character strings are joined, it is referred as concatenation of strings.
Ex:
char city[20] = “BANGALORE”;
char pin[8] = “-560001”;
strcat (city/L, pin/R);
=>  This will join the two strings and store the result in city as “BANGALORE –560001”.
=>  Note that the resulting string is always stored in the left side string variable.

(iv) strcmp( ) Function:
strcm ( ) function is used to compare two character strings.
=>  It returns a 0 when two strings are identical. Otherwise it returns a numerical value which is the different in ASCII values of the first mismatching character
of the strings being compared.
char city[20] = “Madras”;
char town[20] = “Mangalore”;
strcmp(city, town);
=>  This will return an integer value „- 10? which is the difference in the ASCII values of the first mismatching letters „D? and „N?
* Note that the integer value obtained as the difference may be assigned to an integer variable as follows:
int n;
n = strcmp(city, town);

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved