C program to check whether two strings are anagram or not.

Posted by Mangesh on March 21, 2018

/* Program in C to check whether two string are anagram or not. */

Description :

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word anagram can be rearranged into 'dad' and 'add'.

Program :

#include<stdio.h>
#include<conio.h>
#include<string.h>
int anagram (char *a, char *b) {
    int count [128], j;
    for (j = 0;  j < 128; j++)
      count[j] = 0;
    j = 0;
    while (a[j] && b[j]) {
	  count[a[j]]++;
	  count[b[j++]]--;
    }
    for (j = 0; j < 128; j++) 
	  if (count [j])
	   return 0;
    return 1;
}
int main()
{
  char a[100], b[100];
  printf("Enter the first string\n");
  scanf("%s",&a);
  printf("Enter the second string\n");
  scanf("%s",&b);
  if(anagram(a,b))
    printf("String is anagram.");
  else
    printf("String is not anagram.");
  getch();
}

Output :

C program for anagram

Written with from Mangesh.

Related Post
1 C program for naive string matching algorithm.
2 C program to check whether the given string is palindrome or not.
3 C program to concatenate two string.
4 C program to check whether two strings are anagram or not.
5 C program to reverse the string.
6 C program to replace a character in a string.
7 C program to compare two string.
8 C program to compare two string using strcmp function.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.