Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Functions strcmp and strncmp

September 13, 2013 Posted by: GPA Views: 753 0 comments

Problem

What is the difference between strcmp and strncmp in C programming?

Solution

In this article, you will know  difference between functions strcmp and strncmp and we'll show simple program using them. You need include header file <string.h> for this program.

Declaration:

int strcmp( const char *firstString, const char *secondString );

This function compares its first argument firstString into its second argument secondString character by character. If these two strings are equal function return 1, if the first string is less than the second string then function return -1, else if the first string is greater than the second then it returns positive value.

char *strncmp( char * firstString , const char * secondString , size_t n );

This function compares up to n characters of the string firstString with the string secondString.

Function strncmp does not compare characters following a null character in a string. The program returns the integer value by each function call. 

Example:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>//include this header for functions strcmp and strncmp
//main function
int main(int argc, char *argv[]){
printf("globalproganswer.com\n");
char firstStringExample[50]="Welcome to globalproganswer.com";//first argument
char secondStringExample[50] = "Welcome to globalproganswer.com";//second argument
char thirdStringExample[50]="Welcome";
printf("\nFirst string is: %s",firstStringExample);
printf("\nSecond string is: %s",secondStringExample);
printf("\nThird string is: %s\n",thirdStringExample);
printf("\nResult of strcmp(First string,Second string) is:");
//0 - strings are equal
//-1 - the first string is less than the second string
//1 - the first string is greater than the second string
if(strcmp(firstStringExample,secondStringExample)==0){//call function strcmp
printf("\nFirst string and second String are equal.\n");
}
if(strcmp(firstStringExample,secondStringExample)==-1){
printf("\nThe first string is less than the second string.\n");
}
if(strcmp(firstStringExample,secondStringExample)==1){
printf("\nThe first string is greater than the second string.\n");
}
//compare firstStringExample and thirdStringExample variables
printf("\nResult of strcmp(First string,Third string) is:");
if(strcmp(firstStringExample,thirdStringExample)==0){//call function strcmp
printf("\nFirst string and second String are equal.\n");
}
if(strcmp(firstStringExample,thirdStringExample)==-1){
printf("\nThe first string is less than the second string.\n");
}
if(strcmp(firstStringExample,thirdStringExample)==1){
printf("\nThe first string is greater than the second string.\n");
}
printf("\nResult of strncmp(First string,Third string) is:");
//0 - strings are equal
//<0 - the first string is less than the second string
//>0 - the first string is greater than the second string
if(strncmp(firstStringExample,thirdStringExample,7)==0){//call function strncmp
printf("\nFirst string and second String are equal.");
}
if(strncmp(firstStringExample,thirdStringExample,7)<0){
printf("\nThe first string is less than the second string.");
}
if(strncmp(firstStringExample,thirdStringExample,7)>0){
printf("\nThe first string is greater than the second string.");
}
if(strncmp(firstStringExample,thirdStringExample,8)==0){//call function strncmp
printf("\nFirst string and second String are equal.");
}
if(strncmp(firstStringExample,thirdStringExample,8)<0){
printf("\nThe first string is less than the second string.");
}
if(strncmp(firstStringExample,thirdStringExample,8)>0){
printf("\nThe first string is greater than the second string.");
}

printf("\n\n\n");
system("PAUSE");//delay
return 0;
}

Output

Functions strcmp and strncmp

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe