String Comparison in C

Level 3121 keystrokes

Uses strcmp to compare two strings for equality.

C code

// Two separate arrays with the same text
1char s1[] = "apple";
2char s2[] = "apple";
// s1 == s2 would compare addresses; strcmp compares the characters and returns 0 when they are equal
3if (strcmp(s1, s2) == 0) {
// Both contain "apple", so this line runs
4 printf("Equal\n");
5} else {
// A nonzero result (negative or positive) means the strings differ
6 printf("Not equal\n");
7}

Output

Equal
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • "× 8
  • ;× 4
  • (× 4
  • )× 4
  • =× 4
  • [× 2
  • ]× 2
  • {× 2
  • }× 2
  • \× 2

More C examples at level 3

← C typing practice