Exemple #1
0
int main() {
  double d = 5.0;

  print_words(3, "apple", "orange", "grape");

  /* implicit conversion */
  print_words(d, "Alice", "Bob", "Carol", "Dave", "Eva");

  return 0;
}
Exemple #2
0
void print_words(struct node ** ptree)
{
    if (*ptree != NULL)
    {
        print_words(&(*ptree)->left);
        printf("%4d : %s\n", (*ptree)->count, (*ptree)->word);
        free((*ptree)->word);
        print_words(&(*ptree)->right);
        free(*ptree);
    }
    return;
}
Exemple #3
0
/*
 * This function adds words in different orders and amounts to the AVL-tree. 
 * After adding, it validates the AVL-tree. Function prints the results to stdout. 
 */
void run_test() {

    node* root;
    FILE* out;
    root = NULL;
    int i;
    char* test_strings[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};

    if ((out = fopen("tree_test_result.txt", "w")) == NULL) {
        fprintf(stderr, "Cannot write to output file tree_test_result\n");
        exit(1);
    }
  
    /*Add words in increasing order*/
    printf("\nAdd words in increasing order:\n");
    for (i = 0; i < 11; i++) {
        root = add_word(root, test_strings[i]);
    }
    printf("Run validation\n");
    validate_tree(root);
    printf("Test passed\n");
    print_words(root, out);
    destroy(root);

    /*Add words in decreasing order*/
    root = NULL;
    printf("\nAdd words in decreasing order:\n");
    for (i = 10; i >= 0; i--) {
        root = add_word(root, test_strings[i]);
    }
    printf("Run validation\n");
    validate_tree(root);
    printf("Test passed\n");
    print_words(root, out);
    destroy(root);

    /*Add only one word*/
    root = NULL;
    printf("\nAdd single word:\n");
    root = add_word(root, test_strings[0]);
    printf("Run validation\n");
    validate_tree(root);
    printf("Test passed\n");
    print_words(root, out);
    destroy(root);
    fclose(out);
    
    exit(0);


}
Exemple #4
0
void print_command_list(command_t* commList, int length)
{

	int i;
	printf("printCommandList\n");
	printf("length: %d\n", length);
	//int k = commList[0].type == SIMPLE_COMMAND;
	//printf("k: %d\n", k);
	for(i = 0; i < length; i++)
	{
		printf("%d ", i);
		switch(commList[i]->type)
		{
			case AND_COMMAND: printf("AND_COMMAND\n"); break;
			case SEQUENCE_COMMAND: printf("SEQUENCE_COMMAND\n"); break;
			case OR_COMMAND: printf("OR_COMMAND\n"); break;
			case PIPE_COMMAND: printf("PIPE_COMMAND\n"); break;
			case SIMPLE_COMMAND: printf("SIMPLE_COMMAND\n"); //printf("commList[i].numWords: %d\n", commList[i]->numWords);
									 print_words(commList[i]->u.word, commList[i]->numWords); break;
				break;
			case OPEN_PAREN: printf("OPEN_PAREN\n"); break;
			case CLOSED_PAREN: printf("CLOSED_PAREN\n"); break;
			case LEFT_REDIRECT: printf("LEFT_REDIRECT\n"); break;
			case RIGHT_REDIRECT: printf("RIGHT_REDIRECT\n"); break;
			case NEW_LINE: printf("NEW_LINE\n"); break;
			case IGNORE_COMMAND: printf("IGNORE_COMMAND\n"); break;
			default: break;
		}

	}
	printf("\n");
}
Exemple #5
0
int main(int argc, char **argv) {
	if(argc == 1) {
		printf("please run this program with at least one argument.\n");
	}
	else{
		int i;
		char buffer[BUFLEN];
		char* saved_words[]= {"int","char","if","else","while","for","return",(char*)0};
		namelist file_words= make_namelist();
		for (i=1; i<argc; ++i){
		      FILE *fptr;
		      if ( (fptr = fopen(argv[i],"r")) == NULL)
			    printf ("file %s couldn't be opened\n",argv[i]);
		      else {
			    char word[BUFLEN];
			    int ich = 0;
			    int isaved = 0;
			    int inword = 0;
			    for(;;) {
				int ch = fgetc(fptr);
				if(feof(fptr))
				      break;
				if(inword) {
				      if(isalpha(ch)) {    // adding char to the word
					  word[ich] = ch;
					  ++ich;
				      } 
				      else{     // just finished reading a word
					  word[ich] = 0;
					  ich = 0;
					  inword = 0;
					  if (!find_word(saved_words,word)){
						add_name(file_words,word);
					  }
	
				      }
				}
		
				else {
				      if(isalpha(ch)) {        //for the first char of the word
					  ungetc(ch,fptr);
					  inword=1;
				      } 
				      if (ch == '\"'){
					  ch = fgetc(fptr);
					  while (ch != '\"')
						ch = fgetc(fptr);
				      }
				}
			    }
		      }
		}
		sort_words(file_words);
		print_words(file_words);
	}
	return 0;
}
Exemple #6
0
void dump_command (command_t com)
{
  printf("COMMAND_T at 0x%p --------------\n", com);

  switch (com->type)
  {
    case     AND_COMMAND:
      printf("type: AND\n");
      printf("status: %d\n", com->status);
      printf("input: %s\n", com->input);
      printf("output: %s\n", com->output);
      printf("commands at: 0x%p and 0x%p\n", com->u.command[0], com->u.command[1]);
      break;

    case     SEQUENCE_COMMAND:
      printf("type: SEQUENCE\n");
      printf("status: %d\n", com->status);
      printf("input: %s\n", com->input);
      printf("output: %s\n", com->output);
      printf("commands at: 0x%p and 0x%p\n", com->u.command[0], com->u.command[1]);
      break;

    case     OR_COMMAND:
      printf("type: OR\n");
      printf("status: %d\n", com->status);
      printf("input: %s\n", com->input);
      printf("output: %s\n", com->output);
      printf("commands at: 0x%p and 0x%p\n", com->u.command[0], com->u.command[1]);
      break;

    case     PIPE_COMMAND:
      printf("type: PIPE\n");
      printf("status: %d\n", com->status);
      printf("input: %s\n", com->input);
      printf("output: %s\n", com->output);
      printf("commands at: 0x%p and 0x%p\n", com->u.command[0], com->u.command[1]);
      break;

    case     SIMPLE_COMMAND:
      printf("type: SIMPLE\n");
      printf("status: %d\n", com->status);
      printf("input: %s\n", com->input);
      printf("output: %s\n", com->output);
      printf("words are:\n");
      print_words((char **) com->u.word);
      break;

    case     SUBSHELL_COMMAND:
      printf("type: SUBSHELL\n");
      printf("status: %d\n", com->status);
      printf("input: %s\n", com->input);
      printf("output: %s\n", com->output);
      printf("command at: 0x%p\n", com->u.subshell_command);
      break;
  }
  printf("\n");
}
void count_sort(int l)
{
	printf("\nFOR l = %d\n",l);
	node out_words[10000];
	int count[28];
	memset(count,0,sizeof(count));
	int i ,j, k;
	for( i = 0; i < 10000; i++)
	{
		out_words[i].str = malloc(100);
		out_words[i].dup_str = malloc(100);
		out_words[i].idx = malloc(50);
		out_words[i].num_idx = 0;
	}


	// count[0] = '0'
	// count[1] = 'a'
	for(i = 0; i < num_words; i++)
	{
		if( words[i].dup_str[l] == '0' )
		{
			count[0]++;  
		}
		else 
		{
			count[ words[i].dup_str[l] - 'a' + 1]++;  
		}
	}
	for( i = 0; i < 28; i++)
		printf("%d ",count[i]);
	puts("\n");
	for(i = 1; i < 28; i++)
		count[i] += count[i-1];
	for( i = 0; i < 28; i++)
		printf("%d ",count[i]);

	puts("\nceck");
	for(i = num_words-1; i >= 0; i--)
	{
		if( words[i].dup_str[l] == '0' )
		{
			out_words[ count[0] - 1] = words[i];
			count[0]--;
		}
		else 
		{
			out_words[ count[ words[i].dup_str[l] - 'a' + 1] - 1] = words[i];
			count[words[i].dup_str[l]-'a'+1]--;
		}
	}
	for( i = 0; i < num_words; i++)
		words[i] = out_words[i];
	print_words();
}
Exemple #8
0
int main()
{
    char word[MAX_WORD_LEN];
    int c;
    struct node * tree = NULL;

    while((c=getword(word, MAX_WORD_LEN)) != EOF)
    {
        if (isalpha(*word) || *word == '_')
            if (add_word(word, &tree) == 0)
                return 1;
    }

    print_words(&tree);
}
Exemple #9
0
int main(void)
{
    int size;
    char ** words;
    printf("How many words do you wish to enter? ");
    scanf("%d", &size);

    words = make_array(size);
    if (words)
    {
        get_words(words, size);
        print_words(words, size);
    }

    free(words);
    return 0;
}
Exemple #10
0
int main(int argc, char **argv)
{
    if (argc < 3) {
        printf("Usage: scramble dict_file board_file\n");
        return RESULT_INVALID_USAGE;
    }

    FILE *board_file = fopen(argv[2], "r");
    char line[LINE_LENGTH];
    int line_no = 0;

    while (line_no < BOARD_HEIGHT && fgets(line, BOARD_WIDTH + 2, board_file)) {
        for (size_t i = 0 ; i < BOARD_WIDTH ; ++i) {
            if (!isalpha(line[i])) {
                printf("all spaces must contain letters\n");
                return RESULT_INVALID_BOARD;
            }
            line[i] = tolower(line[i]);
        }
        strncpy(board[line_no++], line, BOARD_WIDTH);
        memset(line, 0, sizeof(line));
    }
    if (line_no < BOARD_HEIGHT) {
        printf("you must provide %i lines\n", BOARD_HEIGHT);
        return RESULT_INVALID_BOARD;
    }
    fclose(board_file);


    FILE *dict_file = fopen(argv[1], "r");
    dict_trie = trie_init();

    while (fgets(line, LINE_LENGTH, dict_file)) {
        /* remove trailing newlines before inserting into trie */
        char *pos;
        if ((pos = strchr(line, '\n'))) *pos = '\0';
        trie_insert(dict_trie, line);
    }
    fclose(dict_file);
    
    print_words();

    trie_dispose(dict_trie);

    return RESULT_SUCCESS;
}
static void print_words(struct trie_node *root, size_t buff_i) {
	static char curr_word[MAX_WORD_SZ];

	if (root == NULL) {
		return;
	}

	if (root->score != -1) {
		curr_word[buff_i] = '\0';
		printf("%s -> %d\n", curr_word, root->score);
	}

	size_t i;
	for (i = 0; i < ALPHABET_SZ; i++) {
		curr_word[buff_i] = 'a'+i;
		print_words(root->children[i], buff_i+1);
	}
}
Exemple #12
0
void radix_sort()
{
	int i , l;
	int mx_len  = 0;
	for(i = 0; i < num_words;i++)
	{
		int temp_len = strlen(words[i].str);
		if(temp_len > mx_len )
			mx_len = temp_len;
	}
	update_dup_str(mx_len);
	puts("inside radix_sort");
	print_words();
	printf("%mx_len = %d\n",mx_len);

	for(l = mx_len-1; l >= 0; l--)
	{
		count_sort(l);
	}
}
Exemple #13
0
/*
 * This function reads strings from input stream, either a file stream or stdin.
 * Function also prints the results to file and stdout.
 * Parameters: FILE* in (input filestream), FILE* out (output stream)
 */
void read_words(FILE* in, FILE* out) {

    bool alpha_numeric_word = false; 
    char* temp;
    char* buf = malloc(sizeof (char) * BUFSIZE);
    int* words_in_sentences = malloc(sizeof (int) * 100);
    node *root;
    unsigned int i, sentences, words, temp_word;
    root = NULL;
    i = sentences = words = temp_word = 0;

    for (i = 0; i < 100; i++) {
        words_in_sentences[i] = 0;
    }
    while (true) {
        fscanf(in, "%99s", buf);
        if (feof(in))
            break;
        words++;
        temp_word++;
        temp = buf;

        /*remove full comma from the end and  update sentences*/
        if (temp[strlen(temp) - 1] == '.' && (!isdigit(temp[strlen(temp) - 2]))) {
            temp[strlen(temp) - 1] = '\0'; 
            sentences++;
            if (temp_word < 100)
                ++words_in_sentences[temp_word];
            temp_word = 0;
        }
    /*check if the string doesn't contain alphanumerics at all*/
        for(i = 0; i < strlen(buf); i++){
       if(isalnum(buf[i])){
              alpha_numeric_word = true;
          break;
       }        
    }
    if(alpha_numeric_word){
            temp = trim(buf);
            root = add_word(root, temp);
        alpha_numeric_word = false;
    }
    
    }

    free(buf);

    /*prints the results to file and stdout*/
    for (i = 0; i < 100; i++) {
        if (words_in_sentences[i] > 0) {
            printf("sentences of length %d: %d\n", i, words_in_sentences[i]);
            fprintf(out, "sentences of length %d: %d\n", i, words_in_sentences[i]);

        }
    }
    printf("sentences: %d\n", sentences);
    printf("words: %d\n", words);
    fprintf(out, "sentences: %d\n", sentences);
    fprintf(out, "words %d\n", words);
    print_words(root, out);
    free(words_in_sentences);
    destroy(root);    

}
Exemple #14
0
int main()
{
	int i = 0 ,j = 0, k = 0 , l = 0;
	int curr_page_num = 0;
	char line[100];
	for( i = 0; i < 10000; i++)
	{
		words[i].str = malloc(100);
		words[i].dup_str = malloc(100);
		words[i].idx = malloc(50);
		words[i].num_idx = 0;
	}
	while(1)
	{
		if( fgets(line,100,stdin) == NULL ) 
			break;
		int pagenumberFLAG = 1;
		line[strlen(line)-1] = '\0';
		for(i = 0; i < strlen(line); i++)
		{
			if( !isdigit(line[i]) )
				pagenumberFLAG = 0;
		}
		//		printf("pagenumberFLAG = %d\n",pagenumberFLAG);

		if( pagenumberFLAG == 1) 
		{
			curr_page_num = atoi(line);
			continue;
		}

		//		puts(line);
		//		printf("len  of line = %lu\n",strlen(line));
		i = 0;
		for(; i < strlen(line); )
		{
			//			printf("i* = %d\n",i);
			if(line[i] == ' ' )
			{
				i++;
				continue;
			}
			char temp_str[100];
			k = 0;
			while(line[i] != ' ')
			{
				if(line[i] == '\0') break;
				//				printf("line[%d] = %c\n",i,line[i]);
				temp_str[k++] = line[i];
				i++;
			}
			temp_str[k] = '\0';
			//			printf("i = %d\n",i);
			//			printf("temp_str = %s\n",temp_str);
			//			printf("len_temp_str = %lu\n",strlen(temp_str));
			// now compare this temp_str with all the existing strings in words[]
			int flag_str = 0;
			//	printf("num_words = %d\n",num_words);
			int la = 0;
			for(;temp_str[la];la++)temp_str[la] = tolower(temp_str[la]);  
			for(j = 0;j < num_words; j++)
			{
				if( strcmp(words[j].str,temp_str) == 0 )
				{
					flag_str = 1;
					if( words[j].idx[words[j].num_idx-1] != curr_page_num )
					{
						words[j].idx[words[j].num_idx] = curr_page_num;// index updated
						words[j].num_idx++;
					}
					break;
				}
			}
			if(flag_str == 0 )
			{
				if( isNumeric( temp_str ) ) continue;
				if( !isword(temp_str)) continue;
				strcpy(words[num_words].str,temp_str);  // str updated
				words[num_words].idx[0] = curr_page_num;// index updated
				words[num_words].num_idx++;
				num_words++;
			}
		}
	}
	for(i = 0; i < num_words; i++)   strcpy(words[i].dup_str,words[i].str);
	//	puts("PRINTITITITITITITI\n\n\n");
	//	print_words();

	radix_sort();
	//	puts("\n\nFINAL ANS");
	print_words();

	/********* input is done *******/

	return 0;  
}
void print_known_words(struct trie_node *root) {
	print_words(root, 0);
}