STANDARD LIBRARY STRING HANDLING FUNCTIONS :

STANDARD LIBRARY STRING HANDLING FUNCTIONS :

There are  a large number of library functions available in C that allow to operate on strings. Using these functions, the string can be compared, copied or concatenated (i.e. combined one after another). Other library functions permit operations on individual character within strings. Some of commonly available library functions used for string manipulation in C  are listed  . They all are included in a header file string.h  .


strlen( s) :

The strlen(s) function takes one argument s which can be a string constant or a string variable. It returns the length of the string stored in the array s i.e. number of characters in the string excluding the null character('\0'). If string is empty, it returns zero . For example : if char str[ ]="gopal" ;, the statement,
  z=strlen(s);
return 5 which will be assigned to z.

To calculate the length of the string using library function strlen( ) ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[51];
int z;
clrscr();
printf("Enter a string please(<=50 characters) ;");
gets(str);
z=strlen(str);
printf("Length of string : %d" ,z);
getch();
}

OUTPUT:
Enter a string please (<=50 characters ) : This is a program
Length of string : 17

strcpy(s1, s2) :

The strcpy ( ) function takes two arguments of which first argument s1 is a string variable and second argument s2 can be a string variable or string constant. It copies the contents or string s2 including the null character('\0') replacing what was previously stored in the string s1. It takes following form,
   strcpy(s1, s2);
for example , consider the following string,
   char s1[ ]="Hello";
On executing the statement,
  strcpy(s1, "Hello everybody");
we get of string s1 changed to "Hello everybody" .

To copy a string to another using library function strcpy() ?
#include<stdio.h>
#include<string.h>
main()
{
char str[ ] = "Hello" ;
clrscr();
printf("Original string = %s " , str);
strcpy(str, "Hello Everybody");
printf("\nString after copying :%s" , str);
getch();
}

OUTPUT:
Original string = Hello
String aftyer copying : Hello Everybody


strcmp(s1,s2) :

The strcmp ( ) function compares the contents of string s1 with the contents of string s2 by compairing successive corresponding characters, starting with the first character in each string. This process continues until either the corresponding characters formed to be different or the last character in one or both string is reached.
The syntax is as follows,
   strcmp(s1,s2)
Example:
A program to comapre two strings using library function strcmp( ) .
#include<stdio.h>
#include<string.h>
main ( )
{
char s1[51], s2[51];
clrscr();
printf("Enter the first string1 (characters<=50) : ");
gets(s1);
printf("Enter the secong string2(characters<=50) ; ");
gets(s2);
if(strcmp(s1,s2)==0)
printf("strings '%s' and '%s'  are equal", s1,s2);
else if(strcmp(s1,s2)>0)
printf("string '%s' is greater than '%s' ", s1,s2);
else
printf("string '%s' is less than '%s' " ,s1,s2);
getch();
}

OUTPUT:
Enter the first string1(characters<=50) : Landslides
Enter the second string2(characters<=50) : Landscape
string 'Landslides' is greater than 'landscape' 


strcpi(s1,s2) :

The strcpi( ) function is similar to the strcmp( ) function except that it ignores case difference between two strings. Its syntax is as follows,
   strcpi(s1,s2)
So considering the strings as in previous example.
  char s1[ ] "hello" ;
  char s2[ ] "HELLo";
On execting the statement,
  strcpi(s1,s2);    /*will return 0, as it ignores the case diff. */

strcat(s1,s2) :

The strcat( ) function concatenates two strings together. It takes two strings i.e. character arrays as arguments of which first argument s1 is a string variable and the second argument s2 can be a string variable or string constant. This function appends a copy of s2 to the end of s1. The first character of s2 overwrites the terminating null character ('\0') of s1 . The length of the resulting string is strlen(s1) + strlen(s2). In addition to appending string s2 to string s2 this function also returns s1. Its syntax is as follows,
  strcat(s1,s2)
For example,
A program to concatenate two strings using library function strcat ( ) ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str1[20] = "Home";
char str2[ ]= "town";
clrscr();
printf("string before concatenation = %s" , str1);
strcat(str1,str2);
printf("string after concatenation = %s ", str2);
getch();
}

OUTPUT:
string before concatenation = Home 
string after concatenation = Home town

strlwr (s) :

The strlwr ( ) function converts the uppercase letters in a given string to lowercase. It takes one argument s which is a string variable. It takes the following form,
  strlwr(s)
After converting the string into lowercase, it stores the result back in string s and returns string s. Consider an example where contents of string variable s is "WELCOME" then the function call strlwr(s); result in value "welcome" being assigned to s.

A program to convert a given string into lowercase without using built-in functions.
#include<stdio.h>
#include<conio.h>
void str_lowercase (char [ ]);
main()
{
char str[81];
clrscr();
printf("Enter a string you want in lowercase(characters<80) =");
gets(str);
str_lowercase("string in lowercase : %s" , str);
getch();
}
void str_lowercase(char s[ ])
{
int i;
for (i=0; s[i] != '\0' i++)
if(s[i]>=65 && s[i]<=90)
s[i]+=32;

OUTPUT:
Enter a string you want in lowercase<characters<80) = WELCOME HOME 
string in lowercase : welcome home


strupr(s) :

The strupr() function is just the reverse of strlwr () function. It converts the lowercase latter in a given string to upercase. It takes one argument s which is a string variable. Its takes the following form,
   strupr()
  Consider an example where the contents of a string variable s is "welcome" then function call  strupr(s) ; result in value "WELCOME" being assigned to s.

strrev(s)  :

The strrev( ) function changes all characters in a string s to reverse order, except the terminating null character. It return pointer to reversed string. So on using this function on string "welcome" we get "emoclew" .

Program to reverse a given string using library function strrev( ).
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[80];
clrscr( );
printf("\nEnter a string please : ");
gets(a);
strrev(a);
printf("\nString after reversing : %s" ,a );
getch();
}

OUTPUT:
Enter a string please : welcome
String after reversing : emoclew 
    

THANKS

No comments

Powered by Blogger.