Char Comparision in C – GeeksforGeeks


Improve Article

Save Article

Like Article

Improve Article

Save Article

Char is a keyword used for representing characters in C. Character size in C is 1 byte.

There are two methods to compare characters in C and these are:

  • Using ASCII values
  • Using strcmp( ) .

1. Using ASCII values to compare characters

The first method is pretty simple, we all know that each character can be in uppercase or lowercase and has a different ASCII value. So, we can directly compare their ASCII values to see which character is greater or lesser than the other.

  • if diff between first character and second character is >0 -> First character is Greater between the two.
  • if diff between first character and second character is <0 -> Second character is Greater between the two.
  • if diff between first character and second character is =0 -> Both Characters are equal

Example:

C

#include <stdio.h>

  

void compare(char a, char b)

{

    if (a == b)

        printf("Both are equal %c and %c\n", a, b);

    else

        printf("%c and %c are not equal\n", a, b);

}

  

int main()

{

  

    

    char x = 'g';

    char y = 'G';

    char z = 'g';

  

    

    compare(x, y);

    compare(y, z);

    compare(x, z);

  

    return 0;

}

Output
g and G are not equal
G and g are not equal
Both are equal g and g

2. Using strcmp( ) to compare characters

The second method that can be used is the strcmp() function defined in the string header file in the C library. The strcmp() function compares two strings character by character. The first character in both the strings is compared followed by the subsequent characters. 

Return type:

if diff between first unmatched character and second unmatched character is >0 -> First character is Greater between the two.
if diff between first unmatched character and second unmatched character is <0 -> Second character is Greater between the two.
if diff between first character and second character is =0 -> Both Characters are equal

Syntax:

int strcmp (const char* String1, const char* String2);

Example: 

C

#include <stdio.h>

#include <string.h>

  

int main()

{

  

    

    char a[] = "g";

    char b[] = "G";

    char c[] = "g";

    int output;

  

    output = strcmp(a, b);

    printf("The Comparison value between %s and %s is %d\n",

           a, b, output);

  

    output = strcmp(b, c);

    printf("The Comparison value between %s and %s is %d\n",

           b, c, output);

  

    output = strcmp(a, c);

    printf("The Comparison value between %s and %s is %d\n",

           a, c, output);

  

    return 0;

}

Output
The Comparison value between g and G is 32
The Comparison value between G and g is -32
The Comparison value between g and g is 0


Improve Article

Save Article

Like Article

Improve Article

Save Article

Char is a keyword used for representing characters in C. Character size in C is 1 byte.

There are two methods to compare characters in C and these are:

  • Using ASCII values
  • Using strcmp( ) .

1. Using ASCII values to compare characters

The first method is pretty simple, we all know that each character can be in uppercase or lowercase and has a different ASCII value. So, we can directly compare their ASCII values to see which character is greater or lesser than the other.

  • if diff between first character and second character is >0 -> First character is Greater between the two.
  • if diff between first character and second character is <0 -> Second character is Greater between the two.
  • if diff between first character and second character is =0 -> Both Characters are equal

Example:

C

#include <stdio.h>

  

void compare(char a, char b)

{

    if (a == b)

        printf("Both are equal %c and %c\n", a, b);

    else

        printf("%c and %c are not equal\n", a, b);

}

  

int main()

{

  

    

    char x = 'g';

    char y = 'G';

    char z = 'g';

  

    

    compare(x, y);

    compare(y, z);

    compare(x, z);

  

    return 0;

}

Output
g and G are not equal
G and g are not equal
Both are equal g and g

2. Using strcmp( ) to compare characters

The second method that can be used is the strcmp() function defined in the string header file in the C library. The strcmp() function compares two strings character by character. The first character in both the strings is compared followed by the subsequent characters. 

Return type:

if diff between first unmatched character and second unmatched character is >0 -> First character is Greater between the two.
if diff between first unmatched character and second unmatched character is <0 -> Second character is Greater between the two.
if diff between first character and second character is =0 -> Both Characters are equal

Syntax:

int strcmp (const char* String1, const char* String2);

Example: 

C

#include <stdio.h>

#include <string.h>

  

int main()

{

  

    

    char a[] = "g";

    char b[] = "G";

    char c[] = "g";

    int output;

  

    output = strcmp(a, b);

    printf("The Comparison value between %s and %s is %d\n",

           a, b, output);

  

    output = strcmp(b, c);

    printf("The Comparison value between %s and %s is %d\n",

           b, c, output);

  

    output = strcmp(a, c);

    printf("The Comparison value between %s and %s is %d\n",

           a, c, output);

  

    return 0;

}

Output
The Comparison value between g and G is 32
The Comparison value between G and g is -32
The Comparison value between g and g is 0

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@technoblender.com. The content will be deleted within 24 hours.
charComparisionGeeksForGeeksLatestTechnoblenderTechnology
Comments (0)
Add Comment