beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

C strcoll() Function – C tutorial

By Chaitanya Singh | Filed Under: C library functions

The strcoll() function is similar to strcmp() function, it compares two strings and returns an integer number based on the result of comparison.

C strcoll() declaration

int strcoll(const char *str1, const char *str2)

str1 – First String
str2 – Second String

Return value of strcoll()

  • > 0 if the ASCII value of first unmatched character in string str1 is greater than str2.
  • < 0 if the ASCII value of first unmatched character in string str1 is less than str2.
  • =0 if both strings are equal

C strcoll() function example

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

int main () {
   char str1[20];
   char str2[20];
   int result;

   strcpy(str1, "HELLO");
   strcpy(str2, "hello world!");

   result = strcoll(str1, str2);

   if(result > 0) { 
      printf("ASCII value of first unmatched character of str1 is greater than str2");
   } else if(result < 0) {
      printf("ASCII value of first unmatched character of str1 is less than str2");
   } else {
      printf("Both the strings str1 and str2 are equal");
   }

   return 0;
}

Output:

ASCII value of first unmatched character of str1 is less than str2

In this example we are comparing two strings that are different. The strcoll() function is case sensitive. The ASCII value of ‘H’ is less than ‘h’ which is why this function returns negative value.
If we reverse the arguments in the strcoll() function, lets say strcoll(str2, str1) then the function would return a positive value.

Related Posts:

  1. C – strcat() function
  2. C – strchr() function
  3. C – strncat() function
  4. C – strncmp() function

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • Turbo C++ installation
  • First C Program
  • C - Keywords
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap