fork download
  1. /*Write a program to take two strings s and t as inputs (assume all characters are lowercase). The task is to
  2. determine if s and t are valid anagrams, meaning they contain the same characters with the same frequencies.
  3. Print "Anagram" if they are, otherwise "Not Anagram".*/
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. int main() {
  8. char s[100], t[100];
  9. int freq[26] = {0};
  10. int i;
  11.  
  12. scanf("%s", s);
  13. scanf("%s", t);
  14.  
  15. if (strlen(s) != strlen(t)) {
  16. printf("Not Anagram");
  17. return 0;
  18. }
  19.  
  20. for (i = 0; s[i] != '\0'; i++) {
  21. freq[s[i] - 'a']++; // increase count for character in s
  22. freq[t[i] - 'a']--; // decrease count for character in t
  23. }
  24.  
  25. for (i = 0; i < 26; i++) {
  26. if (freq[i] != 0) {
  27. printf("Not Anagram");
  28. return 0;
  29. }
  30. }
  31.  
  32. printf("Anagram");
  33. return 0;
  34. }
  35.  
  36.  
Success #stdin #stdout 0s 5316KB
stdin
listen
silent
stdout
Anagram