Advertisement
J00ker

Untitled

Nov 10th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /// 0
  5. /*void Prob1() {
  6.     char rasp;
  7.  
  8.     printf("rasp = ");
  9.     scanf("%c", &rasp);
  10.  
  11.     switch(rasp) {
  12.         case 'a'
  13.     }
  14. }*/
  15.  
  16. /// 1
  17. /// A
  18. void EliminareLitera() {
  19.     FILE *fin, *fout, *f;
  20.     char lit;
  21.     char text[100];
  22.     int i, sfarsit;
  23.  
  24.     fin = fopen("f.in", "r");
  25.     fout = fopen("f.out", "w");
  26.  
  27.     if(fin == NULL || fout == NULL)
  28.         printf("Eroare deschidere fisier.");
  29.  
  30.     lit = fgetc(fin);
  31.     fscanf(fin, "\n");
  32.     //printf("%c\n", lit);
  33.  
  34.     while(fgets(text, 100, fin) != NULL) {
  35.         printf("%s", text);
  36.         for(i = 0; i < strlen(text); i++) {
  37.             if(text[i] == lit) {
  38.                 strcpy(text + i, text + i + 1);
  39.                 i--;
  40.             }
  41.         }
  42.         //printf("%s\n", text);
  43.         fputs(text, fout);
  44.     }
  45.     fclose(fin);
  46.     fclose(fout);
  47. }
  48.  
  49. /// B
  50. void EliminareLitera2() {
  51.     FILE *fAppend, *fRead;
  52.     char lit;
  53.     char text[100];
  54.     int i, sfarsit;
  55.  
  56.     fRead = fopen("f.in", "r");
  57.     fAppend = fopen("f.in", "a");
  58.  
  59.     if(fAppend == NULL || fRead == NULL)
  60.         printf("Eroare deschidere fisier.");
  61.  
  62.     fseek(fRead, 0, SEEK_END);
  63.     sfarsit = ftell(fRead);
  64.     //printf("%d", sfarsit);
  65.     fseek(fRead, 0, SEEK_SET);
  66.  
  67.     lit = fgetc(fRead);
  68.     fscanf(fRead, "\n");
  69.     printf("%c\n", lit);
  70.  
  71.     while((fgets(text, 100, fRead) != NULL) && (ftell(fRead) <= sfarsit)) {
  72.         printf("%s", text);
  73.         for(i = 0; i < strlen(text); i++) {
  74.             if(text[i] == lit) {
  75.                 strcpy(text + i, text + i + 1);
  76.                 i--;
  77.             }
  78.         }
  79.         //printf("%s\n", text);
  80.         fputs(text, fAppend);
  81.     }
  82.     fclose(fRead);
  83.     fclose(fAppend);
  84. }
  85.  
  86. /// 2
  87. void InlocuireLitera() {
  88.     FILE *f;
  89.     char a, b;
  90.     char text[100];
  91.     int i, pos;
  92.  
  93.     f = fopen("f.in", "r+");
  94.  
  95.     if(f == NULL)
  96.         printf("Eroare deschidere fisier.");
  97.  
  98.     a = fgetc(f);
  99.     fgetc(f); /// spatiu
  100.     b = fgetc(f);
  101.     fscanf(f, "\n");
  102.     printf("%c %c\n", a, b);
  103.  
  104.     char *p;
  105.     pos = ftell(f);
  106.     while(fgets(text, 100, f) != NULL) {
  107.         printf("%s", text);
  108.  
  109. /*
  110.         p = strchr(text, a);
  111.         while(p = strchr(p, a) != NULL) {
  112.             p[0] = b;
  113.         }
  114. */
  115.  
  116.         for(i = 0; i < strlen(text); i++)
  117.             if(text[i] == a)
  118.                 text[i] = b;
  119.  
  120.         printf("%s\n", text);
  121.  
  122.         fseek(f, pos, SEEK_SET);
  123.         fputs(text, f);
  124.         fflush(f);
  125.         pos = ftell(f);
  126.     }
  127.     fclose(f);
  128. }
  129.  
  130. /// 3
  131. void ConvCase() {
  132.     FILE *f;
  133.     char text[100];
  134.     int i, pos;
  135.  
  136.     f = fopen("f.in", "r+");
  137.  
  138.     if(f == NULL)
  139.         printf("Eroare deschidere fisier.");
  140.  
  141.     pos = ftell(f);
  142.     while(fgets(text, 100, f) != NULL) {
  143.         printf("%s", text);
  144.  
  145.         for(i = 0; i < strlen(text); i++) {
  146.             if(!(text[i] >= 'a' || text[i] <= 'z') &&
  147.                !(text[i] >= 'A' || text[i] <= 'Z')) continue;
  148.             if(text[i] >= 'a' && text[i] <= 'z')
  149.                 text[i] += 'A' - 'a';
  150.             else text[i] -= 'A' - 'a';
  151.         }
  152.  
  153.         printf("%s\n", text);
  154.  
  155.         fseek(f, pos, SEEK_SET);
  156.         fputs(text, f);
  157.         fflush(f);
  158.         pos = ftell(f);
  159.     }
  160.     fclose(f);
  161. }
  162.  
  163. /// 4
  164. void AparitiiLitere() {
  165.     FILE *f;
  166.     char text[100];
  167.     int i, j;
  168.     int ap['z' - 'a' + 2] = {0};
  169.  
  170.     f = fopen("f.in", "r+");
  171.  
  172.     if(f == NULL)
  173.         printf("Eroare deschidere fisier.");
  174.  
  175.     while(fgets(text, 100, f) != NULL) {
  176.         for(i = 0; i < strlen(text); i++) {
  177.             char c = text[i];
  178.             if(!(text[i] >= 'a' || text[i] <= 'z') &&
  179.                !(text[i] >= 'A' || text[i] <= 'Z')) continue;
  180.  
  181.             if(text[i] >= 'a' && text[i] <= 'z')
  182.                 ap[ text[i] - 'a' ]++;
  183.             else ap[ text[i] - 'A' ]++;
  184.  
  185.         }
  186.  
  187.         for(j = 0; j <= 'z' - 'a'; j++) {
  188.             printf("%c ", j+'a');
  189.         }
  190.         printf("\n");
  191.         for(j = 0; j <= 'z' - 'a'; j++) {
  192.             printf("%d ", ap[j]);
  193.         }
  194.         printf("\n\n");
  195.     }
  196.  
  197.     int posMax = 0;
  198.     for(j = 0; j <= 'z' - 'a'; j++) {
  199.         if(ap[j] > ap[posMax])
  200.             posMax = j;
  201.     }
  202.     for(j = 0; j <= 'z' - 'a'; j++) {
  203.         if(ap[j] == ap[posMax])
  204.             printf("%c ", j + 'a');
  205.     }
  206.  
  207.     if(ap[posMax] == 0) printf("Nu exista litere");
  208.  
  209.     fclose(f);
  210.     /*
  211.     */
  212. }
  213.  
  214. /// 5
  215. void GasireCuvant() {
  216.     FILE *f;
  217.     char text[100], cuv[100], *p;
  218.     int i, j;
  219.  
  220.     printf("Cuvant: ");
  221.     scanf("%s", cuv);
  222.  
  223.     f = fopen("f.in", "r+");
  224.  
  225.     if(f == NULL)
  226.         printf("Eroare deschidere fisier.");
  227.  
  228.     while(fgets(text, 100, f) != NULL) {
  229.         p = text;
  230.         while((p = strstr(p, cuv)) != NULL) {
  231.             while(p[0] != ' ' && p != text) p--;
  232.             if(p[0] == ' ') p++;
  233.  
  234.             while(p[0] != ' ' && p[0] != NULL){
  235.                 printf("%c", p[0]);
  236.                 p++;
  237.             }
  238.             printf("\n");
  239.         }
  240.     }
  241.  
  242.     fclose(f);
  243. }
  244.  
  245. int main() {
  246.     /*
  247.     EliminareLitera();
  248.     EliminareLitera2();
  249.     InlocuireLitera();
  250.     ConvCase();
  251.     AparitiiLitere();
  252.     */
  253.     GasireCuvant();
  254.     return 0;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement