C language: Simulate Implementation of strncmp

Source: Internet
Author: User

C language: Simulate Implementation of strncmp
The strncmp () function is used to simulate the implementation of the string comparison function. You can view the blog http://41040184.blog.51cto.com/41030184/1714512. Return Value: if the first n characters of str1 and str2 are the same, 0 is returned. If arr1 is greater than arr2, a value greater than 0 is returned. If arr1 is less than arr2, returns a value smaller than 0. [Parameter] str1 and str2 are the two strings to be compared, and n is the number of characters to be compared (which cannot be done by the strcmp function ). The len I set here is the length of arr1 .,

# Define _ CRT_SECURE_NO_WARNINGS 1 # include <stdio. h> # include <stdlib. h> # include <assert. h> int my_strcmp (const char * dest, const char * src, int len) {assert (dest); assert (src); while (len --) {if (* dest = * src) {dest ++; src ++ ;}} if (len = 0) return 0; else return (* dest-'\ 0')-(* src-' \ 0');} int main () {char arr1 [] = "abcdeff "; char arr2 [] = "abcd"; int len = sizeof (arr1)/sizeof (arr1 [0]); int ret = my_strcmp (arr1, arr2, len ); printf ("% d", ret); system ("pause"); return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.