Pre-define String function in C

strncmp string function in C programming language

strncmp string function in C programming language

In this tutorial, We will learn  about strncmp string function in C programming language

Description

strncmp() – In the c programming language, the strncmp() function returns a negative , Zero and positive integer depending on whether the first n character of the object pointed by str1 is less than, equal to, or greater than the first n character of the object pointed to by str2.(compare str1 and str2)

Declaration

The syntax for strncmp() function is given below, in C Language is:

int strncmp(const char *str1, const char *str2,size of n);

Parameters

str1 – this is the first string to be compared

str2 – this is the second string to be compared

n- the maximum number of character to be compared

 

Returns

the strncmp function returns an integer value as follows

when strncmp() returns value<0 then it denotes str1 is less than str2

when strncmp() returns value>0 then it denotes str1 is grater than than str2

when strncmp() returns value=0 then it denotes str1 is equal to str2

Required Header

the required header for strncmp() String function in C Programming language

#include<string.h>

strncmp string function

Example

Program 1

If both strings are same

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char str1[]="Hello";
    char str2[]="Hello";
    int check=strncmp(str1,str2,4);
    if(check<0){
        printf("str1 is less than to str2\n");
    }
    else if(check>0){
        printf("str1 is greater than to str2\n");
    }
    else{
        printf("str1 is equal to str2\n");
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

str1 is equal to str2

 

If both strings are not the same

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char str1[]="Hello";
    char str2[]="hello";
    int check=strncmp(str1,str2,4);
    if(check<0){
        printf("str1 is less than to str2\n");
    }
    else if(check>0){
        printf("str1 is greater than to str2\n");
    }
    else{
        printf("str1 is equal to str2\n");
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

str1 is less than to str2

 

 

There are other C programming language functions that are  similar to this function

strcat() function in C language

strcpy() function in C language

strncpy() function in C language

strset() function in C language

strlwr() function in C language

strdub() function in C language

strupr() function in C language

strlen()  function in C language

strnset() function in C language

strchr()  function in C language

strrchr()  function in C language

strtok()  function in C language

strrev()  function in C language

strcmp()  function in C language

strnset()  function in C language

 

Suggested for you

String function in C programming Language

Function in C programming language

Arithmetic function in C language

User defined function in C programming language

function in python language

recursive Function in C language

recursive Function in C++ language

recursive Function in Python language

 

 

 

strchr string function in C programming language
strrchr string function in C programming Language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

25 minutes ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago