Write a C program that uses the strncmp library function to compare two strings

WBOY
Release: 2023-09-09 13:17:01
forward
659 people have browsed it

Strncmp is a predefined library function that exists in the string.h file. It is used to compare two strings and display which string is larger.

strcmp function (string comparison)

This function compares two strings. It returns the ASCII difference of the first non-matching character in the two strings.

Syntax

int strcmp (string1, string2);
Copy after login
  • If the difference is equal to zero, then string1 = string2.

  • If the difference is positive, string1> string2.

  • If the difference is negative, string1

Example

Write a C program that uses the strncmp library function to compare two strings

strncmp function

This function is used to compare the first n characters of two strings .

Syntax

strncmp ( string1, string2,2)
Copy after login

Program

#include #include void main(){ //Declaring two strings// char string1[25],string2[25]; int value; //Reading string 1 and String 2// printf("Enter String 1: "); gets(string1); printf("Enter String 2: "); gets(string2); //Comparing using library function// value = strncmp(string1,string2,4); //If conditions// if(value==0){ printf("%s is same as %s",string1,string2); } else if(value>0) { printf("%s is greater than %s",string1,string2); } else { printf("%s is less than %s",string1,string2); } }
Copy after login

Output

Enter String 1: Tutorials Enter String 2: Point Tutorials is greater than Point
Copy after login

The above is the detailed content of Write a C program that uses the strncmp library function to compare two strings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!