Advertisement
Mazamin

Prova1 C Metodo A

Jan 2nd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1. /*
  2.  * PROVA DI FONDAMENTI DI PROGRAMMAZIONE 22.01.2018
  3.  *
  4.  * In un file sono memorizzati i CARATTERI che compongono una stringa.
  5.  * In ogni linea c'è il carattere effettivo, seguito da una sequenza di CARATTERI che
  6.  * rappresentano le posizioni in cui appare quel carattere. Ad esempio:
  7.  * b 2 4 5
  8.  * significa che il carattere 'b' apparirà  nella stringa nelle posizioni di indice
  9.  * '2', '4' e '5'.
  10.  *
  11.  * Scrivere un programma C che letta da file una sequenza di caratteri, ricostruisca la
  12.  stringa contenente tali caratteri (per semplicità si supponga di avere stringhe al più di
  13.  10 caratteri).
  14.  * Il programma deve stampare a video il numero di caratteri che compongono la stringa
  15.  * e salvare in un file la stringa ottenuta, dopo aver sostituito con "$" un carattere
  16.  * digitato da tastiera.
  17.  *
  18.  * Un esempio di file di input e' il seguente:
  19.  
  20.   b 0
  21.   u 1
  22.   n 3 8
  23.   g 4
  24.   i 5
  25.   r 7
  26.   o 2 6 9
  27.  
  28.  Un esempio di esecuzione e':
  29.  
  30.    Digitare il nome del file contenente la stringa da ricostruire: parola.txt
  31.  
  32.    stringa letta: buongiorno
  33.    I caratteri che compongono la stringa sono: 10
  34.  
  35.    Inserire nome file su cui scrivere la stringa: outparola.txt
  36.    carattere da modificare: n
  37.  
  38.  
  39.    Nel file outparola.txt apparirà  il seguente testo:
  40.    buo$gior$o
  41.  
  42.  *
  43.  * NOTA: IL PROGRAMMA DEVE ESSERE STRUTTURATO MEDIANTE FUNZIONI E PROCEDURE.
  44.  * NON E' POSSIBILE IMPORTARE NUOVE LIBRERIE.
  45.  *
  46.  */
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #define DIM 30
  51.  
  52. int get_string(FILE *, char[]);
  53. void replace_char_string(char[], char, int);
  54. void save_string(FILE *, char[]);
  55.  
  56. int main(){
  57.     FILE * fp;
  58.     char filename[DIM], string[DIM], ch;
  59.     int size;
  60.     puts("Insert input filename:");
  61.     scanf("%s", filename);
  62.     printf("Opening %s...\n", filename);
  63.     if((fp=fopen(filename, "r"))==NULL){
  64.         puts("Fatal error: unable to open file!");
  65.         exit(1);
  66.     }
  67.     else
  68.         puts("File opened successfully!");
  69.     size=get_string(fp, string);
  70.     printf("%d characters in the string\n", size);
  71.     fclose(fp);
  72.     puts("Insert output filename:");
  73.     scanf("%s", filename);
  74.     printf("Opening %s...", filename);
  75.     if((fp=fopen(filename, "w"))==NULL){
  76.         puts("Fatal error: unable to open file!");
  77.         exit(1);
  78.     }
  79.     else
  80.         puts("File opened successfully!");
  81.     puts("Insert char to be replaced:");
  82.     while ((ch = getchar()) != '\n' && ch != EOF) { } /* in order to clear stdin buffer */
  83.     scanf("%c", &ch);
  84.     printf("Replacing every %c in the string %s...\n", ch, string);
  85.     replace_char_string(string, ch, size);
  86.     printf("Saving the string \"%s\" in \"%s\"", string, filename);
  87.     save_string(fp, string);
  88.     fclose(fp);
  89.     return EXIT_SUCCESS;
  90. }
  91.  
  92. int get_string(FILE * fp, char str[]){
  93.     char ch, temp;
  94.     int pos, len=0;
  95.     do{
  96.         fscanf(fp, "%c", &ch);
  97.         while((fscanf(fp, "%d", &pos))==1){
  98.             str[pos]=ch;
  99.             if(pos>len)
  100.                 len=pos;
  101.             fgetc(fp);
  102.         }
  103.         puts("NL");
  104.     }while(!(feof(fp)));
  105.     str[len+1]='\0';
  106.     return len;
  107. }
  108.  
  109. void replace_char_string(char str[], char ch, int size){
  110.     while( size --> 0 )
  111.         if(str[size]==ch)
  112.             str[size]='$';
  113. }
  114.  
  115. void save_string(FILE * fp, char str[]){
  116.     fputs(str, fp);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement