Advertisement
monyca98

anagrame

Mar 1st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int verifAparitii(char s[], char c)
  4. {
  5.     int aparitii = 0;
  6.     for (int i = 0; i < strlen(s); i++)
  7.         if (s[i] == c)
  8.             aparitii++;
  9.     return aparitii;
  10.  
  11. }
  12. bool anagrame(char s1[], char s2[])
  13. {
  14.  
  15.     int n = strlen(s1);
  16.     int m = strlen(s2);
  17.     for (int i = 0; i < n; i++)
  18.         if(s1[i]!=' ')
  19.         if (verifAparitii(s1, s1[i]) != verifAparitii(s2, s1[i]))
  20.             return false;
  21.  
  22.     return true;
  23.  
  24. }
  25. int main()
  26. {
  27.     char s1[100], s2[100];
  28.     cin.get(s1, 99);
  29.     cin.get();
  30.     cin.get(s2, 99);
  31.     if (anagrame(s1, s2))
  32.         cout << "da";
  33.     else
  34.         cout << "nu";
  35.     cout << endl << endl;
  36.     system("pause");
  37.     return 0;
  38. }
  39.  
  40. -------alta rezolvare---
  41.  
  42.  
  43. #include<iostream>
  44. #include<string>
  45. using namespace std;
  46. void citireSiruri(char s1[], char s2[])
  47. {
  48.     cin.get(s1, 29);
  49.     cin.get();
  50.     cin.get(s2, 29);
  51. }
  52. bool verifLungimi(char s1[], char s2[])
  53. {
  54.     int count1 = 0, count2 = 0;
  55.     for (int i = 0; i < strlen(s1); i++)
  56.         if (s1[i] != ' ')
  57.             count1++;
  58.     for (int i = 0; i < strlen(s2); i++)
  59.         if (s2[i] != ' ')
  60.             count2++;
  61.     if (count1 != count2)
  62.         return false;
  63.     return true;
  64. }
  65. bool anagrame(char s1[], char s2[])
  66. {
  67.     if (!verifLungimi(s1, s2))
  68.         return false;
  69.     int aparitii[26] = { 0 };
  70.     for(int i=0;i<strlen(s1);i++)
  71.         if (s1[i] != ' ')
  72.         {
  73.             int a = s1[i];
  74.             aparitii[a - 97]++;
  75.         }
  76.     for (int i = 0; i<strlen(s2); i++)
  77.         if (s2[i] != ' ')
  78.         {
  79.             int a = s2[i];
  80.             aparitii[a - 97]--;
  81.         }
  82.     for (int i = 0; i < 26; i++)
  83.         if (aparitii[i] != 0)
  84.             return false;
  85.     return true;
  86. }
  87. int main()
  88. {
  89.     char s1[50], s2[50];
  90.     citireSiruri(s1, s2);
  91.     if (anagrame(s1, s2))
  92.         cout << "sunt anagrame";
  93.     else
  94.         cout << "nu sunt anagrame";
  95.     cout << endl << endl;
  96.     system("pause");
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement