C Program to find two given strings are anagram or not

Write a program to find whether the 2 given strings are anagrams or not.

Anagrams are words or phrases made by mixing up the letters of other words or phrases,

 Input and Output Format:

Input consists of 2 string. Assume that all characters in the string are lowercase letters or spaces and the maximum length of the string is 100.

Refer sample input and output for formatting specifications.

All text in bold corresponds to input and the rest corresponds to output.

Sample Input 

Enter the first string


Enter the second string



 codeatglance and blog are not anagrams

Sample Input and Output 2:

Enter the first string

the eyes

Enter the second string

they see

the eyes and they see are anagrams

C Program to find two given strings are an anagram or not

#include < stdio.h >

  int function_anagram(char[], char[]);

int main() {

  char first_array[50], second_array[50];

  int flag;

  printf("Enter the first string\n");


  printf("Enter the second string\n");


  flag = function_anagram(first_array, second_array);

  if (flag == 1)

    printf("" % s " and " % s " are anagrams.\n", first_array, second_array);


    printf("" % s " and " % s " are not anagrams.\n", first_array, second_array);

  return 0;


int function_anagram(char first_array[], char second_array[]) {

  int num1[26] = {


  }, num2[26] = {


  }, i = 0;

  while (first_array[i] != '\0') {

    num1[first_array[i] - 'a']++;



  i = 0;

  while (second_array[i] != '\0') {

    num2[second_array[i] - 'a']++;



  for (i = 0; i < 26; i++) {

    if (num1[i] != num2[i])

      return 0;


  return 1;


Leave a Comment

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