January 22, 2013

Library String Function



string library functions banner in C language


strlen()

This function calculates the length of the string it means it can count the total number of character presents in the string which includes alphabets, numbers and all special characters.

/* Program to show use of strlen( ) function */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     int num;
     char para[200];
     printf("\n enter any of your string");
     gets(para);
     num=strlen(para);
     printf("\n In string %s there are %d words",para,num);
     getch();
     }
output of program to show use of strlen string function in C language


/* Program to enter 2 strings and compare which string is longer */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     int num1,num2;
     char par1[200],par2[200];
     printf("\n enter 1st string");
     gets(par1);
     printf("\n enter 2nd string");
     gets(par2);
     num1=strlen(par1);
     num2=strlen(par2);
     if(num1>num2)
     printf("\n 1st string is bigger than 2nd string");
     else
     printf("\n 2nd string is bigger than 1st string");
     getch();
     }
output of program to show use of strlen string function and compare which is larger in C language


strcat()

This function can join two strings. It actually joins the second string at the end of the first string. Hence, by this function act as a junction for two strings.

/* Program to show use of strcat( ) function */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     char st1[200],st2[200];
     printf("\n enter 1st string");
     gets(st1);
     printf("\n enter 2nd string");
     gets(st2);
     strcat(st1,st2);
     printf("\n");
     puts(st1);
     getch();
     }
output of program to show use of strcat string function and catinate both in C language


strcpy()

This function will copy one string over the another string. It means second string will overwrite over the first string and value content of first string will be changed by the second string.

/*Program to show use of strcpy( ) function */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     char st1[200],st2[200];
     printf("\n enter 1st string");
     gets(st1);
     printf("\n enter 2nd string");
     gets(st2);
     printf("\n 1st string is copied by 2nd: ");
     strcpy(st1,st2);
     puts(st1);
     getch();
     }

/* Program to enter two strings by the user, join these two strings, now compare between larger string and the 2nd string among these larger is replaced by the smaller*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     int a,b;
     char st1[200],st2[200];
     printf("\n enter 1st string");
     gets(st1);
     printf("\n enter 2nd string");
     gets(st2);
     strcat(st1,st2);
     puts(st1);
     a=strlen(st1);
     b=strlen(st2);
     if(a>b)
     {
            strcpy(st1,st2);
            puts(st1);
            }
     else
     {
         strcpy(st2,st1);
         puts(st1);
         }
     getch();
     }
output of program to use strcmp and strcpy string function and show in C language

strcmp()

This function compares two strings and if two strings are equal then it gives result as 0 but if two strings differ then it gives a non-negative number.

/*Program to show use of strcmp( ) function */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     char st1[200],st2[200];
     printf("\n enter 1st string");
     gets(st1);
     printf("\n enter 2nd string");
     gets(st2);
     if(strcmp(st1,st2)==0)
     printf("\n strings are identical");
     else
     printf("\n both strings are different");
     getch();
     }
output of program to compare bit by bit word in string using strcmp function in C language

strcmpi()

This function compares two string after ignoring the case of an alphabet it means it consider upper case alphabets and lower case alphabets as same and if both the string are same then it gives result as zero otherwise a non-negative number.

/*Program to show use of strcmpi( ) function */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
     char st1[200],st2[200];
     printf("\n enter 1st string");
     gets(st1);
     printf("\n enter 2nd string");
     gets(st2);
     if(strcmpi(st1,st2)==0)
     printf("\n strings are identical");
     else
     printf("\n both strings are different");
     getch();
     }
output of program to compare bit by bit word in string using strcmpi function in C language


No comments:

Post a Comment