Advertisement
visoft

sortat_cuvinte

Dec 6th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. /*
  2.  ============================================================================
  3.  Name        : sortat_cuvinte.c
  4.  Author      :
  5.  Version     :
  6.  Copyright   : Your copyright notice
  7.  Description : Hello World in C, Ansi-style
  8.  ============================================================================
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. typedef struct{
  16.     char *cuvant;
  17.     int aparitii;
  18. } Aparitii;
  19.  
  20. int insereaza_cuvant(Aparitii *cuvinte, int nr_crt, char*de_inserat){
  21.     int gasit = 0;
  22.     for(int i = 0; i < nr_crt; i++){
  23.         int egal;
  24.         egal = strcmp(cuvinte[i].cuvant, de_inserat);
  25.         if (egal == 0){
  26.             cuvinte[i].aparitii++;
  27.             gasit = 1;
  28.             break;
  29.         }
  30.     }
  31.     if (!gasit){
  32.         cuvinte[nr_crt].cuvant = de_inserat;
  33.         cuvinte[nr_crt].aparitii = 1;
  34.         nr_crt++;
  35.     }
  36.     return nr_crt;
  37. }
  38.  
  39.  
  40. int main(void) {
  41.     char sir_initial[] = "sa ion au au sa sa sa ion";
  42.     char *delimitatori = " ";
  43.     char *subsir;
  44.     Aparitii *cuvinte = (Aparitii*)malloc(20 * sizeof(Aparitii));
  45.     int nr_total_cuvinte = 0;
  46.     subsir = strtok(sir_initial, delimitatori);
  47.     while(subsir != NULL){
  48.         nr_total_cuvinte =
  49.                 insereaza_cuvant(cuvinte, nr_total_cuvinte, subsir);
  50.         subsir = strtok(NULL, delimitatori);
  51.     }
  52.  
  53.     for(int i=0;i<nr_total_cuvinte;i++){
  54.         printf("%s:%d\n", cuvinte[i].cuvant, cuvinte[i].aparitii);
  55.     }
  56.  
  57.     return EXIT_SUCCESS;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement