Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <assert.h>
  5.  
  6. typedef struct {
  7. int length;
  8. int *letterFreq;
  9. char *string;
  10. } tab;
  11.  
  12. void *safeMalloc(int size) {
  13. void *ptr = malloc(size);
  14. if (ptr == NULL) {
  15. printf("\nError: memory allocation failed....abort\n");
  16. exit(-1);
  17. }
  18. return ptr;
  19. }
  20.  
  21. void storeSentences(int n, tab arr[]){
  22. for(int i = 0; i < n; i++) { //this loops through the sentences in the table//
  23. arr[i].length = 0;
  24. arr[i].letterFreq = calloc(26, sizeof(int));
  25. arr[i].string = calloc(50, sizeof(char));
  26. int check = 1;
  27. while(check) { //this while loop should get each char from the input sentence//
  28. char c = getchar();
  29. if(c == '.') {
  30. check = 0;
  31. }
  32. if(c >= 'a' && c <= 'z'){
  33. arr[i].letterFreq[c-'a']++;
  34. arr[i].length++;
  35. arr[i].string[arr[i].length] = c;
  36. }
  37. }
  38. }
  39. }
  40.  
  41. void compareSentences(int a, tab test[], int b, tab table[]) {
  42. for(int i = 0; i < 26; i++) {
  43. if(test[a].letterFreq[i] != table[b].letterFreq[i]) {
  44. return;
  45. }
  46. }
  47. printf("%d ", b+1);
  48. printf("(");
  49. for(int i=0; i<table[b].length; i++){
  50. printf("%c", table[b].string[i]);
  51. }
  52. printf(") ");
  53.  
  54.  
  55.  
  56. }
  57.  
  58. void testSentence(int m, int n, tab test[], tab table[]) {
  59. for(int i = 0; i < m; i++) { //This loops through the test list of sentences...//
  60. for(int j = 0; j < n; j++) { //...and compares it to all the first sentences//
  61. if(test[i].length == table[j].length) {
  62. compareSentences(i, test, j, table);
  63. }
  64. }
  65. printf("\n");
  66. }
  67. }
  68.  
  69. int main(int argc, char *argv[]) {
  70. int n;
  71. int m;
  72. scanf("%d", &n); //scan the n//
  73. tab* table = safeMalloc(n*sizeof(tab));
  74. //Get n sentences for the table//
  75. storeSentences(n, table);
  76. scanf("%d", &m);
  77. tab* test = safeMalloc(m*sizeof(tab));
  78. //Get m sentences for the test //
  79. storeSentences(m, test);
  80. //After storing the senteces in an array of structs, start by comparing them//
  81. testSentence(m, n, test, table);
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement