Advertisement
Guest User

Untitled

a guest
May 29th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace RecusionAnagram
  8. {
  9. class Program
  10. {
  11. static string first;
  12. static string second;
  13. static bool anagrams = true;
  14.  
  15. static void Main(string[] args)
  16. {
  17. Console.WriteLine("Enter first string: ");
  18. first = Console.ReadLine();
  19.  
  20. Console.WriteLine("Enter second string: ");
  21. second = Console.ReadLine();
  22.  
  23. permute(first.ToCharArray(), 0, first.Length - 1);
  24.  
  25. if (anagrams)
  26. {
  27. Console.WriteLine("Strings are anagrams.");
  28. }
  29. else
  30. {
  31. Console.WriteLine("Strings are not anagrams.");
  32. }
  33. }
  34. static void permute(char[] a, int i, int n)
  35. {
  36.  
  37. int j;
  38. if (i == n)
  39. if (a.ToString() == second)
  40. {
  41. anagrams = true;
  42. }
  43. else
  44. {
  45. for (j = i; j <= n; j++)
  46. {
  47. char temp = a[i];
  48. a[i] = a[j];
  49. a[j] = temp;
  50. permute(a, i+1, n);
  51.  
  52. temp = a[i];
  53. a[i] = a[j];
  54. a[j] = temp;
  55. }
  56. }
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement