Wednesday 29 January 2014

Explain stricmp(),strcmp(),strncmp() function.

stricmp() function-This function compares between two strings,it doesn't discriminates between lower and upper case.
syntax
stricmp(source string,destination string)

Write a program to compare between two strings.
#include<stdio.h>
#include<conio.h>
main()
{
    char s1[10],s2[10];
    int diff;
    clrscr();
    printf("Enter the source string:");
    gets(s1);
    printf("Enter the destination string:");
    gets(s2);
    diff=stricmp(s2,s1);
    if(diff==0)
        {
            printf("Two strings are identical");
        }
        else
        {
            printf("Two strings are not identical");
        }
}
Output
Enter the source string:HELLO
Enter the destination string:hello
Two strings are identical


strcmp() function-This function compares between two strings,it does discriminates between lower and upper case.
syntax
stricmp(source string,destination string)

Write a program to compare between two strings.
#include<stdio.h>
#include<conio.h>
main()
{
    char s1[10],s2[10];
    int diff;
    clrscr();
    printf("Enter the source string:");
    gets(s1);
    printf("Enter the destination string:");
    gets(s2);
    diff=strcmp(s2,s1);
    if(diff==0)
        {
            printf("Two strings are identical");
        }
        else
        {
            printf("Two strings are not identical");
        }
}
Output
Enter the source string:HELLO
Enter the destination string:hello
Two strings are not identical

strncmp() function-This function compares between two strings upto specified length.
syntax
stricmp(source string,destination string,n)

Write a program to compare between two strings upto specified length.
#include<stdio.h>
#include<conio.h>
main()
{
    char s1[10],s2[10];
    int n,diff;
    clrscr();
    printf("Enter the source string:");
    gets(s1);
    printf("Enter the destination string:");
    gets(s2);
    printf("Enter length upto which comparison is to be made");
    scanf("%d",&n);
    diff=strcmp(s2,s1,n);
    if(diff==0)
        {
            printf("Two strings are identical upto %d characters");
        }
        else
        {
            printf("Two strings are not identical");
        }
}
Output
Enter the source string:HELLO
Enter the destination string:hello
Two strings are not identical

No comments:

Post a Comment