Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <assert.h>
- typedef struct {
- int length;
- int *letterFreq;
- char *string;
- } tab;
- void *safeMalloc(int size) {
- void *ptr = malloc(size);
- if (ptr == NULL) {
- printf("\nError: memory allocation failed....abort\n");
- exit(-1);
- }
- return ptr;
- }
- void storeSentences(int n, tab arr[]){
- for(int i = 0; i < n; i++) { //this loops through the sentences in the table//
- arr[i].length = 0;
- arr[i].letterFreq = calloc(26, sizeof(int));
- arr[i].string = calloc(50, sizeof(char));
- int check = 1;
- while(check) { //this while loop should get each char from the input sentence//
- char c = getchar();
- if(c == '.') {
- check = 0;
- }
- if(c >= 'a' && c <= 'z'){
- arr[i].letterFreq[c-'a']++;
- arr[i].length++;
- arr[i].string[arr[i].length] = c;
- }
- }
- }
- }
- void compareSentences(int a, tab test[], int b, tab table[]) {
- for(int i = 0; i < 26; i++) {
- if(test[a].letterFreq[i] != table[b].letterFreq[i]) {
- return;
- }
- }
- printf("%d ", b+1);
- printf("(");
- for(int i=0; i<table[b].length; i++){
- printf("%c", table[b].string[i]);
- }
- printf(") ");
- }
- void testSentence(int m, int n, tab test[], tab table[]) {
- for(int i = 0; i < m; i++) { //This loops through the test list of sentences...//
- for(int j = 0; j < n; j++) { //...and compares it to all the first sentences//
- if(test[i].length == table[j].length) {
- compareSentences(i, test, j, table);
- }
- }
- printf("\n");
- }
- }
- int main(int argc, char *argv[]) {
- int n;
- int m;
- scanf("%d", &n); //scan the n//
- tab* table = safeMalloc(n*sizeof(tab));
- //Get n sentences for the table//
- storeSentences(n, table);
- scanf("%d", &m);
- tab* test = safeMalloc(m*sizeof(tab));
- //Get m sentences for the test //
- storeSentences(m, test);
- //After storing the senteces in an array of structs, start by comparing them//
- testSentence(m, n, test, table);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement